rmtab.c revision 4a508a79352d55b18841bdb18d6d7b4153b6c03a
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/pathconf.h>
#include <sys/systeminfo.h>
#include <signal.h>
#include <locale.h>
#include <unistd.h>
#include <thread.h>
#include <syslog.h>
#include "../lib/sharetab.h"
#include "hashset.h"
#include "mountd.h"
/*
* There is nothing magic about the value selected here. Too low,
* and mountd might spend too much time rewriting the rmtab file.
* Too high, it won't do it frequently enough.
*/
static int rmtab_del_thresh = 250;
#define RMTAB_TOOMANY_DELETED() \
/*
* mountd's version of a "struct mountlist". It is the same except
* for the added ml_pos field.
*/
struct mntentry {
char *m_host;
char *m_path;
long m_pos;
};
static int mntentry_equal(const void *, const void *);
static uint32_t mntentry_hash(const void *);
static int mntlist_contains(char *, char *);
static void rmtab_delete(long);
static long rmtab_insert(char *, char *);
static void rmtab_rewrite(void);
static void rmtab_parse(char *buf);
#define exstrdup(s) \
static int rmtab_inuse;
static int rmtab_deleted;
/*
* already appears in the mount list.
*/
static int
{
struct mntentry m;
}
/*
* Add an entry to the mount list.
* First check whether it's there already - the client
* may have crashed and be rebooting.
*/
static void
{
struct mntentry *m;
}
}
void
{
(void) rw_wrlock(&rmtab_lock);
(void) rw_unlock(&rmtab_lock);
}
/*
* Delete an entry from the mount list.
*/
void
{
(void) rw_wrlock(&rmtab_lock);
rmtab_delete(m->m_pos);
free(m);
if (RMTAB_TOOMANY_DELETED())
}
(void) rw_unlock(&rmtab_lock);
}
/*
* Delete all entries for a host from the mount list
*/
void
mntlist_delete_all(char *host)
{
struct mntentry *m;
(void) rw_wrlock(&rmtab_lock);
continue;
rmtab_delete(m->m_pos);
free(m);
}
if (RMTAB_TOOMANY_DELETED())
(void) rw_unlock(&rmtab_lock);
}
/*
* Equivalent to xdr_mountlist from librpcsvc but for HASHSET
* rather that for a linked list. It is used only to encode data
* from HASHSET before sending it over the wire.
*/
static bool_t
{
for (;;) {
return (FALSE);
}
if (!more_data)
break;
return (FALSE);
}
}
return (TRUE);
}
void
{
(void) rw_rdlock(&rmtab_lock);
errno = 0;
(void) rw_unlock(&rmtab_lock);
}
/*
* Compute a 32 bit hash value for an mntlist entry.
*/
/*
* The string hashing algorithm is from the "Dragon Book" --
* "Compilers: Principles, Tools & Techniques", by Aho, Sethi, Ullman
*
*/
static uint_t
{
uint_t g;
for (; *s != '\0'; s++) {
if ((g = (hash & 0xf0000000)) != 0) {
hash ^= (g >> 24);
hash ^= g;
}
}
return (hash);
}
static uint32_t
mntentry_hash(const void *p)
{
return (hash);
}
/*
* Compare mntlist entries.
* The comparison ignores a value of m_pos.
*/
static int
{
}
/*
*/
static void
{
if (rmtabf)
/* Rewrite the file. */
struct mntentry *m;
rmtab_inuse = rmtab_deleted = 0;
}
}
/*
* The buffer s should be ended with a NUL char.
*/
static void
rmtab_parse(char *s)
{
char *host;
char *path;
char *tmp;
if (*s == '#')
goto skip_rest;
host = s;
for (;;) {
switch (*s++) {
case '\0':
return;
case '\n':
goto host_part;
case ':':
s[-1] = '\0';
goto path_part;
case '[':
if (tmp) {
*tmp = '\0';
host = s;
s = ++tmp;
} else
*tmp = ']';
}
default:
continue;
}
}
path = s;
for (;;) {
switch (*s++) {
case '\n':
s[-1] = '\0';
goto host_part;
case '\0':
return;
default:
continue;
}
}
for (;;) {
switch (*++s) {
case '\n':
goto host_part;
case '\0':
return;
default:
continue;
}
}
}
/*
* Read in contents of rmtab.
* Call rmtab_parse to parse the file and store entries in mntlist.
* Rewrites the file to get rid of unused entries.
*/
void
{
/*
* Don't need to lock the list at this point
* because there's only a single thread running.
*/
/*
* - if fread returns RMTAB_LOADLEN we can be in the middle
* of a line so change the last newline character into NUL
* and seek back to the next character after newline.
* - otherwise set NUL behind the last character read.
*/
if (len == RMTAB_LOADLEN) {
int i;
for (i = 1; i < len; i++) {
SEEK_CUR);
goto parse;
}
}
}
/* Put a NUL character at the end of buffer */
}
}
}
/*
* Write an entry at the current location in rmtab
* for the given client and path.
*
* Returns the starting position of the entry
* or -1 if there was an error.
*/
long
{
long pos;
return (-1);
}
/*
* Check if host is an IPv6 literal
*/
return (-1);
}
} else {
return (-1);
}
}
return (-1);
}
rmtab_inuse++;
return (pos);
}
/*
* Mark as unused the rmtab entry at the given offset in the file.
*/
void
rmtab_delete(long pos)
{
rmtab_inuse--;
}
}