1 /* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
4 * Copyright (c) 1993 John Brezak
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
53 #define NFSREAD_SIZE 1024
55 /* Define our own NFS attributes without NQNFS stuff. */
69 struct nfsv2_time fa_atime;
70 struct nfsv2_time fa_mtime;
71 struct nfsv2_time fa_ctime;
74 struct nfs_read_args {
75 u_char fh[NFS_FHSIZE];
78 n_long xxx; /* XXX what's this for? */
81 /* Data part of nfs rpc reply (also the largest thing we receive) */
82 struct nfs_read_repl {
84 struct nfsv2_fattrs fa;
86 u_char data[NFSREAD_SIZE];
90 struct nfs_readlnk_repl {
93 char path[NFS_MAXPATHLEN];
97 struct nfs_readdir_args {
98 u_char fh[NFS_FHSIZE];
103 struct nfs_readdir_data {
109 struct nfs_readdir_off {
115 struct iodesc *iodesc;
117 u_char fh[NFS_FHSIZE];
118 struct nfsv2_fattrs fa; /* all in network order */
120 #else /* !OLD_NFSV2 */
122 /* NFSv3 definitions */
123 #define NFS_V3MAXFHSIZE 64
125 #define RPCMNT_VER3 3
126 #define NFSPROCV3_LOOKUP 3
127 #define NFSPROCV3_READLINK 5
128 #define NFSPROCV3_READ 6
129 #define NFSPROCV3_READDIR 16
140 struct nfsv3_fattrs {
151 struct nfsv3_time fa_atime;
152 struct nfsv3_time fa_mtime;
153 struct nfsv3_time fa_ctime;
157 * For NFSv3, the file handle is variable in size, so most fixed sized
158 * structures for arguments won't work. For most cases, a structure
159 * that starts with any fixed size section is followed by an array
160 * that covers the maximum size required.
162 struct nfsv3_readdir_repl {
165 struct nfsv3_fattrs fa;
170 struct nfsv3_readdir_entry {
175 uint32_t nameplus[0];
179 struct iodesc *iodesc;
182 u_char fh[NFS_V3MAXFHSIZE];
183 struct nfsv3_fattrs fa; /* all in network order */
185 #endif /* OLD_NFSV2 */
188 * XXX interactions with tftp? See nfswrapper.c for a confusing
191 int nfs_open(const char *path, struct open_file *f);
192 static int nfs_close(struct open_file *f);
193 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
194 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
195 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
196 static int nfs_stat(struct open_file *f, struct stat *sb);
197 static int nfs_readdir(struct open_file *f, struct dirent *d);
199 struct nfs_iodesc nfs_root_node;
201 struct fs_ops nfs_fsops = {
214 * Fetch the root file handle (call mount daemon)
215 * Return zero or error number.
218 nfs_getrootfh(struct iodesc *d, char *path, u_char *fhp)
223 char path[FNAME_SIZE];
227 u_char fh[NFS_FHSIZE];
230 n_long h[RPC_HEADER_WORDS];
234 n_long h[RPC_HEADER_WORDS];
241 printf("nfs_getrootfh: %s\n", path);
247 bzero(args, sizeof(*args));
249 if (len > sizeof(args->path))
250 len = sizeof(args->path);
251 args->len = htonl(len);
252 bcopy(path, args->path, len);
253 len = 4 + roundup(len, 4);
255 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
256 args, len, repl, sizeof(*repl));
258 /* errno was set by rpc_call */
264 return (ntohl(repl->errno));
265 bcopy(repl->fh, fhp, sizeof(repl->fh));
270 * Lookup a file. Store handle and attributes.
271 * Return zero or error number.
274 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
278 u_char fh[NFS_FHSIZE];
280 char name[FNAME_SIZE];
284 u_char fh[NFS_FHSIZE];
285 struct nfsv2_fattrs fa;
288 n_long h[RPC_HEADER_WORDS];
292 n_long h[RPC_HEADER_WORDS];
299 printf("lookupfh: called\n");
305 bzero(args, sizeof(*args));
306 bcopy(d->fh, args->fh, sizeof(args->fh));
308 if (len > sizeof(args->name))
309 len = sizeof(args->name);
310 bcopy(name, args->name, len);
311 args->len = htonl(len);
312 len = 4 + roundup(len, 4);
315 rlen = sizeof(*repl);
317 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
318 args, len, repl, rlen);
320 return (errno); /* XXX - from rpc_call */
324 /* saerrno.h now matches NFS error numbers. */
325 return (ntohl(repl->errno));
327 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
328 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
332 #ifndef NFS_NOSYMLINK
334 * Get the destination of a symbolic link.
337 nfs_readlink(struct nfs_iodesc *d, char *buf)
340 n_long h[RPC_HEADER_WORDS];
341 u_char fh[NFS_FHSIZE];
344 n_long h[RPC_HEADER_WORDS];
345 struct nfs_readlnk_repl d;
351 printf("readlink: called\n");
354 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
355 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
356 sdata.fh, NFS_FHSIZE,
357 &rdata.d, sizeof(rdata.d));
365 return (ntohl(rdata.d.errno));
367 rdata.d.len = ntohl(rdata.d.len);
368 if (rdata.d.len > NFS_MAXPATHLEN)
369 return (ENAMETOOLONG);
371 bcopy(rdata.d.path, buf, rdata.d.len);
372 buf[rdata.d.len] = 0;
378 * Read data from a file.
379 * Return transfer count or -1 (and set errno)
382 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
384 struct nfs_read_args *args;
385 struct nfs_read_repl *repl;
387 n_long h[RPC_HEADER_WORDS];
388 struct nfs_read_args d;
391 n_long h[RPC_HEADER_WORDS];
392 struct nfs_read_repl d;
401 bcopy(d->fh, args->fh, NFS_FHSIZE);
402 args->off = htonl((n_long)off);
403 if (len > NFSREAD_SIZE)
405 args->len = htonl((n_long)len);
406 args->xxx = htonl((n_long)0);
407 hlen = sizeof(*repl) - NFSREAD_SIZE;
409 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
411 repl, sizeof(*repl));
413 /* errno was already set by rpc_call */
421 errno = ntohl(repl->errno);
425 x = ntohl(repl->count);
427 printf("nfsread: short packet, %d < %ld\n", rlen, x);
431 bcopy(repl->data, addr, x);
437 * return zero or error number
440 nfs_open(const char *upath, struct open_file *f)
443 struct nfs_iodesc *currfd;
444 char buf[2 * NFS_FHSIZE + 3];
448 #ifndef NFS_NOSYMLINK
449 struct nfs_iodesc *newfd;
450 struct nfsv2_fattrs *fa;
453 char namebuf[NFS_MAXPATHLEN + 1];
454 char linkbuf[NFS_MAXPATHLEN + 1];
462 printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
465 printf("no rootpath, no nfs\n");
470 * This is silly - we should look at dv_type but that value is
471 * arch dependant and we can't use it here.
474 if (strcmp(f->f_dev->dv_name, "net") != 0)
477 if (strcmp(f->f_dev->dv_name, "pxe") != 0)
481 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
484 /* Bind to a reserved port. */
485 desc->myport = htons(--rpc_port);
486 desc->destip = rootip;
487 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
489 nfs_root_node.iodesc = desc;
491 fh = &nfs_root_node.fh[0];
494 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
495 sprintf(cp, "%02x", fh[i]);
497 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
498 setenv("boot.nfsroot.path", rootpath, 1);
499 setenv("boot.nfsroot.nfshandle", buf, 1);
501 #ifndef NFS_NOSYMLINK
502 /* Fake up attributes for the root dir. */
503 fa = &nfs_root_node.fa;
504 fa->fa_type = htonl(NFDIR);
505 fa->fa_mode = htonl(0755);
506 fa->fa_nlink = htonl(2);
508 currfd = &nfs_root_node;
511 cp = path = strdup(upath);
518 * Remove extra separators
526 * Check that current node is a directory.
528 if (currfd->fa.fa_type != htonl(NFDIR)) {
533 /* allocate file system specific data structure */
534 newfd = malloc(sizeof(*newfd));
535 newfd->iodesc = currfd->iodesc;
539 * Get next component of path name.
545 while ((c = *cp) != '\0' && c != '/') {
546 if (++len > NFS_MAXNAMLEN) {
555 /* lookup a file handle */
556 error = nfs_lookupfh(currfd, ncp, newfd);
562 * Check for symbolic link
564 if (newfd->fa.fa_type == htonl(NFLNK)) {
567 error = nfs_readlink(newfd, linkbuf);
571 link_len = strlen(linkbuf);
574 if (link_len + len > MAXPATHLEN
575 || ++nlinks > MAXSYMLINKS) {
580 bcopy(cp, &namebuf[link_len], len + 1);
581 bcopy(linkbuf, namebuf, link_len);
584 * If absolute pathname, restart at root.
585 * If relative pathname, restart at parent directory.
589 if (currfd != &nfs_root_node)
591 currfd = &nfs_root_node;
600 if (currfd != &nfs_root_node)
614 /* allocate file system specific data structure */
615 currfd = malloc(sizeof(*currfd));
616 currfd->iodesc = desc;
619 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
622 f->f_fsdata = (void *)currfd;
628 printf("nfs_open: %s lookupfh failed: %s\n",
629 path, strerror(error));
631 #ifndef NFS_NOSYMLINK
632 if (currfd != &nfs_root_node)
640 nfs_close(struct open_file *f)
642 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
646 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
649 if (fp != &nfs_root_node && fp)
651 f->f_fsdata = (void *)0;
657 * read a portion of a file
660 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
662 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
668 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
671 while ((int)size > 0) {
673 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
674 /* XXX maybe should retry on certain errors */
678 printf("nfs_read: read: %s", strerror(errno));
680 return (errno); /* XXX - from nfs_readdata */
685 printf("nfs_read: hit EOF unexpectantly");
704 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
710 nfs_seek(struct open_file *f, off_t offset, int where)
712 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
713 n_long size = ntohl(d->fa.fa_size);
723 d->off = size - offset;
733 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
734 int nfs_stat_types[8] = {
735 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
738 nfs_stat(struct open_file *f, struct stat *sb)
740 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
743 ftype = ntohl(fp->fa.fa_type);
744 mode = ntohl(fp->fa.fa_mode);
745 mode |= nfs_stat_types[ftype & 7];
748 sb->st_nlink = ntohl(fp->fa.fa_nlink);
749 sb->st_uid = ntohl(fp->fa.fa_uid);
750 sb->st_gid = ntohl(fp->fa.fa_gid);
751 sb->st_size = ntohl(fp->fa.fa_size);
757 nfs_readdir(struct open_file *f, struct dirent *d)
759 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
760 struct nfs_readdir_args *args;
761 struct nfs_readdir_data *rd;
762 struct nfs_readdir_off *roff = NULL;
764 static n_long cookie = 0;
769 n_long h[RPC_HEADER_WORDS];
770 struct nfs_readdir_args d;
773 n_long h[RPC_HEADER_WORDS];
774 u_char d[NFS_READDIRSIZE];
780 bzero(args, sizeof(*args));
782 bcopy(fp->fh, args->fh, NFS_FHSIZE);
783 args->cookie = htonl(cookie);
784 args->count = htonl(NFS_READDIRSIZE);
786 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
788 rdata.d, sizeof(rdata.d));
790 roff = (struct nfs_readdir_off *)buf;
791 if (ntohl(roff->cookie) != 0)
794 roff = (struct nfs_readdir_off *)buf;
796 if (ntohl(roff->follows) == 0) {
797 eof = ntohl((roff+1)->cookie);
805 buf += sizeof(struct nfs_readdir_off);
806 rd = (struct nfs_readdir_data *)buf;
807 d->d_namlen = ntohl(rd->len);
808 bcopy(rd->name, d->d_name, d->d_namlen);
809 d->d_name[d->d_namlen] = '\0';
811 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
812 roff = (struct nfs_readdir_off *)buf;
813 cookie = ntohl(roff->cookie);
816 #else /* !OLD_NFSV2 */
818 * Fetch the root file handle (call mount daemon)
819 * Return zero or error number.
822 nfs_getrootfh(struct iodesc *d, char *path, uint32_t *fhlenp, u_char *fhp)
827 char path[FNAME_SIZE];
832 u_char fh[NFS_V3MAXFHSIZE];
837 uint32_t h[RPC_HEADER_WORDS];
841 uint32_t h[RPC_HEADER_WORDS];
848 printf("nfs_getrootfh: %s\n", path);
854 bzero(args, sizeof(*args));
856 if (len > sizeof(args->path))
857 len = sizeof(args->path);
858 args->len = htonl(len);
859 bcopy(path, args->path, len);
860 len = sizeof(uint32_t) + roundup(len, sizeof(uint32_t));
862 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER3, RPCMNT_MOUNT,
863 args, len, repl, sizeof(*repl));
865 /* errno was set by rpc_call */
867 if (cc < 2 * sizeof (uint32_t))
869 if (repl->errno != 0)
870 return (ntohl(repl->errno));
871 *fhlenp = ntohl(repl->fhsize);
872 bcopy(repl->fh, fhp, *fhlenp);
877 * Lookup a file. Store handle and attributes.
878 * Return zero or error number.
881 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
886 uint32_t fhplusname[1 +
887 (NFS_V3MAXFHSIZE + FNAME_SIZE) / sizeof(uint32_t)];
892 uint32_t fhplusattr[(NFS_V3MAXFHSIZE +
893 2 * (sizeof(uint32_t) +
894 sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
897 uint32_t h[RPC_HEADER_WORDS];
901 uint32_t h[RPC_HEADER_WORDS];
908 printf("lookupfh: called\n");
914 bzero(args, sizeof(*args));
915 args->fhsize = htonl(d->fhsize);
916 bcopy(d->fh, args->fhplusname, d->fhsize);
918 if (len > FNAME_SIZE)
920 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
921 args->fhplusname[pos++] = htonl(len);
922 bcopy(name, &args->fhplusname[pos], len);
923 len = sizeof(uint32_t) + pos * sizeof(uint32_t) +
924 roundup(len, sizeof(uint32_t));
926 rlen = sizeof(*repl);
928 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_LOOKUP,
929 args, len, repl, rlen);
931 return (errno); /* XXX - from rpc_call */
932 if (cc < 2 * sizeof(uint32_t))
934 if (repl->errno != 0)
935 /* saerrno.h now matches NFS error numbers. */
936 return (ntohl(repl->errno));
937 newfd->fhsize = ntohl(repl->fhsize);
938 bcopy(repl->fhplusattr, &newfd->fh, newfd->fhsize);
939 pos = roundup(newfd->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
940 if (repl->fhplusattr[pos++] == 0)
942 bcopy(&repl->fhplusattr[pos], &newfd->fa, sizeof(newfd->fa));
946 #ifndef NFS_NOSYMLINK
948 * Get the destination of a symbolic link.
951 nfs_readlink(struct nfs_iodesc *d, char *buf)
955 u_char fh[NFS_V3MAXFHSIZE];
960 struct nfsv3_fattrs fa;
962 u_char path[NFS_MAXPATHLEN];
965 uint32_t h[RPC_HEADER_WORDS];
969 uint32_t h[RPC_HEADER_WORDS];
976 printf("readlink: called\n");
982 bzero(args, sizeof(*args));
983 args->fhsize = htonl(d->fhsize);
984 bcopy(d->fh, args->fh, d->fhsize);
985 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READLINK,
986 args, sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
987 repl, sizeof(*repl));
991 if (cc < 2 * sizeof(uint32_t))
994 if (repl->errno != 0)
995 return (ntohl(repl->errno));
1000 repl->len = ntohl(repl->len);
1001 if (repl->len > NFS_MAXPATHLEN)
1002 return (ENAMETOOLONG);
1004 bcopy(repl->path, buf, repl->len);
1011 * Read data from a file.
1012 * Return transfer count or -1 (and set errno)
1015 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
1019 uint32_t fhoffcnt[NFS_V3MAXFHSIZE / sizeof(uint32_t) + 3];
1024 struct nfsv3_fattrs fa;
1028 u_char data[NFSREAD_SIZE];
1031 uint32_t h[RPC_HEADER_WORDS];
1035 uint32_t h[RPC_HEADER_WORDS];
1040 int hlen, rlen, pos;
1045 bzero(args, sizeof(*args));
1046 args->fhsize = htonl(d->fhsize);
1047 bcopy(d->fh, args->fhoffcnt, d->fhsize);
1048 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
1049 args->fhoffcnt[pos++] = 0;
1050 args->fhoffcnt[pos++] = htonl((uint32_t)off);
1051 if (len > NFSREAD_SIZE)
1053 args->fhoffcnt[pos] = htonl((uint32_t)len);
1054 hlen = sizeof(*repl) - NFSREAD_SIZE;
1056 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READ,
1057 args, 4 * sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
1058 repl, sizeof(*repl));
1060 /* errno was already set by rpc_call */
1066 if (repl->errno != 0) {
1067 errno = ntohl(repl->errno);
1071 x = ntohl(repl->count);
1073 printf("nfsread: short packet, %d < %ld\n", rlen, x);
1077 bcopy(repl->data, addr, x);
1083 * return zero or error number
1086 nfs_open(const char *upath, struct open_file *f)
1088 struct iodesc *desc;
1089 struct nfs_iodesc *currfd;
1090 char buf[2 * NFS_V3MAXFHSIZE + 3];
1094 #ifndef NFS_NOSYMLINK
1095 struct nfs_iodesc *newfd;
1096 struct nfsv3_fattrs *fa;
1099 char namebuf[NFS_MAXPATHLEN + 1];
1100 char linkbuf[NFS_MAXPATHLEN + 1];
1108 printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
1111 printf("no rootpath, no nfs\n");
1116 * This is silly - we should look at dv_type but that value is
1117 * arch dependant and we can't use it here.
1120 if (strcmp(f->f_dev->dv_name, "net") != 0)
1123 if (strcmp(f->f_dev->dv_name, "pxe") != 0)
1127 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
1130 /* Bind to a reserved port. */
1131 desc->myport = htons(--rpc_port);
1132 desc->destip = rootip;
1133 if ((error = nfs_getrootfh(desc, rootpath, &nfs_root_node.fhsize,
1136 nfs_root_node.iodesc = desc;
1138 fh = &nfs_root_node.fh[0];
1141 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
1142 sprintf(cp, "%02x", fh[i]);
1144 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
1145 setenv("boot.nfsroot.path", rootpath, 1);
1146 setenv("boot.nfsroot.nfshandle", buf, 1);
1147 sprintf(buf, "%d", nfs_root_node.fhsize);
1148 setenv("boot.nfsroot.nfshandlelen", buf, 1);
1150 #ifndef NFS_NOSYMLINK
1151 /* Fake up attributes for the root dir. */
1152 fa = &nfs_root_node.fa;
1153 fa->fa_type = htonl(NFDIR);
1154 fa->fa_mode = htonl(0755);
1155 fa->fa_nlink = htonl(2);
1157 currfd = &nfs_root_node;
1160 cp = path = strdup(upath);
1167 * Remove extra separators
1175 * Check that current node is a directory.
1177 if (currfd->fa.fa_type != htonl(NFDIR)) {
1182 /* allocate file system specific data structure */
1183 newfd = malloc(sizeof(*newfd));
1184 if (newfd == NULL) {
1188 newfd->iodesc = currfd->iodesc;
1192 * Get next component of path name.
1198 while ((c = *cp) != '\0' && c != '/') {
1199 if (++len > NFS_MAXNAMLEN) {
1208 /* lookup a file handle */
1209 error = nfs_lookupfh(currfd, ncp, newfd);
1215 * Check for symbolic link
1217 if (newfd->fa.fa_type == htonl(NFLNK)) {
1220 error = nfs_readlink(newfd, linkbuf);
1224 link_len = strlen(linkbuf);
1227 if (link_len + len > MAXPATHLEN
1228 || ++nlinks > MAXSYMLINKS) {
1233 bcopy(cp, &namebuf[link_len], len + 1);
1234 bcopy(linkbuf, namebuf, link_len);
1237 * If absolute pathname, restart at root.
1238 * If relative pathname, restart at parent directory.
1242 if (currfd != &nfs_root_node)
1244 currfd = &nfs_root_node;
1253 if (currfd != &nfs_root_node)
1265 /* allocate file system specific data structure */
1266 currfd = malloc(sizeof(*currfd));
1267 if (currfd != NULL) {
1268 currfd->iodesc = desc;
1271 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
1276 f->f_fsdata = (void *)currfd;
1282 printf("nfs_open: %s lookupfh failed: %s\n",
1283 path, strerror(error));
1285 #ifndef NFS_NOSYMLINK
1286 if (currfd != &nfs_root_node)
1294 nfs_close(struct open_file *f)
1296 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1300 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
1303 if (fp != &nfs_root_node && fp)
1305 f->f_fsdata = (void *)0;
1311 * read a portion of a file
1314 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
1316 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1322 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
1325 while ((int)size > 0) {
1327 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
1328 /* XXX maybe should retry on certain errors */
1332 printf("nfs_read: read: %s", strerror(errno));
1334 return (errno); /* XXX - from nfs_readdata */
1339 printf("nfs_read: hit EOF unexpectantly");
1358 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
1364 nfs_seek(struct open_file *f, off_t offset, int where)
1366 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
1367 uint32_t size = ntohl(d->fa.fa_size.val[1]);
1377 d->off = size - offset;
1387 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
1388 int nfs_stat_types[9] = {
1389 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, 0 };
1392 nfs_stat(struct open_file *f, struct stat *sb)
1394 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1395 uint32_t ftype, mode;
1397 ftype = ntohl(fp->fa.fa_type);
1398 mode = ntohl(fp->fa.fa_mode);
1399 mode |= nfs_stat_types[ftype & 7];
1402 sb->st_nlink = ntohl(fp->fa.fa_nlink);
1403 sb->st_uid = ntohl(fp->fa.fa_uid);
1404 sb->st_gid = ntohl(fp->fa.fa_gid);
1405 sb->st_size = ntohl(fp->fa.fa_size.val[1]);
1411 nfs_readdir(struct open_file *f, struct dirent *d)
1413 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1414 struct nfsv3_readdir_repl *repl;
1415 struct nfsv3_readdir_entry *rent;
1417 static uint32_t cookie0 = 0;
1418 static uint32_t cookie1 = 0;
1420 static uint32_t cookieverf0 = 0;
1421 static uint32_t cookieverf1 = 0;
1426 uint32_t fhpluscookie[5 + NFS_V3MAXFHSIZE];
1429 uint32_t h[RPC_HEADER_WORDS];
1433 uint32_t h[RPC_HEADER_WORDS];
1434 u_char d[NFS_READDIRSIZE];
1437 if (cookie0 == 0 && cookie1 == 0) {
1440 bzero(args, sizeof(*args));
1442 args->fhsize = htonl(fp->fhsize);
1443 bcopy(fp->fh, args->fhpluscookie, fp->fhsize);
1444 pos = roundup(fp->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
1445 args->fhpluscookie[pos++] = cookie0;
1446 args->fhpluscookie[pos++] = cookie1;
1447 args->fhpluscookie[pos++] = cookieverf0;
1448 args->fhpluscookie[pos++] = cookieverf1;
1449 args->fhpluscookie[pos] = htonl(NFS_READDIRSIZE);
1451 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READDIR,
1452 args, 6 * sizeof(uint32_t) +
1453 roundup(fp->fhsize, sizeof(uint32_t)),
1454 rdata.d, sizeof(rdata.d));
1456 repl = (struct nfsv3_readdir_repl *)buf;
1457 if (repl->errno != 0)
1458 return (ntohl(repl->errno));
1459 cookieverf0 = repl->cookiev0;
1460 cookieverf1 = repl->cookiev1;
1461 buf += sizeof (struct nfsv3_readdir_repl);
1463 rent = (struct nfsv3_readdir_entry *)buf;
1465 if (rent->follows == 0) {
1466 /* fid0 is actually eof */
1467 if (rent->fid0 != 0) {
1477 d->d_namlen = ntohl(rent->len);
1478 bcopy(rent->nameplus, d->d_name, d->d_namlen);
1479 d->d_name[d->d_namlen] = '\0';
1481 pos = roundup(d->d_namlen, sizeof(uint32_t)) / sizeof(uint32_t);
1482 cookie0 = rent->nameplus[pos++];
1483 cookie1 = rent->nameplus[pos++];
1484 buf = (u_char *)&rent->nameplus[pos];
1487 #endif /* OLD_NFSV2 */