/* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
/*-
* Copyright (c) 1993 John Brezak
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
__FBSDID("$FreeBSD$");
#include <string.h>
#include <netinet/in_systm.h>
#include "rpcv2.h"
#include "nfsv2.h"
#include "stand.h"
#include "net.h"
#include "netif.h"
#include "rpc.h"
#define NFS_DEBUGxx
/* Define our own NFS attributes without NQNFS stuff. */
#ifdef OLD_NFSV2
struct nfsv2_fattrs {
};
struct nfs_read_args {
};
/* Data part of nfs rpc reply (also the largest thing we receive) */
struct nfs_read_repl {
};
#ifndef NFS_NOSYMLINK
struct nfs_readlnk_repl {
};
#endif
struct nfs_readdir_args {
};
struct nfs_readdir_data {
char name[0];
};
struct nfs_readdir_off {
};
struct nfs_iodesc {
};
#else /* !OLD_NFSV2 */
/* NFSv3 definitions */
typedef struct {
} n_quad;
struct nfsv3_time {
};
struct nfsv3_fattrs {
};
/*
* For NFSv3, the file handle is variable in size, so most fixed sized
* structures for arguments won't work. For most cases, a structure
* that starts with any fixed size section is followed by an array
* that covers the maximum size required.
*/
struct nfsv3_readdir_repl {
};
struct nfsv3_readdir_entry {
};
struct nfs_iodesc {
};
#endif /* OLD_NFSV2 */
/*
* XXX interactions with tftp? See nfswrapper.c for a confusing
* issue.
*/
"nfs",
};
#ifdef OLD_NFSV2
/*
* Fetch the root file handle (call mount daemon)
* Return zero or error number.
*/
int
{
int len;
struct args {
} *args;
struct repl {
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
#endif
if (cc == -1) {
/* errno was set by rpc_call */
return (errno);
}
if (cc < 4)
return (EBADRPC);
return (0);
}
/*
* Lookup a file. Store handle and attributes.
* Return zero or error number.
*/
int
{
struct args {
} *args;
struct repl {
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
printf("lookupfh: called\n");
#endif
len += NFS_FHSIZE;
if (cc == -1)
return (errno); /* XXX - from rpc_call */
if (cc < 4)
return (EIO);
/* saerrno.h now matches NFS error numbers. */
}
return (0);
}
#ifndef NFS_NOSYMLINK
/*
* Get the destination of a symbolic link.
*/
int
{
struct {
} sdata;
struct {
struct nfs_readlnk_repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
printf("readlink: called\n");
#endif
if (cc == -1)
return (errno);
if (cc < 4)
return (EIO);
return (ENAMETOOLONG);
return (0);
}
#endif
/*
* Read data from a file.
* Return transfer count or -1 (and set errno)
*/
{
struct {
struct nfs_read_args d;
} sdata;
struct {
struct nfs_read_repl d;
} rdata;
long x;
if (len > NFSREAD_SIZE)
len = NFSREAD_SIZE;
if (cc == -1) {
/* errno was already set by rpc_call */
return (-1);
}
return (-1);
}
return (-1);
}
if (rlen < x) {
return(-1);
}
return (x);
}
/*
* Open a file.
* return zero or error number
*/
int
{
char *cp;
int i;
#ifndef NFS_NOSYMLINK
char *ncp;
int c;
int nlinks = 0;
#endif
int error;
char *path;
#ifdef NFS_DEBUG
if (debug)
#endif
if (!rootpath[0]) {
printf("no rootpath, no nfs\n");
return (ENXIO);
}
/*
* This is silly - we should look at dv_type but that value is
* arch dependant and we can't use it here.
*/
#ifndef __i386__
return(EINVAL);
#else
return(EINVAL);
#endif
return(EINVAL);
/* Bind to a reserved port. */
return (error);
buf[0] = 'X';
/* Allocate file system specific data structure */
goto out;
}
#ifndef NFS_NOSYMLINK
goto out;
}
while (*cp) {
/*
* Remove extra separators
*/
while (*cp == '/')
cp++;
if (*cp == '\0')
break;
/*
* Check that current node is a directory.
*/
goto out;
}
/* allocate file system specific data structure */
/*
* Get next component of path name.
*/
{
int len = 0;
if (++len > NFS_MAXNAMLEN) {
goto out;
}
cp++;
}
*cp = '\0';
}
/* lookup a file handle */
*cp = c;
if (error)
goto out;
/*
* Check for symbolic link
*/
if (error)
goto out;
|| ++nlinks > MAXSYMLINKS) {
goto out;
}
/*
* If absolute pathname, restart at root.
* If relative pathname, restart at parent directory.
*/
if (*cp == '/')
continue;
}
}
error = 0;
out:
#else
#endif
if (!error) {
return (0);
}
#ifdef NFS_DEBUG
if (debug)
printf("nfs_open: %s lookupfh failed: %s\n",
#endif
return (error);
}
int
{
#ifdef NFS_DEBUG
if (debug)
#endif
if (fp)
f->f_fsdata = (void *)0;
return (0);
}
/*
* read a portion of a file
*/
int
{
#ifdef NFS_DEBUG
if (debug)
#endif
while ((int)size > 0) {
twiddle(16);
/* XXX maybe should retry on certain errors */
if (cc == -1) {
#ifdef NFS_DEBUG
if (debug)
#endif
return (errno); /* XXX - from nfs_readdata */
}
if (cc == 0) {
#ifdef NFS_DEBUG
if (debug)
printf("nfs_read: hit EOF unexpectantly");
#endif
goto ret;
}
}
ret:
if (resid)
return (0);
}
/*
* Not implemented.
*/
int
{
return (EROFS);
}
{
switch (where) {
case SEEK_SET:
break;
case SEEK_CUR:
break;
case SEEK_END:
break;
default:
return (-1);
}
return (d->off);
}
/* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
int
{
return (0);
}
static int
{
static char *buf;
struct {
struct nfs_readdir_args d;
} sdata;
static struct {
} rdata;
return EIO;
}
if (eof) {
cookie = 0;
return ENOENT;
}
goto refill;
}
buf += sizeof(struct nfs_readdir_off);
return 0;
}
#else /* !OLD_NFSV2 */
/*
* Fetch the root file handle (call mount daemon)
* Return zero or error number.
*/
int
{
int len;
struct args {
} *args;
struct repl {
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
#endif
if (cc == -1)
/* errno was set by rpc_call */
return (errno);
return (EBADRPC);
return (0);
}
/*
* Lookup a file. Store handle and attributes.
* Return zero or error number.
*/
int
{
struct args {
} *args;
struct repl {
2 * (sizeof(uint32_t) +
sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
printf("lookupfh: called\n");
#endif
if (len > FNAME_SIZE)
len = FNAME_SIZE;
if (cc == -1)
return (errno); /* XXX - from rpc_call */
return (EIO);
/* saerrno.h now matches NFS error numbers. */
return (EIO);
return (0);
}
#ifndef NFS_NOSYMLINK
/*
* Get the destination of a symbolic link.
*/
int
{
struct args {
} *args;
struct repl {
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
#ifdef NFS_DEBUG
if (debug)
printf("readlink: called\n");
#endif
if (cc == -1)
return (errno);
return (EIO);
return (EIO);
return (ENAMETOOLONG);
return (0);
}
#endif
/*
* Read data from a file.
* Return transfer count or -1 (and set errno)
*/
{
struct args {
} *args;
struct repl {
} *repl;
struct {
struct args d;
} sdata;
struct {
struct repl d;
} rdata;
long x;
if (len > NFSREAD_SIZE)
len = NFSREAD_SIZE;
if (cc == -1)
/* errno was already set by rpc_call */
return (-1);
return (-1);
}
return (-1);
}
if (rlen < x) {
return (-1);
}
return (x);
}
/*
* Open a file.
* return zero or error number
*/
int
{
char *cp;
int i;
#ifndef NFS_NOSYMLINK
char *ncp;
int c;
int nlinks = 0;
#endif
int error;
char *path;
#ifdef NFS_DEBUG
if (debug)
#endif
if (!rootpath[0]) {
printf("no rootpath, no nfs\n");
return (ENXIO);
}
/*
* This is silly - we should look at dv_type but that value is
* arch dependant and we can't use it here.
*/
#ifndef __i386__
return (EINVAL);
#else
return (EINVAL);
#endif
return (EINVAL);
/* Bind to a reserved port. */
nfs_root_node.fh)))
return (error);
buf[0] = 'X';
/* Allocate file system specific data structure */
goto out;
}
#ifndef NFS_NOSYMLINK
goto out;
}
while (*cp) {
/*
* Remove extra separators
*/
while (*cp == '/')
cp++;
if (*cp == '\0')
break;
/*
* Check that current node is a directory.
*/
goto out;
}
/* allocate file system specific data structure */
goto out;
}
/*
* Get next component of path name.
*/
{
int len = 0;
if (++len > NFS_MAXNAMLEN) {
goto out;
}
cp++;
}
*cp = '\0';
}
/* lookup a file handle */
*cp = c;
if (error)
goto out;
/*
* Check for symbolic link
*/
if (error)
goto out;
|| ++nlinks > MAXSYMLINKS) {
goto out;
}
/*
* If absolute pathname, restart at root.
* If relative pathname, restart at parent directory.
*/
if (*cp == '/')
continue;
}
}
error = 0;
out:
#else
#endif
if (!error) {
return (0);
}
#ifdef NFS_DEBUG
if (debug)
printf("nfs_open: %s lookupfh failed: %s\n",
#endif
return (error);
}
int
{
#ifdef NFS_DEBUG
if (debug)
#endif
if (fp)
f->f_fsdata = (void *)0;
return (0);
}
/*
* read a portion of a file
*/
int
{
#ifdef NFS_DEBUG
if (debug)
#endif
while ((int)size > 0) {
twiddle(16);
/* XXX maybe should retry on certain errors */
if (cc == -1) {
#ifdef NFS_DEBUG
if (debug)
#endif
return (errno); /* XXX - from nfs_readdata */
}
if (cc == 0) {
#ifdef NFS_DEBUG
if (debug)
printf("nfs_read: hit EOF unexpectantly");
#endif
goto ret;
}
}
ret:
if (resid)
return (0);
}
/*
* Not implemented.
*/
int
{
return (EROFS);
}
{
switch (where) {
case SEEK_SET:
break;
case SEEK_CUR:
break;
case SEEK_END:
break;
default:
return (-1);
}
return (d->off);
}
/* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
int
{
return (0);
}
static int
{
static char *buf;
int pos;
struct args {
} *args;
struct {
struct args d;
} sdata;
static struct {
} rdata;
buf += sizeof (struct nfsv3_readdir_repl);
}
/* fid0 is actually eof */
cookie = 0;
return (ENOENT);
}
goto refill;
}
pos += 2;
return (0);
}
#endif /* OLD_NFSV2 */