fresh git tree for public release
we regretfully had to remove our git history for licensing reasons Signed-off-by: Casey Bodley <cbodley@citi.umich.edu>
This commit is contained in:
commit
0ad4db4fad
271 changed files with 71255 additions and 0 deletions
332
daemon/nfs41_server.c
Normal file
332
daemon/nfs41_server.c
Normal file
|
|
@ -0,0 +1,332 @@
|
|||
/* Copyright (c) 2010
|
||||
* The Regents of the University of Michigan
|
||||
* All Rights Reserved
|
||||
*
|
||||
* Permission is granted to use, copy and redistribute this software
|
||||
* for noncommercial education and research purposes, so long as no
|
||||
* fee is charged, and so long as the name of the University of Michigan
|
||||
* is not used in any advertising or publicity pertaining to the use
|
||||
* or distribution of this software without specific, written prior
|
||||
* authorization. Permission to modify or otherwise create derivative
|
||||
* works of this software is not granted.
|
||||
*
|
||||
* This software is provided as is, without representation or warranty
|
||||
* of any kind either express or implied, including without limitation
|
||||
* the implied warranties of merchantability, fitness for a particular
|
||||
* purpose, or noninfringement. The Regents of the University of
|
||||
* Michigan shall not be liable for any damages, including special,
|
||||
* indirect, incidental, or consequential damages, with respect to any
|
||||
* claim arising out of or in connection with the use of the software,
|
||||
* even if it has been or is hereafter advised of the possibility of
|
||||
* such damages.
|
||||
*/
|
||||
|
||||
#include <Windows.h>
|
||||
#include <strsafe.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "wintirpc.h"
|
||||
#include "rpc/rpc.h"
|
||||
|
||||
#include "name_cache.h"
|
||||
#include "daemon_debug.h"
|
||||
#include "nfs41.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
#define SRVLVL 2 /* dprintf level for server logging */
|
||||
|
||||
|
||||
/* nfs41_server_list */
|
||||
struct server_list {
|
||||
struct list_entry head;
|
||||
CRITICAL_SECTION lock;
|
||||
};
|
||||
static struct server_list g_server_list;
|
||||
|
||||
#define server_entry(pos) list_container(pos, nfs41_server, entry)
|
||||
|
||||
|
||||
void nfs41_server_list_init()
|
||||
{
|
||||
list_init(&g_server_list.head);
|
||||
InitializeCriticalSection(&g_server_list.lock);
|
||||
}
|
||||
|
||||
/* http://tools.ietf.org/html/rfc5661#section-1.6
|
||||
* 1.6. General Definitions: Server Owner:
|
||||
* "When the client has two connections each to a peer with the same major
|
||||
* identifier, the client assumes that both peers are the same server (the
|
||||
* server namespace is the same via each connection)" */
|
||||
|
||||
/* http://tools.ietf.org/html/rfc5661#section-2.10.4
|
||||
* 2.10.4. Server Scope
|
||||
* "When the server scope values are the same, server owner value may be
|
||||
* validly compared. In cases where the server scope values are different,
|
||||
* server owner values are treated as different even if they contain all
|
||||
* identical bytes." */
|
||||
|
||||
/* given these definitions, we require that both the server_owner.major_id
|
||||
* and server_scope are identical when matching instances of nfs41_server */
|
||||
|
||||
struct server_info {
|
||||
const char *scope;
|
||||
const char *owner;
|
||||
};
|
||||
|
||||
static int server_compare(
|
||||
const struct list_entry *entry,
|
||||
const void *value)
|
||||
{
|
||||
const nfs41_server *server = server_entry(entry);
|
||||
const struct server_info *info = (const struct server_info*)value;
|
||||
const int diff = strncmp(server->scope, info->scope, NFS4_OPAQUE_LIMIT);
|
||||
return diff ? diff : strncmp(server->owner, info->owner, NFS4_OPAQUE_LIMIT);
|
||||
}
|
||||
|
||||
static int server_entry_find(
|
||||
IN struct server_list *servers,
|
||||
IN const struct server_info *info,
|
||||
OUT struct list_entry **entry_out)
|
||||
{
|
||||
*entry_out = list_search(&servers->head, info, server_compare);
|
||||
return *entry_out ? NO_ERROR : ERROR_FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
static int server_create(
|
||||
IN const struct server_info *info,
|
||||
OUT nfs41_server **server_out)
|
||||
{
|
||||
int status = NO_ERROR;
|
||||
nfs41_server *server;
|
||||
|
||||
server = calloc(1, sizeof(nfs41_server));
|
||||
if (server == NULL) {
|
||||
status = GetLastError();
|
||||
eprintf("failed to allocate server %s\n", info->owner);
|
||||
goto out;
|
||||
}
|
||||
|
||||
StringCchCopyA(server->scope, NFS4_OPAQUE_LIMIT, info->scope);
|
||||
StringCchCopyA(server->owner, NFS4_OPAQUE_LIMIT, info->owner);
|
||||
InitializeSRWLock(&server->addrs.lock);
|
||||
nfs41_superblock_list_init(&server->superblocks);
|
||||
nfs41_name_cache_create(&server->name_cache);
|
||||
*server_out = server;
|
||||
out:
|
||||
return status;
|
||||
}
|
||||
|
||||
static void server_free(
|
||||
IN nfs41_server *server)
|
||||
{
|
||||
dprintf(SRVLVL, "server_free(%s)\n", server->owner);
|
||||
nfs41_superblock_list_free(&server->superblocks);
|
||||
nfs41_name_cache_free(&server->name_cache);
|
||||
free(server);
|
||||
}
|
||||
|
||||
static __inline void server_ref_locked(
|
||||
IN nfs41_server *server)
|
||||
{
|
||||
server->ref_count++;
|
||||
dprintf(SRVLVL, "nfs41_server_ref(%s) count %d\n",
|
||||
server->owner, server->ref_count);
|
||||
}
|
||||
|
||||
void nfs41_server_ref(
|
||||
IN nfs41_server *server)
|
||||
{
|
||||
EnterCriticalSection(&g_server_list.lock);
|
||||
|
||||
server_ref_locked(server);
|
||||
|
||||
LeaveCriticalSection(&g_server_list.lock);
|
||||
}
|
||||
|
||||
void nfs41_server_deref(
|
||||
IN nfs41_server *server)
|
||||
{
|
||||
EnterCriticalSection(&g_server_list.lock);
|
||||
|
||||
server->ref_count--;
|
||||
dprintf(SRVLVL, "nfs41_server_deref(%s) count %d\n",
|
||||
server->owner, server->ref_count);
|
||||
if (server->ref_count == 0) {
|
||||
list_remove(&server->entry);
|
||||
server_free(server);
|
||||
}
|
||||
|
||||
LeaveCriticalSection(&g_server_list.lock);
|
||||
}
|
||||
|
||||
static void server_addrs_add(
|
||||
IN OUT struct server_addrs *addrs,
|
||||
IN const netaddr4 *addr)
|
||||
{
|
||||
/* we keep a list of addrs used to connect to each server. once it gets
|
||||
* bigger than NFS41_ADDRS_PER_SERVER, overwrite the oldest addrs. use
|
||||
* server_addrs.next_index to implement a circular array */
|
||||
|
||||
AcquireSRWLockExclusive(&addrs->lock);
|
||||
|
||||
if (multi_addr_find(&addrs->addrs, addr, NULL)) {
|
||||
dprintf(SRVLVL, "server_addrs_add() found existing addr '%s'.\n",
|
||||
addr->uaddr);
|
||||
} else {
|
||||
/* overwrite the address at 'next_index' */
|
||||
StringCchCopyA(addrs->addrs.arr[addrs->next_index].netid,
|
||||
NFS41_NETWORK_ID_LEN, addr->netid);
|
||||
StringCchCopyA(addrs->addrs.arr[addrs->next_index].uaddr,
|
||||
NFS41_UNIVERSAL_ADDR_LEN, addr->uaddr);
|
||||
|
||||
/* increment/wrap next_index */
|
||||
addrs->next_index = (addrs->next_index + 1) % NFS41_ADDRS_PER_SERVER;
|
||||
/* update addrs.count if necessary */
|
||||
if (addrs->addrs.count < addrs->next_index)
|
||||
addrs->addrs.count = addrs->next_index;
|
||||
|
||||
dprintf(SRVLVL, "server_addrs_add() added new addr '%s'.\n",
|
||||
addr->uaddr);
|
||||
}
|
||||
ReleaseSRWLockExclusive(&addrs->lock);
|
||||
}
|
||||
|
||||
void nfs41_server_addrs(
|
||||
IN nfs41_server *server,
|
||||
OUT multi_addr4 *addrs)
|
||||
{
|
||||
struct server_addrs *saddrs = &server->addrs;
|
||||
uint32_t i, j;
|
||||
|
||||
/* make a copy of the server's addrs, with most recent first */
|
||||
AcquireSRWLockShared(&saddrs->lock);
|
||||
j = saddrs->next_index;
|
||||
for (i = 0; i < saddrs->addrs.count; i++) {
|
||||
/* decrement/wrap j */
|
||||
j = (NFS41_ADDRS_PER_SERVER + j - 1) % NFS41_ADDRS_PER_SERVER;
|
||||
memcpy(&addrs->arr[i], &saddrs->addrs.arr[j], sizeof(netaddr4));
|
||||
}
|
||||
ReleaseSRWLockShared(&saddrs->lock);
|
||||
}
|
||||
|
||||
int nfs41_server_find_or_create(
|
||||
IN const char *server_owner_major_id,
|
||||
IN const char *server_scope,
|
||||
IN const netaddr4 *addr,
|
||||
OUT nfs41_server **server_out)
|
||||
{
|
||||
const struct server_info info = { server_scope, server_owner_major_id };
|
||||
struct list_entry *entry;
|
||||
nfs41_server *server;
|
||||
int status;
|
||||
|
||||
dprintf(SRVLVL, "--> nfs41_server_find_or_create(%s)\n", info.owner);
|
||||
|
||||
EnterCriticalSection(&g_server_list.lock);
|
||||
|
||||
/* search for an existing server */
|
||||
status = server_entry_find(&g_server_list, &info, &entry);
|
||||
if (status) {
|
||||
/* create a new server */
|
||||
status = server_create(&info, &server);
|
||||
if (status == NO_ERROR) {
|
||||
/* add it to the list */
|
||||
list_add_tail(&g_server_list.head, &server->entry);
|
||||
*server_out = server;
|
||||
|
||||
dprintf(SRVLVL, "<-- nfs41_server_find_or_create() "
|
||||
"returning new server %p\n", server);
|
||||
} else {
|
||||
dprintf(SRVLVL, "<-- nfs41_server_find_or_create() "
|
||||
"returning %d\n", status);
|
||||
}
|
||||
} else {
|
||||
server = server_entry(entry);
|
||||
|
||||
dprintf(SRVLVL, "<-- nfs41_server_find_or_create() "
|
||||
"returning existing server %p\n", server);
|
||||
}
|
||||
|
||||
if (server) {
|
||||
/* register the address used to connect */
|
||||
server_addrs_add(&server->addrs, addr);
|
||||
|
||||
server_ref_locked(server);
|
||||
}
|
||||
|
||||
*server_out = server;
|
||||
LeaveCriticalSection(&g_server_list.lock);
|
||||
return status;
|
||||
}
|
||||
|
||||
int nfs41_server_resolve(
|
||||
IN const char *hostname,
|
||||
IN unsigned short port,
|
||||
OUT multi_addr4 *addrs)
|
||||
{
|
||||
int status = ERROR_BAD_NET_NAME;
|
||||
char service[16];
|
||||
struct addrinfo hints, *res, *info;
|
||||
struct netconfig *nconf;
|
||||
struct netbuf addr;
|
||||
char *netid, *uaddr;
|
||||
|
||||
dprintf(SRVLVL, "--> nfs41_server_resolve(%s:%u)\n",
|
||||
hostname, port);
|
||||
|
||||
addrs->count = 0;
|
||||
|
||||
StringCchPrintfA(service, 16, "%u", port);
|
||||
|
||||
/* request a list of tcp addrs for the given hostname,port */
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
if (getaddrinfo(hostname, service, &hints, &res) != 0)
|
||||
goto out;
|
||||
|
||||
for (info = res; info != NULL; info = info->ai_next) {
|
||||
/* find the appropriate entry in /etc/netconfig */
|
||||
switch (info->ai_family) {
|
||||
case AF_INET: netid = "tcp"; break;
|
||||
case AF_INET6: netid = "tcp6"; break;
|
||||
default: continue;
|
||||
}
|
||||
|
||||
nconf = getnetconfigent(netid);
|
||||
if (nconf == NULL)
|
||||
continue;
|
||||
|
||||
/* convert to a transport-independent universal address */
|
||||
addr.buf = info->ai_addr;
|
||||
addr.maxlen = addr.len = (unsigned int)info->ai_addrlen;
|
||||
|
||||
uaddr = taddr2uaddr(nconf, &addr);
|
||||
freenetconfigent(nconf);
|
||||
|
||||
if (uaddr == NULL)
|
||||
continue;
|
||||
|
||||
StringCchCopyA(addrs->arr[addrs->count].netid,
|
||||
NFS41_NETWORK_ID_LEN+1, netid);
|
||||
StringCchCopyA(addrs->arr[addrs->count].uaddr,
|
||||
NFS41_UNIVERSAL_ADDR_LEN+1, uaddr);
|
||||
freeuaddr(uaddr);
|
||||
|
||||
status = NO_ERROR;
|
||||
if (++addrs->count >= NFS41_ADDRS_PER_SERVER)
|
||||
break;
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
out:
|
||||
if (status)
|
||||
dprintf(SRVLVL, "<-- nfs41_server_resolve(%s:%u) returning "
|
||||
"error %d\n", hostname, port, status);
|
||||
else
|
||||
dprintf(SRVLVL, "<-- nfs41_server_resolve(%s:%u) returning "
|
||||
"%s\n", hostname, port, addrs->arr[0].uaddr);
|
||||
return status;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue