199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1993 John Brezak
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. The name of the author may not be used to endorse or promote products
199767f8919635c4928607450d9e0abb932109ceToomas Soome * derived from this software without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
199767f8919635c4928607450d9e0abb932109ceToomas Soome * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/time.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/socket.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpcv2.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "nfsv2.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "netif.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpc.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFS_DEBUGxx
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFSREAD_SIZE 1024
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Define our own NFS attributes without NQNFS stuff. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef OLD_NFSV2
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfsv2_fattrs {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_nlink;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_blocksize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_rdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_blocks;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_fsid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fa_fileid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_time fa_atime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_time fa_mtime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_time fa_ctime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_read_args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long xxx; /* XXX what's this for? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Data part of nfs rpc reply (also the largest thing we receive) */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_read_repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_fattrs fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char data[NFSREAD_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_readlnk_repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char path[NFS_MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_readdir_args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long cookie;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_readdir_data {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long fileid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char name[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_readdir_off {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long cookie;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long follows;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_iodesc {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_fattrs fa; /* all in network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* !OLD_NFSV2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* NFSv3 definitions */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFS_V3MAXFHSIZE 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFS_VER3 3
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RPCMNT_VER3 3
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFSPROCV3_LOOKUP 3
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFSPROCV3_READLINK 5
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFSPROCV3_READ 6
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFSPROCV3_READDIR 16
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t val[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome} n_quad;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfsv3_time {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t nfs_sec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t nfs_nsec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfsv3_fattrs {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fa_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fa_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fa_nlink;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fa_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fa_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_quad fa_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_quad fa_used;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_quad fa_rdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_quad fa_fsid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_quad fa_fileid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_time fa_atime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_time fa_mtime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_time fa_ctime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For NFSv3, the file handle is variable in size, so most fixed sized
199767f8919635c4928607450d9e0abb932109ceToomas Soome * structures for arguments won't work. For most cases, a structure
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that starts with any fixed size section is followed by an array
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that covers the maximum size required.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfsv3_readdir_repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t ok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_fattrs fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t cookiev0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t cookiev1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfsv3_readdir_entry {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t follows;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fid0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fid1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t nameplus[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_iodesc {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_V3MAXFHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_fattrs fa; /* all in network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t cookie;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* OLD_NFSV2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX interactions with tftp? See nfswrapper.c for a confusing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * issue.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nfs_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int nfs_close(struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t nfs_seek(struct open_file *f, off_t offset, int where);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int nfs_stat(struct open_file *f, struct stat *sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int nfs_readdir(struct open_file *f, struct dirent *d);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_iodesc nfs_root_node;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct fs_ops nfs_fsops = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "nfs",
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_read,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_write,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_seek,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_stat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_readdir
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef OLD_NFSV2
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Fetch the root file handle (call mount daemon)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return zero or error number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_getrootfh(struct iodesc *d, char *path, u_char *fhp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char path[FNAME_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_getrootfh: %s\n", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > sizeof(args->path))
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(args->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->len = htonl(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(path, args->path, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = 4 + roundup(len, 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, len, repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* errno was set by rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EBADRPC);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->fh, fhp, sizeof(repl->fh));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a file. Store handle and attributes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return zero or error number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, rlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char name[FNAME_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_fattrs fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("lookupfh: called\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, args->fh, sizeof(args->fh));
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > sizeof(args->name))
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(args->name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(name, args->name, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->len = htonl(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = 4 + roundup(len, 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len += NFS_FHSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = sizeof(*repl);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, len, repl, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno); /* XXX - from rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* saerrno.h now matches NFS error numbers. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the destination of a symbolic link.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readlink(struct nfs_iodesc *d, char *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_readlnk_repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("readlink: called\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, sdata.fh, NFS_FHSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sdata.fh, NFS_FHSIZE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &rdata.d, sizeof(rdata.d));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rdata.d.errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(rdata.d.errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rdata.d.len = ntohl(rdata.d.len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rdata.d.len > NFS_MAXPATHLEN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENAMETOOLONG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(rdata.d.path, buf, rdata.d.len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[rdata.d.len] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read data from a file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return transfer count or -1 (and set errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_read_args *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_read_repl *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_read_args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_read_repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome long x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int hlen, rlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, args->fh, NFS_FHSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->off = htonl((n_long)off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > NFSREAD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = NFSREAD_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->len = htonl((n_long)len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->xxx = htonl((n_long)0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome hlen = sizeof(*repl) - NFSREAD_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, sizeof(*args),
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* errno was already set by rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < hlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = ntohl(repl->errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = cc - hlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(repl->count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rlen < x) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfsread: short packet, %d < %ld\n", rlen, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->data, addr, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open a file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return zero or error number
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_open(const char *upath, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *currfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[2 * NFS_FHSIZE + 3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *fh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *newfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv2_fattrs *fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *ncp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char namebuf[NFS_MAXPATHLEN + 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char linkbuf[NFS_MAXPATHLEN + 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nlinks = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!rootpath[0]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("no rootpath, no nfs\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is silly - we should look at dv_type but that value is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * arch dependant and we can't use it here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef __i386__
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "net") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "pxe") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(desc = socktodesc(*(int *)(f->f_devdata))))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Bind to a reserved port. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->destip = rootip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_type = htonl(NFDIR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_mode = htonl(0755);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_nlink = htonl(2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.iodesc = desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fh = &nfs_root_node.fh[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = 'X';
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = &buf[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "%02x", fh[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "X");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.path", rootpath, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandle", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate file system specific data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd = malloc(sizeof(*newfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currfd == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&nfs_root_node, currfd, sizeof(*currfd));
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = path = strdup(upath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remove extra separators
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check that current node is a directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currfd->fa.fa_type != htonl(NFDIR)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOTDIR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* allocate file system specific data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome newfd = malloc(sizeof(*newfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome newfd->iodesc = currfd->iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get next component of path name.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ncp = cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = *cp) != '\0' && c != '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (++len > NFS_MAXNAMLEN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* lookup a file handle */
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_lookupfh(currfd, ncp, newfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check for symbolic link
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (newfd->fa.fa_type == htonl(NFLNK)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int link_len, len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_readlink(newfd, linkbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome link_len = strlen(linkbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (link_len + len > MAXPATHLEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome || ++nlinks > MAXSYMLINKS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(cp, &namebuf[link_len], len + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(linkbuf, namebuf, link_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If absolute pathname, restart at root.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If relative pathname, restart at parent directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = namebuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&nfs_root_node, currfd, sizeof(*currfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(newfd);
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd = newfd;
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(newfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd->iodesc = desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_lookupfh(&nfs_root_node, upath, currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!error) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd->off = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)currfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_open: %s lookupfh failed: %s\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome path, strerror(error));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_close: fp=0x%lx\n", (u_long)fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * read a portion of a file
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *addr = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: size=%lu off=%d\n", (u_long)size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (int)fp->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((int)size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(16);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = nfs_readdata(fp, fp->off, (void *)addr, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX maybe should retry on certain errors */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: read: %s", strerror(errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno); /* XXX - from nfs_readdata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: hit EOF unexpectantly");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->off += cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeret:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Not implemented.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EROFS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeoff_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long size = ntohl(d->fa.fa_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (where) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_SET:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off = offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_CUR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_END:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off = size - offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EINVAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (d->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nfs_stat_types[8] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_stat(struct open_file *f, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long ftype, mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ftype = ntohl(fp->fa.fa_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome mode = ntohl(fp->fa.fa_mode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome mode |= nfs_stat_types[ftype & 7];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_nlink = ntohl(fp->fa.fa_nlink);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = ntohl(fp->fa.fa_uid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = ntohl(fp->fa.fa_gid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = ntohl(fp->fa.fa_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readdir(struct open_file *f, struct dirent *d)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_readdir_args *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_readdir_data *rd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_readdir_off *roff = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static struct nfs_iodesc *pfp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static n_long cookie = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long eof;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_readdir_args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char d[NFS_READDIRSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp != pfp || fp->off != cookie) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pfp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refill:
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(fp->fh, args->fh, NFS_FHSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->cookie = htonl(fp->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->count = htonl(NFS_READDIRSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, sizeof(*args),
199767f8919635c4928607450d9e0abb932109ceToomas Soome rdata.d, sizeof(rdata.d));
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome roff = (struct nfs_readdir_off *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ntohl(roff->cookie) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pfp = fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cookie = fp->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome roff = (struct nfs_readdir_off *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ntohl(roff->follows) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome eof = ntohl((roff+1)->cookie);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (eof) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome cookie = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto refill;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof(struct nfs_readdir_off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rd = (struct nfs_readdir_data *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_namlen = ntohl(rd->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(rd->name, d->d_name, d->d_namlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_name[d->d_namlen] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
199767f8919635c4928607450d9e0abb932109ceToomas Soome roff = (struct nfs_readdir_off *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->off = cookie = ntohl(roff->cookie);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* !OLD_NFSV2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Fetch the root file handle (call mount daemon)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return zero or error number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_getrootfh(struct iodesc *d, char *path, uint32_t *fhlenp, u_char *fhp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char path[FNAME_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_V3MAXFHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t authcnt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t auth[7];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_getrootfh: %s\n", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > sizeof(args->path))
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(args->path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->len = htonl(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(path, args->path, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(uint32_t) + roundup(len, sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER3, RPCMNT_MOUNT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, len, repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* errno was set by rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 2 * sizeof (uint32_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EBADRPC);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome *fhlenp = ntohl(repl->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->fh, fhp, *fhlenp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Lookup a file. Store handle and attributes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return zero or error number.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, rlen, pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhplusname[1 +
199767f8919635c4928607450d9e0abb932109ceToomas Soome (NFS_V3MAXFHSIZE + FNAME_SIZE) / sizeof(uint32_t)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhplusattr[(NFS_V3MAXFHSIZE +
199767f8919635c4928607450d9e0abb932109ceToomas Soome 2 * (sizeof(uint32_t) +
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("lookupfh: called\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhsize = htonl(d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, args->fhplusname, d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > FNAME_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = FNAME_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhplusname[pos++] = htonl(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(name, &args->fhplusname[pos], len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = sizeof(uint32_t) + pos * sizeof(uint32_t) +
199767f8919635c4928607450d9e0abb932109ceToomas Soome roundup(len, sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = sizeof(*repl);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_LOOKUP,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, len, repl, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno); /* XXX - from rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 2 * sizeof(uint32_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* saerrno.h now matches NFS error numbers. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome newfd->fhsize = ntohl(repl->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->fhplusattr, &newfd->fh, newfd->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos = roundup(newfd->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->fhplusattr[pos++] == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&repl->fhplusattr[pos], &newfd->fa, sizeof(newfd->fa));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the destination of a symbolic link.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readlink(struct nfs_iodesc *d, char *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_V3MAXFHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t ok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_fattrs fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char path[NFS_MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("readlink: called\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhsize = htonl(d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, args->fh, d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READLINK,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < 2 * sizeof(uint32_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->ok == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl->len = ntohl(repl->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->len > NFS_MAXPATHLEN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENAMETOOLONG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->path, buf, repl->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[repl->len] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read data from a file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return transfer count or -1 (and set errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhoffcnt[NFS_V3MAXFHSIZE / sizeof(uint32_t) + 3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t ok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_fattrs fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t eof;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char data[NFSREAD_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome long x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int hlen, rlen, pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhsize = htonl(d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(d->fh, args->fhoffcnt, d->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhoffcnt[pos++] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhoffcnt[pos++] = htonl((uint32_t)off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > NFSREAD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = NFSREAD_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhoffcnt[pos] = htonl((uint32_t)len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome hlen = sizeof(*repl) - NFSREAD_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READ,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, 4 * sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* errno was already set by rpc_call */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < hlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = ntohl(repl->errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = cc - hlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(repl->count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rlen < x) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfsread: short packet, %d < %ld\n", rlen, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(repl->data, addr, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open a file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return zero or error number
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_open(const char *upath, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *currfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[2 * NFS_V3MAXFHSIZE + 3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *fh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *newfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_fattrs *fa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *ncp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char namebuf[NFS_MAXPATHLEN + 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char linkbuf[NFS_MAXPATHLEN + 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nlinks = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!rootpath[0]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("no rootpath, no nfs\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is silly - we should look at dv_type but that value is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * arch dependant and we can't use it here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef __i386__
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "net") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(f->f_dev->dv_name, "pxe") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(desc = socktodesc(*(int *)(f->f_devdata))))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Bind to a reserved port. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->destip = rootip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = nfs_getrootfh(desc, rootpath, &nfs_root_node.fhsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fh)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_type = htonl(NFDIR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_mode = htonl(0755);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fa.fa_nlink = htonl(2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.iodesc = desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fh = &nfs_root_node.fh[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = 'X';
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = &buf[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "%02x", fh[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "X");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.path", rootpath, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandle", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", nfs_root_node.fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandlelen", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate file system specific data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd = malloc(sizeof(*newfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currfd == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef NFS_NOSYMLINK
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&nfs_root_node, currfd, sizeof(*currfd));
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = path = strdup(upath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remove extra separators
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check that current node is a directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currfd->fa.fa_type != htonl(NFDIR)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOTDIR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* allocate file system specific data structure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome newfd = malloc(sizeof(*newfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (newfd == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome newfd->iodesc = currfd->iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get next component of path name.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ncp = cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = *cp) != '\0' && c != '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (++len > NFS_MAXNAMLEN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* lookup a file handle */
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_lookupfh(currfd, ncp, newfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check for symbolic link
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (newfd->fa.fa_type == htonl(NFLNK)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int link_len, len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_readlink(newfd, linkbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome link_len = strlen(linkbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (link_len + len > MAXPATHLEN
199767f8919635c4928607450d9e0abb932109ceToomas Soome || ++nlinks > MAXSYMLINKS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(cp, &namebuf[link_len], len + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(linkbuf, namebuf, link_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If absolute pathname, restart at root.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If relative pathname, restart at parent directory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = namebuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&nfs_root_node, currfd, sizeof(*currfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(newfd);
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd = newfd;
00b631909040bb219a21436e55f74fc367c62176Toomas Soome newfd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(newfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd->iodesc = desc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = nfs_lookupfh(&nfs_root_node, upath, currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!error) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd->off = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome currfd->cookie = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)currfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_open: %s lookupfh failed: %s\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome path, strerror(error));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(currfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_close: fp=0x%lx\n", (u_long)fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = (void *)0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * read a portion of a file
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *addr = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: size=%lu off=%d\n", (u_long)size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (int)fp->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((int)size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome twiddle(16);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = nfs_readdata(fp, fp->off, (void *)addr, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX maybe should retry on certain errors */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: read: %s", strerror(errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno); /* XXX - from nfs_readdata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef NFS_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("nfs_read: hit EOF unexpectantly");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->off += cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size -= cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeret:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Not implemented.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EROFS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeoff_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t size = ntohl(d->fa.fa_size.val[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (where) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_SET:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off = offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_CUR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off += offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_END:
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->off = size - offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EINVAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (d->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nfs_stat_types[9] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, 0 };
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_stat(struct open_file *f, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t ftype, mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ftype = ntohl(fp->fa.fa_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome mode = ntohl(fp->fa.fa_mode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome mode |= nfs_stat_types[ftype & 7];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_mode = mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_nlink = ntohl(fp->fa.fa_nlink);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_uid = ntohl(fp->fa.fa_uid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_gid = ntohl(fp->fa.fa_gid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = ntohl(fp->fa.fa_size.val[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenfs_readdir(struct open_file *f, struct dirent *d)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_readdir_repl *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct nfsv3_readdir_entry *rent;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static struct nfs_iodesc *pfp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static uint64_t cookie = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhpluscookie[5 + NFS_V3MAXFHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char d[NFS_READDIRSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp != pfp || fp->off != cookie) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pfp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refill:
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(args, sizeof(*args));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhsize = htonl(fp->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(fp->fh, args->fhpluscookie, fp->fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos = roundup(fp->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhpluscookie[pos++] = htonl(fp->off >> 32);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhpluscookie[pos++] = htonl(fp->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhpluscookie[pos++] = htonl(fp->cookie >> 32);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhpluscookie[pos++] = htonl(fp->cookie);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->fhpluscookie[pos] = htonl(NFS_READDIRSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READDIR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, 6 * sizeof(uint32_t) +
199767f8919635c4928607450d9e0abb932109ceToomas Soome roundup(fp->fhsize, sizeof(uint32_t)),
199767f8919635c4928607450d9e0abb932109ceToomas Soome rdata.d, sizeof(rdata.d));
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = (struct nfsv3_readdir_repl *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (repl->errno != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ntohl(repl->errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pfp = fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cookie = fp->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->cookie = ((uint64_t)ntohl(repl->cookiev0) << 32) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome ntohl(repl->cookiev1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof (struct nfsv3_readdir_repl);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rent = (struct nfsv3_readdir_entry *)buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rent->follows == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* fid0 is actually eof */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rent->fid0 != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome cookie = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto refill;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_namlen = ntohl(rent->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(rent->nameplus, d->d_name, d->d_namlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_name[d->d_namlen] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos = roundup(d->d_namlen, sizeof(uint32_t)) / sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->off = cookie = ((uint64_t)ntohl(rent->nameplus[pos]) << 32) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome ntohl(rent->nameplus[pos + 1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos += 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = (u_char *)&rent->nameplus[pos];
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* OLD_NFSV2 */