/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This module adds support to the RCM framework for mounted filesystems.
*
* The module provides this functionality:
* 1) reports device usage for mounted filesystems
* 2) prevents offline operations for mounted resources
* 3) prevents suspend operations (unless forced) of those filesystems
* deemed critical for the continued operation of the OS
* 4) propagates RCM operations from mounted resources to the consumers
* of files within the mounted filesystems
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <synch.h>
#include <libintl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include "rcm_module.h"
/* Definitions */
typedef struct hashentry {
int n_mounts;
char *special;
char *fstype;
char **mountps;
} hashentry_t;
typedef struct {
} cache_t;
/* Forward Declarations */
/* module interface routines */
static int mnt_register(rcm_handle_t *);
static int mnt_unregister(rcm_handle_t *);
nvlist_t *, rcm_info_t **);
uint_t, char **, rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
/* cache functions */
static cache_t *cache_create();
static void free_cache(cache_t **);
static void free_entry(hashentry_t **);
static void free_list(char **);
/* miscellaneous functions */
static void register_rsrc(rcm_handle_t *, char *);
static void unregister_rsrc(rcm_handle_t *, char *);
static char *create_message(char *, char *, char **);
static int detect_critical_failure(char **, uint_t, char **);
static int is_critical(char *);
static int use_cache(char *, char **, char ***);
static void prune_dependents(char **, char *);
static char **create_dependents(hashentry_t *);
/* Module-Private data */
{
};
/* Module Interface Routines */
/*
* rcm_mod_init()
*
* Called when module is loaded. Returns the ops vector.
*/
struct rcm_mod_ops *
{
return (&mnt_ops);
}
/*
* rcm_mod_info()
*
* Returns a string identifying this module.
*/
const char *
{
return ("File system module 1.9");
}
/*
* rcm_mod_fini()
*
* Called when module is unloaded. Frees up all used memory.
*
* Locking: the cache is locked for the duration of this function.
*/
int
{
(void) mutex_lock(&cache_lock);
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* mnt_register()
*
* Called to synchronize the module's registrations. Results in the
* construction of a new cache, destruction of any old cache data,
* and a full synchronization of the module's registrations.
*
* Locking: the cache is locked for the duration of this function.
*/
int
{
(void) mutex_lock(&cache_lock);
/* cache_sync() does all of the necessary work */
"FILESYS: failed to synchronize cache (%s).\n",
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* mnt_unregister()
*
* Manually walk through the cache, unregistering all the special
* files and mount points.
*
* Locking: the cache is locked throughout the execution of this
* routine because it reads and modifies cache links continuously.
*/
int
{
(void) mutex_lock(&cache_lock);
/* Unregister everything in the cache */
if (mnt_cache) {
}
}
}
/* Destroy the cache */
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* mnt_offline()
*
* Filesystem resources cannot be offlined. They can however be retired
* if they don't provide a critical service. The offline entry point
* checks if this is a retire operation and if it is and the filesystem
* doesn't provide a critical service, the entry point returns success
* For all other cases, failure is returned.
* Since no real action is taken, QUERY or not doesn't matter.
*/
int
{
char **dependents;
int retval;
int i;
/* Retrieve necessary info from the cache */
if (flags & RCM_RETIRE_REQUEST)
return (RCM_NO_CONSTRAINT);
else
return (RCM_FAILURE);
}
if (flags & RCM_RETIRE_REQUEST) {
(void) mutex_lock(&cache_lock);
"failed to look up \"%s\" in cache (%s).\n",
(void) mutex_unlock(&cache_lock);
goto out;
}
"FILESYS: zfs: NO_CONSTRAINT: %s\n", rsrc);
} else {
for (i = 0; dependents[i] != NULL; i++) {
if (is_critical(dependents[i])) {
"CRITICAL %s\n", rsrc);
break;
}
}
}
(void) mutex_unlock(&cache_lock);
goto out;
}
/* Convert the gathered dependents into an error message */
"FILESYS: failed to construct offline message (%s).\n",
}
out:
return (retval);
}
/*
* mnt_online()
*
* Filesystem resources aren't offlined, so there's really nothing to do
* here.
*/
int
{
return (RCM_SUCCESS);
}
/*
* mnt_getinfo()
*
* Report how a given resource is in use by this module. And also
* possibly include dependent consumers of the mounted filesystems.
*/
int
{
char **dependents;
/* Retrieve necessary info from the cache */
return (RCM_FAILURE);
/* Convert the gathered dependents into a usage message */
"FILESYS: failed to construct usage message (%s).\n",
return (RCM_FAILURE);
}
/* Recurse on dependents if necessary */
if (dependents[0] != NULL) {
depend_info)) != RCM_SUCCESS) {
}
}
}
/* Free up info retrieved from the cache */
return (rv);
}
/*
* mnt_suspend()
*
* Notify all dependents that the resource is being suspended.
* Since no real action is taken, QUERY or not doesn't matter.
*/
int
{
char **dependents;
/* Retrieve necessary info from the cache */
return (RCM_FAILURE);
/* Unforced suspensions fail if any of the dependents are critical */
return (RCM_FAILURE);
}
/* Recurse on dependents if necessary */
if (dependents[0] != NULL)
}
}
return (rv);
}
/*
* mnt_resume()
*
* Resume all the dependents of a suspended filesystem.
*/
int
{
char **dependents;
/* Retrieve necessary info from the cache */
return (RCM_FAILURE);
/* Recurse on dependents if necessary */
if (dependents[0] != NULL) {
depend_info)) != RCM_SUCCESS) {
}
}
}
return (rv);
}
static int
{
char *cp;
char *start;
"line: %s\n", line);
return (-1);
}
cp++;
if (*cp == '#')
return (-1);
cp++;
*cp = '\0';
return (0);
}
static int
{
char r[PATH_MAX];
char s[PATH_MAX];
goto error;
goto error;
goto error;
}
goto error;
}
return (0);
else
return (1);
return (-1);
}
static int
{
int retval;
int update;
return (RCM_FAILURE);
}
return (RCM_FAILURE);
}
update = 0;
char *l;
l = line;
goto foot;
}
l = line;
goto foot;
}
update = 1;
/* Paths match. Disable this entry */
line);
l = newline;
foot:
update = 0;
break;
}
}
if (vfp)
if (tfp)
if (update) {
}
}
return (retval);
}
/*
* mnt_remove()
*
* Remove will only be called in the retire case i.e. if RCM_RETIRE_NOTIFY
* flag is set.
*
* If the flag is not set, then return failure and log the mistake if a
* remove is ever received for a mounted filesystem resource.
*/
int
{
if (!(flag & RCM_RETIRE_NOTIFY)) {
/* Log the mistake */
"\"%s\"\n", rsrc);
return (RCM_FAILURE);
}
return (disable_vfstab_entry(rsrc));
}
/*
* Cache management routines
*/
/*
* cache_create()
*
* This routine constructs a new cache of the current mnttab file.
*
* Locking: the cache must be locked prior to calling this function.
*
* Return Values: NULL with errno set on failure, new cache point on
* success.
*/
static cache_t *
{
int i;
/*
* To keep the hash table relatively sparse, default values are
* used for smaller mnttab files and these values are scaled up
* as a fraction of the total mnttab file size for larger ones.
*/
"FILESYS: failed to stat \"%s\" (%s).\n", MNTTAB,
return (NULL);
}
} else {
size = HASH_DEFAULT;
}
/* Allocate a new empty cache */
"FILESYS: failed to allocate cache (%s).\n",
return (NULL);
}
/* Allocate an empty hash table for the registered special devices */
"FILESYS: failed to allocate mount table (%s).\n",
free_cache(&cache);
return (NULL);
}
/* Open the mnttab file */
"FILESYS: failed to open \"%s\" (%s).\n", MNTTAB,
free_cache(&cache);
return (NULL);
}
/* Insert each mnttab entry into the cache */
/* Well, not each entry... some are meant to be ignored */
continue;
"FILESYS: cache insertion failure (%s).\n",
free_cache(&cache);
return (NULL);
}
}
/* Close the mnttab file */
return (cache);
}
/*
* free_cache()
*
* Free up all the memory associated with a cache.
*
* Locking: the cache must be locked before calling this function.
*/
static void
{
/* Do nothing with empty caches */
return;
/* Walk through the hashtable, emptying it */
while (entry) {
free_entry(&entry);
}
}
}
}
/*
* free_entry()
*
* Free up memory associated with a hashtable entry.
*
* Locking: the cache must be locked before calling this function.
*/
static void
{
if (entryp) {
if (*entryp) {
}
}
}
/*
* free_list()
*
* Free up memory associated with a null terminated list of names.
*/
static void
{
int i;
if (list) {
}
}
/*
* cache_sync()
*
* Resynchronize the mnttab cache with the mnttab file.
*
* Locking: the cache must be locked before calling this function.
*
* Return Values: -1 with errno set on failure, 0 on success.
*/
static int
{
/* Only accept valid arguments */
"FILESYS: invalid arguments to cache_sync().\n");
return (-1);
}
/* Do nothing if there's already an up-to-date cache */
if (old_cache) {
return (0);
}
} else {
"FILESYS: failed to stat \"%s\", cache is stale "
return (-1);
}
}
/* Create a new cache based on the new mnttab file. */
"FILESYS: failed creating cache, cache is stale (%s).\n",
return (-1);
}
/* Register any specials found in the new cache but not the old one */
}
}
}
/* Pass the new cache pointer to the calling function */
/* If there wasn't an old cache, return successfully now */
return (0);
/*
* If there was an old cache, then unregister whatever specials it
* contains that aren't in the new cache. And then destroy the old
* cache.
*/
}
}
}
return (0);
}
/*
* cache_insert()
*
* Given a cache and a mnttab entry, this routine inserts that entry in
* the cache. The mnttab entry's special device and filesystem type
* is added to the 'mounts' hashtable of the cache, and the entry's
* mountp value is added to the list of associated mountpoints for the
* corresponding hashtable entry.
*
* Locking: the cache must be locked before calling this function.
*
* Return Values: -1 with errno set on failure, 0 on success.
*/
static int
{
char **mountps;
/* Only accept valid arguments */
return (-1);
}
/*
* Disregard any non-loopback mounts whose special device names
* don't begin with "/dev".
*/
return (0);
/*
* Find the special device's entry in the mounts hashtable, allocating
* a new entry if necessary.
*/
break;
}
"FILESYS: failed to allocate special device name "
free_entry(&entry);
return (-1);
}
}
/*
* Keep entries in the list of mounts unique, so exit early if the
* mount is already in the list.
*/
return (0);
}
/*
* Add this mountpoint to the list of mounts associated with the
* special device.
*/
"FILESYS: failed to allocate mountpoint name (%s).\n",
free_entry(&entry);
}
return (-1);
}
return (0);
}
/*
* cache_lookup()
*
* Searches the cached table of mounts for a special device entry.
*
* Locking: the cache must be locked before calling this function.
*
* Return Value: NULL with errno set if failure, pointer to existing
* cache entry when successful.
*/
static hashentry_t *
{
/* Only accept valid arguments */
return (NULL);
}
/* Search the cached mounts table for the resource's entry */
return (entry);
}
}
return (NULL);
}
/*
* Miscellaneous Functions
*/
/*
* hash()
*
* A naive hashing function that converts a string 's' to an index in a
* hash table of size 'h'. It seems to spread entries around well enough.
*/
static uint32_t
{
unsigned char *byte;
while (*byte) {
byte++;
}
}
return (sum % h);
}
/*
* register_rsrc()
*
* Registers for any given resource, unless it's "/".
*/
static void
{
/* Only accept valid arguments */
return;
/*
* Register any resource other than "/" or "/devices"
*/
"FILESYS: failed to register %s\n", rsrc);
}
}
}
/*
* unregister_rsrc()
*
* Unregister a resource. This does a little filtering since we know
* "/" can't be registered, so we never bother unregistering for it.
*/
static void
{
/* Unregister any resource other than "/" */
}
}
/*
* create_message()
*
* Given some header strings and a list of dependent names, this
* constructs a single string. If there's only one dependent, the
* string consists of the first header and the only dependent appended
* to the end of the string enclosed in quotemarks. If there are
* multiple dependents, then the string uses the second header and the
* full list of dependents is appended at the end as a comma separated
* list of names enclosed in quotemarks.
*/
static char *
{
int i;
int ndependents;
char *msg_buf;
char *msg_header;
/* Count the number of dependents */
/* If there are no dependents, fail */
if (ndependents == 0) {
return (NULL);
}
/* Pick the appropriate header to use based on amount of dependents */
if (ndependents == 1) {
msg_header = header;
} else {
}
/* Compute the size required for the message buffer */
for (i = 0; dependents[i] != NULL; i++)
/* Allocate the message buffer */
"FILESYS: failed to allocate message buffer (%s).\n",
return (NULL);
}
/* Fill in the message buffer */
for (i = 0; dependents[i] != NULL; i++) {
if ((i + 1) < ndependents)
}
return (msg_buf);
}
/*
* create_dependents()
*
* Creates a copy of the list of dependent mounts associated with a
* given hashtable entry from the cache.
*
* Return Values: NULL with errno set on failure, the resulting list of
* dependent resources when successful.
*/
static char **
{
int i;
char **dependents;
return (NULL);
}
return (NULL);
}
/* Allocate space for the full dependency list */
if (dependents == NULL) {
"FILESYS: failed to allocate dependents (%s).\n",
return (NULL);
}
/* Copy all the dependent names into the new list of dependents */
"FILESYS: failed to allocate dependent \"%s\" "
return (NULL);
}
}
return (dependents);
}
/*
* detect_critical_failure()
*
* Given a list of dependents, a place to store an error message, and
* the flags associated with an operation, this function detects whether
* or not the operation should fail due to the presence of any critical
* filesystem resources. When a failure is detected, an appropriate
* error message is constructed and passed back to the caller. This is
* called during a suspend request operation.
*
* Return Values: 0 when a critical resource failure shouldn't prevent
* the operation, and 1 when such a failure condition does exist.
*/
static int
{
int i;
int n_critical;
char *tmp;
/* Do nothing if the operation is forced or there are no dependents */
return (0);
/*
* Count how many of the dependents are critical, and shift the
* critical resources to the head of the list.
*/
if (dependents) {
if (is_critical(dependents[i])) {
if (n_critical != i) {
dependents[i] = tmp;
}
n_critical++;
}
}
}
/* If no criticals were found, do nothing and return */
if (n_critical == 0)
return (0);
/*
* Criticals were found. Prune the list appropriately and construct
* an error message.
*/
/* Prune non-criticals out of the list */
free(dependents[i]);
dependents[i] = NULL;
}
/* Construct the critical resource error message */
return (1);
}
/*
* is_critical()
*
* Test a resource to determine if it's critical to the system and thus
* cannot be suspended.
*
* Return Values: 1 if the named resource is critical, 0 if not.
*/
static int
{
return (1);
return (0);
}
/*
* use_cache()
*
* This routine handles all the tasks necessary to lookup a resource
* in the cache and extract a separate list of dependents for that
* entry. If an error occurs while doing this, an appropriate error
* message is passed back to the caller.
*
* Locking: the cache is locked for the whole duration of this function.
*/
static int
{
(void) mutex_lock(&cache_lock);
"FILESYS: failed looking up \"%s\" in cache (%s).\n",
(void) mutex_unlock(&cache_lock);
return (-1);
}
(void) mutex_unlock(&cache_lock);
return (0);
}
/*
* prune_dependents()
*
* Before calling back into RCM with a list of dependents, the list
* must be cleaned up a little. To avoid infinite recursion, "/" and
* the named resource must be pruned out of the list.
*/
static void
{
int i;
int n;
if (dependents) {
/* Set 'n' to the total length of the list */
for (n = 0; dependents[n] != NULL; n++);
/*
* Move offending dependents to the tail of the list and
* then truncate the list.
*/
for (i = 0; dependents[i] != NULL; i++) {
free(dependents[i]);
dependents[n] = NULL;
i--;
n--;
}
}
}
}