10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6747Sga159272 * Common Development and Distribution License (the "License").
6*6747Sga159272 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6747Sga159272 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Simple nfs ops - open, close, read, and lseek.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <rpc/types.h>
310Sstevel@tonic-gate #include <rpc/auth.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include "clnt.h"
340Sstevel@tonic-gate #include <sys/fcntl.h>
350Sstevel@tonic-gate #include <sys/vfs.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/promif.h>
380Sstevel@tonic-gate #include <rpc/xdr.h>
390Sstevel@tonic-gate #include "nfs_inet.h"
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/bootvfs.h>
420Sstevel@tonic-gate #include <sys/bootdebug.h>
430Sstevel@tonic-gate #include <sys/salib.h>
440Sstevel@tonic-gate #include <sys/sacache.h>
450Sstevel@tonic-gate #include <rpc/rpc.h>
460Sstevel@tonic-gate #include "brpc.h"
470Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define dprintf if (boothowto & RB_DEBUG) printf
500Sstevel@tonic-gate
510Sstevel@tonic-gate static struct timeval zero_timeout = {0, 0}; /* default */
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * NFS Version 2 specific functions
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate ssize_t
nfsread(struct nfs_file * filep,char * buf,size_t size)580Sstevel@tonic-gate nfsread(struct nfs_file *filep, char *buf, size_t size)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate readargs read_args;
610Sstevel@tonic-gate readres read_res;
620Sstevel@tonic-gate enum clnt_stat read_stat;
630Sstevel@tonic-gate uint_t readcnt = 0; /* # bytes read by nfs */
640Sstevel@tonic-gate uint_t count = 0; /* # bytes transferred to buf */
650Sstevel@tonic-gate int done = FALSE; /* last block has come in */
660Sstevel@tonic-gate int framing_errs = 0; /* stack errors */
670Sstevel@tonic-gate char *buf_offset; /* current buffer offset */
680Sstevel@tonic-gate struct timeval timeout;
690Sstevel@tonic-gate static uint_t pos; /* progress indicator counter */
700Sstevel@tonic-gate static char ind[] = "|/-\\"; /* progress indicator */
710Sstevel@tonic-gate static int blks_read;
720Sstevel@tonic-gate
730Sstevel@tonic-gate read_args.file = filep->fh.fh2; /* structure copy */
740Sstevel@tonic-gate read_args.offset = filep->offset;
750Sstevel@tonic-gate buf_offset = buf;
760Sstevel@tonic-gate
770Sstevel@tonic-gate /* Optimize for reads of less than one block size */
780Sstevel@tonic-gate
790Sstevel@tonic-gate if (nfs_readsize == 0)
800Sstevel@tonic-gate nfs_readsize = READ_SIZE;
810Sstevel@tonic-gate
820Sstevel@tonic-gate if (size < nfs_readsize)
830Sstevel@tonic-gate read_args.count = size;
840Sstevel@tonic-gate else
850Sstevel@tonic-gate read_args.count = nfs_readsize;
860Sstevel@tonic-gate
870Sstevel@tonic-gate do {
880Sstevel@tonic-gate /* use the user's buffer to stuff the data into. */
890Sstevel@tonic-gate read_res.readres_u.reply.data.data_val = buf_offset;
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Handle the case where the file does not end
930Sstevel@tonic-gate * on a block boundary.
940Sstevel@tonic-gate */
950Sstevel@tonic-gate if ((count + read_args.count) > size)
960Sstevel@tonic-gate read_args.count = size - count;
970Sstevel@tonic-gate
980Sstevel@tonic-gate timeout.tv_sec = NFS_REXMIT_MIN; /* Total wait for call */
990Sstevel@tonic-gate timeout.tv_usec = 0;
1000Sstevel@tonic-gate do {
1010Sstevel@tonic-gate read_stat = CLNT_CALL(root_CLIENT, NFSPROC_READ,
1020Sstevel@tonic-gate xdr_readargs, (caddr_t)&read_args,
1030Sstevel@tonic-gate xdr_readres, (caddr_t)&read_res, timeout);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if (read_stat == RPC_TIMEDOUT) {
1060Sstevel@tonic-gate dprintf("NFS read(%d) timed out. Retrying...\n",
1070Sstevel@tonic-gate read_args.count);
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * If the remote is there and trying to respond,
1100Sstevel@tonic-gate * but our stack is having trouble reassembling
1110Sstevel@tonic-gate * the reply, reduce the read size in an
1120Sstevel@tonic-gate * attempt to compensate. Reset the
1130Sstevel@tonic-gate * transmission and reply wait timers.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate if (errno == ETIMEDOUT)
1160Sstevel@tonic-gate framing_errs++;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if (framing_errs > NFS_MAX_FERRS &&
1190Sstevel@tonic-gate read_args.count > NFS_READ_DECR) {
1200Sstevel@tonic-gate read_args.count -= NFS_READ_DECR;
1210Sstevel@tonic-gate nfs_readsize -= NFS_READ_DECR;
1220Sstevel@tonic-gate dprintf("NFS Read size now %d.\n",
1230Sstevel@tonic-gate nfs_readsize);
1240Sstevel@tonic-gate timeout.tv_sec = NFS_REXMIT_MIN;
1250Sstevel@tonic-gate framing_errs = 0;
1260Sstevel@tonic-gate } else {
1270Sstevel@tonic-gate if (timeout.tv_sec < NFS_REXMIT_MAX)
1280Sstevel@tonic-gate timeout.tv_sec++;
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate timeout.tv_sec = 0;
1310Sstevel@tonic-gate /* default RPC */
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate } while (read_stat == RPC_TIMEDOUT);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (read_stat != RPC_SUCCESS)
1370Sstevel@tonic-gate return (-1);
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate readcnt = read_res.readres_u.reply.data.data_len;
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * Handle the case where the file is simply empty, and
1420Sstevel@tonic-gate * nothing could be read.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate if (readcnt == 0)
1450Sstevel@tonic-gate break; /* eof */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * Handle the case where the file is smaller than
1490Sstevel@tonic-gate * the size of the read request, thus the request
1500Sstevel@tonic-gate * couldn't be completely filled.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate if (readcnt < read_args.count) {
1530Sstevel@tonic-gate #ifdef NFS_OPS_DEBUG
154*6747Sga159272 if ((boothowto & DBFLAGS) == DBFLAGS)
155*6747Sga159272 printf("nfsread(): partial read %d"
156*6747Sga159272 " instead of %d\n",
157*6747Sga159272 readcnt, read_args.count);
1580Sstevel@tonic-gate #endif
159*6747Sga159272 done = TRUE; /* update the counts and exit */
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /* update various offsets */
1630Sstevel@tonic-gate count += readcnt;
1640Sstevel@tonic-gate filep->offset += readcnt;
1650Sstevel@tonic-gate buf_offset += readcnt;
1660Sstevel@tonic-gate read_args.offset += readcnt;
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * round and round she goes (though not on every block..
1690Sstevel@tonic-gate * - OBP's take a fair bit of time to actually print stuff)
1700Sstevel@tonic-gate */
1710Sstevel@tonic-gate if ((blks_read++ & 0x3) == 0)
1720Sstevel@tonic-gate printf("%c\b", ind[pos++ & 3]);
1730Sstevel@tonic-gate } while (count < size && !done);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate return (count);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate static vtype_t nf_to_vt[] = {
1790Sstevel@tonic-gate VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate int
nfsgetattr(struct nfs_file * nfp,struct vattr * vap)1830Sstevel@tonic-gate nfsgetattr(struct nfs_file *nfp, struct vattr *vap)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate enum clnt_stat getattr_stat;
1860Sstevel@tonic-gate attrstat getattr_res;
1870Sstevel@tonic-gate fattr *na;
1880Sstevel@tonic-gate struct timeval timeout = {0, 0}; /* default */
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate getattr_stat = CLNT_CALL(root_CLIENT, NFSPROC_GETATTR,
191*6747Sga159272 xdr_nfs_fh, (caddr_t)&(nfp->fh.fh2),
192*6747Sga159272 xdr_attrstat, (caddr_t)&getattr_res, timeout);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (getattr_stat != RPC_SUCCESS) {
1950Sstevel@tonic-gate dprintf("nfs_getattr: RPC error %d\n", getattr_stat);
1960Sstevel@tonic-gate return (-1);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate if (getattr_res.status != NFS_OK) {
1990Sstevel@tonic-gate nfs_error(getattr_res.status);
2000Sstevel@tonic-gate return (getattr_res.status);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /* adapted from nattr_to_vattr() in nfs_client.c */
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate na = &getattr_res.attrstat_u.attributes;
2060Sstevel@tonic-gate if (vap->va_mask & AT_TYPE) {
2070Sstevel@tonic-gate if (na->type < NFNON || na->type > NFSOCK)
2080Sstevel@tonic-gate vap->va_type = VBAD;
2090Sstevel@tonic-gate else
2100Sstevel@tonic-gate vap->va_type = nf_to_vt[na->type];
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate if (vap->va_mask & AT_MODE)
2130Sstevel@tonic-gate vap->va_mode = na->mode;
2140Sstevel@tonic-gate if (vap->va_mask & AT_SIZE)
2150Sstevel@tonic-gate vap->va_size = na->size;
2160Sstevel@tonic-gate if (vap->va_mask & AT_NODEID)
2170Sstevel@tonic-gate vap->va_nodeid = na->fileid;
2180Sstevel@tonic-gate if (vap->va_mask & AT_ATIME) {
2190Sstevel@tonic-gate vap->va_atime.tv_sec = na->atime.seconds;
2200Sstevel@tonic-gate vap->va_atime.tv_nsec = na->atime.useconds * 1000;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate if (vap->va_mask & AT_CTIME) {
2230Sstevel@tonic-gate vap->va_ctime.tv_sec = na->ctime.seconds;
2240Sstevel@tonic-gate vap->va_ctime.tv_nsec = na->ctime.useconds * 1000;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate if (vap->va_mask & AT_MTIME) {
2270Sstevel@tonic-gate vap->va_mtime.tv_sec = na->mtime.seconds;
2280Sstevel@tonic-gate vap->va_mtime.tv_nsec = na->mtime.useconds * 1000;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate #ifdef NFS_OPS_DEBUG
2320Sstevel@tonic-gate if ((boothowto & DBFLAGS) == DBFLAGS)
2330Sstevel@tonic-gate printf("nfs_getattr(): done.\n");
2340Sstevel@tonic-gate #endif
2350Sstevel@tonic-gate return (getattr_res.status);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * Display nfs error messages.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate /*ARGSUSED*/
2420Sstevel@tonic-gate void
nfs_error(enum nfsstat status)2430Sstevel@tonic-gate nfs_error(enum nfsstat status)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate if (!(boothowto & RB_DEBUG))
2460Sstevel@tonic-gate return;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate switch (status) {
2490Sstevel@tonic-gate case NFSERR_PERM:
2500Sstevel@tonic-gate printf("NFS: Not owner.\n");
2510Sstevel@tonic-gate break;
2520Sstevel@tonic-gate case NFSERR_NOENT:
2530Sstevel@tonic-gate #ifdef NFS_OPS_DEBUG
2540Sstevel@tonic-gate printf("NFS: No such file or directory.\n");
2550Sstevel@tonic-gate #endif /* NFS_OPS_DEBUG */
2560Sstevel@tonic-gate break;
2570Sstevel@tonic-gate case NFSERR_IO:
2580Sstevel@tonic-gate printf("NFS: IO ERROR occurred on NFS server.\n");
2590Sstevel@tonic-gate break;
2600Sstevel@tonic-gate case NFSERR_NXIO:
2610Sstevel@tonic-gate printf("NFS: No such device or address.\n");
2620Sstevel@tonic-gate break;
2630Sstevel@tonic-gate case NFSERR_ACCES:
2640Sstevel@tonic-gate printf("NFS: Permission denied.\n");
2650Sstevel@tonic-gate break;
2660Sstevel@tonic-gate case NFSERR_EXIST:
2670Sstevel@tonic-gate printf("NFS: File exists.\n");
2680Sstevel@tonic-gate break;
2690Sstevel@tonic-gate case NFSERR_NODEV:
2700Sstevel@tonic-gate printf("NFS: No such device.\n");
2710Sstevel@tonic-gate break;
2720Sstevel@tonic-gate case NFSERR_NOTDIR:
2730Sstevel@tonic-gate printf("NFS: Not a directory.\n");
2740Sstevel@tonic-gate break;
2750Sstevel@tonic-gate case NFSERR_ISDIR:
2760Sstevel@tonic-gate printf("NFS: Is a directory.\n");
2770Sstevel@tonic-gate break;
2780Sstevel@tonic-gate case NFSERR_FBIG:
2790Sstevel@tonic-gate printf("NFS: File too large.\n");
2800Sstevel@tonic-gate break;
2810Sstevel@tonic-gate case NFSERR_NOSPC:
2820Sstevel@tonic-gate printf("NFS: No space left on device.\n");
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate case NFSERR_ROFS:
2850Sstevel@tonic-gate printf("NFS: Read-only filesystem.\n");
2860Sstevel@tonic-gate break;
2870Sstevel@tonic-gate case NFSERR_NAMETOOLONG:
2880Sstevel@tonic-gate printf("NFS: File name too long.\n");
2890Sstevel@tonic-gate break;
2900Sstevel@tonic-gate case NFSERR_NOTEMPTY:
2910Sstevel@tonic-gate printf("NFS: Directory not empty.\n");
2920Sstevel@tonic-gate break;
2930Sstevel@tonic-gate case NFSERR_DQUOT:
2940Sstevel@tonic-gate printf("NFS: Disk quota exceeded.\n");
2950Sstevel@tonic-gate break;
2960Sstevel@tonic-gate case NFSERR_STALE:
2970Sstevel@tonic-gate printf("NFS: Stale file handle.\n");
2980Sstevel@tonic-gate break;
2990Sstevel@tonic-gate case NFSERR_WFLUSH:
3000Sstevel@tonic-gate printf("NFS: server's write cache has been flushed.\n");
3010Sstevel@tonic-gate break;
3020Sstevel@tonic-gate default:
3030Sstevel@tonic-gate printf("NFS: unknown error.\n");
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate struct nfs_file *
nfslookup(struct nfs_file * dir,char * name,int * nstat)3090Sstevel@tonic-gate nfslookup(struct nfs_file *dir, char *name, int *nstat)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate static struct nfs_file cd;
3120Sstevel@tonic-gate diropargs dirop;
3130Sstevel@tonic-gate diropres res_lookup;
3140Sstevel@tonic-gate enum clnt_stat status;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate *nstat = (int)NFS_OK;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate bcopy(&dir->fh.fh2, &dirop.dir, NFS_FHSIZE);
3190Sstevel@tonic-gate dirop.name = name;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate status = CLNT_CALL(root_CLIENT, NFSPROC_LOOKUP, xdr_diropargs,
322*6747Sga159272 (caddr_t)&dirop, xdr_diropres, (caddr_t)&res_lookup,
323*6747Sga159272 zero_timeout);
3240Sstevel@tonic-gate if (status != RPC_SUCCESS) {
3250Sstevel@tonic-gate dprintf("lookup: RPC error.\n");
3260Sstevel@tonic-gate return (NULL);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate if (res_lookup.status != NFS_OK) {
3290Sstevel@tonic-gate nfs_error(res_lookup.status);
3300Sstevel@tonic-gate *nstat = (int)res_lookup.status;
3310Sstevel@tonic-gate return (NULL);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate bzero((caddr_t)&cd, sizeof (struct nfs_file));
3350Sstevel@tonic-gate cd.version = NFS_VERSION;
3360Sstevel@tonic-gate cd.ftype.type2 = res_lookup.diropres_u.diropres.attributes.type;
3370Sstevel@tonic-gate bcopy(&res_lookup.diropres_u.diropres.file, &cd.fh.fh2, NFS_FHSIZE);
3380Sstevel@tonic-gate return (&cd);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate * Gets symbolic link into pathname.
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate int
nfsgetsymlink(struct nfs_file * cfile,char ** path)3450Sstevel@tonic-gate nfsgetsymlink(struct nfs_file *cfile, char **path)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate enum clnt_stat status;
3480Sstevel@tonic-gate struct readlinkres linkres;
3490Sstevel@tonic-gate static char symlink_path[NFS_MAXPATHLEN];
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate * linkres needs a zeroed buffer to place path data into:
3530Sstevel@tonic-gate */
3540Sstevel@tonic-gate bzero(symlink_path, NFS_MAXPATHLEN);
3550Sstevel@tonic-gate linkres.readlinkres_u.data = &symlink_path[0];
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate status = CLNT_CALL(root_CLIENT, NFSPROC_READLINK,
358*6747Sga159272 xdr_nfs_fh, (caddr_t)&cfile->fh.fh2,
359*6747Sga159272 xdr_readlinkres, (caddr_t)&linkres, zero_timeout);
3600Sstevel@tonic-gate if (status != RPC_SUCCESS) {
3610Sstevel@tonic-gate dprintf("nfsgetsymlink: RPC call failed.\n");
3620Sstevel@tonic-gate return (-1);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate if (linkres.status != NFS_OK) {
3650Sstevel@tonic-gate nfs_error(linkres.status);
3660Sstevel@tonic-gate return (linkres.status);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate *path = linkres.readlinkres_u.data;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate return (NFS_OK);
3720Sstevel@tonic-gate }
373