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(d, path, fhp)
226 char path[FNAME_SIZE];
230 u_char fh[NFS_FHSIZE];
233 n_long h[RPC_HEADER_WORDS];
237 n_long h[RPC_HEADER_WORDS];
244 printf("nfs_getrootfh: %s\n", path);
250 bzero(args, sizeof(*args));
252 if (len > sizeof(args->path))
253 len = sizeof(args->path);
254 args->len = htonl(len);
255 bcopy(path, args->path, len);
256 len = 4 + roundup(len, 4);
258 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
259 args, len, repl, sizeof(*repl));
261 /* errno was set by rpc_call */
267 return (ntohl(repl->errno));
268 bcopy(repl->fh, fhp, sizeof(repl->fh));
273 * Lookup a file. Store handle and attributes.
274 * Return zero or error number.
277 nfs_lookupfh(d, name, newfd)
278 struct nfs_iodesc *d;
280 struct nfs_iodesc *newfd;
284 u_char fh[NFS_FHSIZE];
286 char name[FNAME_SIZE];
290 u_char fh[NFS_FHSIZE];
291 struct nfsv2_fattrs fa;
294 n_long h[RPC_HEADER_WORDS];
298 n_long h[RPC_HEADER_WORDS];
305 printf("lookupfh: called\n");
311 bzero(args, sizeof(*args));
312 bcopy(d->fh, args->fh, sizeof(args->fh));
314 if (len > sizeof(args->name))
315 len = sizeof(args->name);
316 bcopy(name, args->name, len);
317 args->len = htonl(len);
318 len = 4 + roundup(len, 4);
321 rlen = sizeof(*repl);
323 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
324 args, len, repl, rlen);
326 return (errno); /* XXX - from rpc_call */
330 /* saerrno.h now matches NFS error numbers. */
331 return (ntohl(repl->errno));
333 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
334 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
338 #ifndef NFS_NOSYMLINK
340 * Get the destination of a symbolic link.
344 struct nfs_iodesc *d;
348 n_long h[RPC_HEADER_WORDS];
349 u_char fh[NFS_FHSIZE];
352 n_long h[RPC_HEADER_WORDS];
353 struct nfs_readlnk_repl d;
359 printf("readlink: called\n");
362 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
363 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
364 sdata.fh, NFS_FHSIZE,
365 &rdata.d, sizeof(rdata.d));
373 return (ntohl(rdata.d.errno));
375 rdata.d.len = ntohl(rdata.d.len);
376 if (rdata.d.len > NFS_MAXPATHLEN)
377 return (ENAMETOOLONG);
379 bcopy(rdata.d.path, buf, rdata.d.len);
380 buf[rdata.d.len] = 0;
386 * Read data from a file.
387 * Return transfer count or -1 (and set errno)
390 nfs_readdata(d, off, addr, len)
391 struct nfs_iodesc *d;
396 struct nfs_read_args *args;
397 struct nfs_read_repl *repl;
399 n_long h[RPC_HEADER_WORDS];
400 struct nfs_read_args d;
403 n_long h[RPC_HEADER_WORDS];
404 struct nfs_read_repl d;
413 bcopy(d->fh, args->fh, NFS_FHSIZE);
414 args->off = htonl((n_long)off);
415 if (len > NFSREAD_SIZE)
417 args->len = htonl((n_long)len);
418 args->xxx = htonl((n_long)0);
419 hlen = sizeof(*repl) - NFSREAD_SIZE;
421 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
423 repl, sizeof(*repl));
425 /* errno was already set by rpc_call */
433 errno = ntohl(repl->errno);
437 x = ntohl(repl->count);
439 printf("nfsread: short packet, %d < %ld\n", rlen, x);
443 bcopy(repl->data, addr, x);
449 * return zero or error number
457 struct nfs_iodesc *currfd;
458 char buf[2 * NFS_FHSIZE + 3];
462 #ifndef NFS_NOSYMLINK
463 struct nfs_iodesc *newfd;
464 struct nfsv2_fattrs *fa;
467 char namebuf[NFS_MAXPATHLEN + 1];
468 char linkbuf[NFS_MAXPATHLEN + 1];
476 printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
479 printf("no rootpath, no nfs\n");
484 * This is silly - we should look at dv_type but that value is
485 * arch dependant and we can't use it here.
488 if (strcmp(f->f_dev->dv_name, "net") != 0)
491 if (strcmp(f->f_dev->dv_name, "pxe") != 0)
495 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
498 /* Bind to a reserved port. */
499 desc->myport = htons(--rpc_port);
500 desc->destip = rootip;
501 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
503 nfs_root_node.iodesc = desc;
505 fh = &nfs_root_node.fh[0];
508 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
509 sprintf(cp, "%02x", fh[i]);
511 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
512 setenv("boot.nfsroot.path", rootpath, 1);
513 setenv("boot.nfsroot.nfshandle", buf, 1);
515 #ifndef NFS_NOSYMLINK
516 /* Fake up attributes for the root dir. */
517 fa = &nfs_root_node.fa;
518 fa->fa_type = htonl(NFDIR);
519 fa->fa_mode = htonl(0755);
520 fa->fa_nlink = htonl(2);
522 currfd = &nfs_root_node;
525 cp = path = strdup(upath);
532 * Remove extra separators
540 * Check that current node is a directory.
542 if (currfd->fa.fa_type != htonl(NFDIR)) {
547 /* allocate file system specific data structure */
548 newfd = malloc(sizeof(*newfd));
549 newfd->iodesc = currfd->iodesc;
553 * Get next component of path name.
559 while ((c = *cp) != '\0' && c != '/') {
560 if (++len > NFS_MAXNAMLEN) {
569 /* lookup a file handle */
570 error = nfs_lookupfh(currfd, ncp, newfd);
576 * Check for symbolic link
578 if (newfd->fa.fa_type == htonl(NFLNK)) {
581 error = nfs_readlink(newfd, linkbuf);
585 link_len = strlen(linkbuf);
588 if (link_len + len > MAXPATHLEN
589 || ++nlinks > MAXSYMLINKS) {
594 bcopy(cp, &namebuf[link_len], len + 1);
595 bcopy(linkbuf, namebuf, link_len);
598 * If absolute pathname, restart at root.
599 * If relative pathname, restart at parent directory.
603 if (currfd != &nfs_root_node)
605 currfd = &nfs_root_node;
614 if (currfd != &nfs_root_node)
628 /* allocate file system specific data structure */
629 currfd = malloc(sizeof(*currfd));
630 currfd->iodesc = desc;
633 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
636 f->f_fsdata = (void *)currfd;
642 printf("nfs_open: %s lookupfh failed: %s\n",
643 path, strerror(error));
645 #ifndef NFS_NOSYMLINK
646 if (currfd != &nfs_root_node)
657 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
661 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
664 if (fp != &nfs_root_node && fp)
666 f->f_fsdata = (void *)0;
672 * read a portion of a file
675 nfs_read(f, buf, size, resid)
679 size_t *resid; /* out */
681 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
687 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
690 while ((int)size > 0) {
692 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
693 /* XXX maybe should retry on certain errors */
697 printf("nfs_read: read: %s", strerror(errno));
699 return (errno); /* XXX - from nfs_readdata */
704 printf("nfs_read: hit EOF unexpectantly");
723 nfs_write(f, buf, size, resid)
727 size_t *resid; /* out */
733 nfs_seek(f, offset, where)
738 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
739 n_long size = ntohl(d->fa.fa_size);
749 d->off = size - offset;
759 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
760 int nfs_stat_types[8] = {
761 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
768 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
771 ftype = ntohl(fp->fa.fa_type);
772 mode = ntohl(fp->fa.fa_mode);
773 mode |= nfs_stat_types[ftype & 7];
776 sb->st_nlink = ntohl(fp->fa.fa_nlink);
777 sb->st_uid = ntohl(fp->fa.fa_uid);
778 sb->st_gid = ntohl(fp->fa.fa_gid);
779 sb->st_size = ntohl(fp->fa.fa_size);
785 nfs_readdir(struct open_file *f, struct dirent *d)
787 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
788 struct nfs_readdir_args *args;
789 struct nfs_readdir_data *rd;
790 struct nfs_readdir_off *roff = NULL;
792 static n_long cookie = 0;
797 n_long h[RPC_HEADER_WORDS];
798 struct nfs_readdir_args d;
801 n_long h[RPC_HEADER_WORDS];
802 u_char d[NFS_READDIRSIZE];
808 bzero(args, sizeof(*args));
810 bcopy(fp->fh, args->fh, NFS_FHSIZE);
811 args->cookie = htonl(cookie);
812 args->count = htonl(NFS_READDIRSIZE);
814 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
816 rdata.d, sizeof(rdata.d));
818 roff = (struct nfs_readdir_off *)buf;
819 if (ntohl(roff->cookie) != 0)
822 roff = (struct nfs_readdir_off *)buf;
824 if (ntohl(roff->follows) == 0) {
825 eof = ntohl((roff+1)->cookie);
833 buf += sizeof(struct nfs_readdir_off);
834 rd = (struct nfs_readdir_data *)buf;
835 d->d_namlen = ntohl(rd->len);
836 bcopy(rd->name, d->d_name, d->d_namlen);
837 d->d_name[d->d_namlen] = '\0';
839 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
840 roff = (struct nfs_readdir_off *)buf;
841 cookie = ntohl(roff->cookie);
844 #else /* !OLD_NFSV2 */
846 * Fetch the root file handle (call mount daemon)
847 * Return zero or error number.
850 nfs_getrootfh(struct iodesc *d, char *path, uint32_t *fhlenp, u_char *fhp)
855 char path[FNAME_SIZE];
860 u_char fh[NFS_V3MAXFHSIZE];
865 uint32_t h[RPC_HEADER_WORDS];
869 uint32_t h[RPC_HEADER_WORDS];
876 printf("nfs_getrootfh: %s\n", path);
882 bzero(args, sizeof(*args));
884 if (len > sizeof(args->path))
885 len = sizeof(args->path);
886 args->len = htonl(len);
887 bcopy(path, args->path, len);
888 len = sizeof(uint32_t) + roundup(len, sizeof(uint32_t));
890 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER3, RPCMNT_MOUNT,
891 args, len, repl, sizeof(*repl));
893 /* errno was set by rpc_call */
895 if (cc < 2 * sizeof (uint32_t))
897 if (repl->errno != 0)
898 return (ntohl(repl->errno));
899 *fhlenp = ntohl(repl->fhsize);
900 bcopy(repl->fh, fhp, *fhlenp);
905 * Lookup a file. Store handle and attributes.
906 * Return zero or error number.
909 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
914 uint32_t fhplusname[1 +
915 (NFS_V3MAXFHSIZE + FNAME_SIZE) / sizeof(uint32_t)];
920 uint32_t fhplusattr[(NFS_V3MAXFHSIZE +
921 2 * (sizeof(uint32_t) +
922 sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
925 uint32_t h[RPC_HEADER_WORDS];
929 uint32_t h[RPC_HEADER_WORDS];
936 printf("lookupfh: called\n");
942 bzero(args, sizeof(*args));
943 args->fhsize = htonl(d->fhsize);
944 bcopy(d->fh, args->fhplusname, d->fhsize);
946 if (len > FNAME_SIZE)
948 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
949 args->fhplusname[pos++] = htonl(len);
950 bcopy(name, &args->fhplusname[pos], len);
951 len = sizeof(uint32_t) + pos * sizeof(uint32_t) +
952 roundup(len, sizeof(uint32_t));
954 rlen = sizeof(*repl);
956 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_LOOKUP,
957 args, len, repl, rlen);
959 return (errno); /* XXX - from rpc_call */
960 if (cc < 2 * sizeof(uint32_t))
962 if (repl->errno != 0)
963 /* saerrno.h now matches NFS error numbers. */
964 return (ntohl(repl->errno));
965 newfd->fhsize = ntohl(repl->fhsize);
966 bcopy(repl->fhplusattr, &newfd->fh, newfd->fhsize);
967 pos = roundup(newfd->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
968 if (repl->fhplusattr[pos++] == 0)
970 bcopy(&repl->fhplusattr[pos], &newfd->fa, sizeof(newfd->fa));
974 #ifndef NFS_NOSYMLINK
976 * Get the destination of a symbolic link.
979 nfs_readlink(struct nfs_iodesc *d, char *buf)
983 u_char fh[NFS_V3MAXFHSIZE];
988 struct nfsv3_fattrs fa;
990 u_char path[NFS_MAXPATHLEN];
993 uint32_t h[RPC_HEADER_WORDS];
997 uint32_t h[RPC_HEADER_WORDS];
1004 printf("readlink: called\n");
1010 bzero(args, sizeof(*args));
1011 args->fhsize = htonl(d->fhsize);
1012 bcopy(d->fh, args->fh, d->fhsize);
1013 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READLINK,
1014 args, sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
1015 repl, sizeof(*repl));
1019 if (cc < 2 * sizeof(uint32_t))
1022 if (repl->errno != 0)
1023 return (ntohl(repl->errno));
1028 repl->len = ntohl(repl->len);
1029 if (repl->len > NFS_MAXPATHLEN)
1030 return (ENAMETOOLONG);
1032 bcopy(repl->path, buf, repl->len);
1039 * Read data from a file.
1040 * Return transfer count or -1 (and set errno)
1043 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
1047 uint32_t fhoffcnt[NFS_V3MAXFHSIZE / sizeof(uint32_t) + 3];
1052 struct nfsv3_fattrs fa;
1056 u_char data[NFSREAD_SIZE];
1059 uint32_t h[RPC_HEADER_WORDS];
1063 uint32_t h[RPC_HEADER_WORDS];
1068 int hlen, rlen, pos;
1073 bzero(args, sizeof(*args));
1074 args->fhsize = htonl(d->fhsize);
1075 bcopy(d->fh, args->fhoffcnt, d->fhsize);
1076 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
1077 args->fhoffcnt[pos++] = 0;
1078 args->fhoffcnt[pos++] = htonl((uint32_t)off);
1079 if (len > NFSREAD_SIZE)
1081 args->fhoffcnt[pos] = htonl((uint32_t)len);
1082 hlen = sizeof(*repl) - NFSREAD_SIZE;
1084 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READ,
1085 args, 4 * sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
1086 repl, sizeof(*repl));
1088 /* errno was already set by rpc_call */
1094 if (repl->errno != 0) {
1095 errno = ntohl(repl->errno);
1099 x = ntohl(repl->count);
1101 printf("nfsread: short packet, %d < %ld\n", rlen, x);
1105 bcopy(repl->data, addr, x);
1111 * return zero or error number
1114 nfs_open(const char *upath, struct open_file *f)
1116 struct iodesc *desc;
1117 struct nfs_iodesc *currfd;
1118 char buf[2 * NFS_V3MAXFHSIZE + 3];
1122 #ifndef NFS_NOSYMLINK
1123 struct nfs_iodesc *newfd;
1124 struct nfsv3_fattrs *fa;
1127 char namebuf[NFS_MAXPATHLEN + 1];
1128 char linkbuf[NFS_MAXPATHLEN + 1];
1136 printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
1139 printf("no rootpath, no nfs\n");
1144 * This is silly - we should look at dv_type but that value is
1145 * arch dependant and we can't use it here.
1148 if (strcmp(f->f_dev->dv_name, "net") != 0)
1151 if (strcmp(f->f_dev->dv_name, "pxe") != 0)
1155 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
1158 /* Bind to a reserved port. */
1159 desc->myport = htons(--rpc_port);
1160 desc->destip = rootip;
1161 if ((error = nfs_getrootfh(desc, rootpath, &nfs_root_node.fhsize,
1164 nfs_root_node.iodesc = desc;
1166 fh = &nfs_root_node.fh[0];
1169 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
1170 sprintf(cp, "%02x", fh[i]);
1172 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
1173 setenv("boot.nfsroot.path", rootpath, 1);
1174 setenv("boot.nfsroot.nfshandle", buf, 1);
1175 sprintf(buf, "%d", nfs_root_node.fhsize);
1176 setenv("boot.nfsroot.nfshandlelen", buf, 1);
1178 #ifndef NFS_NOSYMLINK
1179 /* Fake up attributes for the root dir. */
1180 fa = &nfs_root_node.fa;
1181 fa->fa_type = htonl(NFDIR);
1182 fa->fa_mode = htonl(0755);
1183 fa->fa_nlink = htonl(2);
1185 currfd = &nfs_root_node;
1188 cp = path = strdup(upath);
1195 * Remove extra separators
1203 * Check that current node is a directory.
1205 if (currfd->fa.fa_type != htonl(NFDIR)) {
1210 /* allocate file system specific data structure */
1211 newfd = malloc(sizeof(*newfd));
1212 if (newfd == NULL) {
1216 newfd->iodesc = currfd->iodesc;
1220 * Get next component of path name.
1226 while ((c = *cp) != '\0' && c != '/') {
1227 if (++len > NFS_MAXNAMLEN) {
1236 /* lookup a file handle */
1237 error = nfs_lookupfh(currfd, ncp, newfd);
1243 * Check for symbolic link
1245 if (newfd->fa.fa_type == htonl(NFLNK)) {
1248 error = nfs_readlink(newfd, linkbuf);
1252 link_len = strlen(linkbuf);
1255 if (link_len + len > MAXPATHLEN
1256 || ++nlinks > MAXSYMLINKS) {
1261 bcopy(cp, &namebuf[link_len], len + 1);
1262 bcopy(linkbuf, namebuf, link_len);
1265 * If absolute pathname, restart at root.
1266 * If relative pathname, restart at parent directory.
1270 if (currfd != &nfs_root_node)
1272 currfd = &nfs_root_node;
1281 if (currfd != &nfs_root_node)
1293 /* allocate file system specific data structure */
1294 currfd = malloc(sizeof(*currfd));
1295 if (currfd != NULL) {
1296 currfd->iodesc = desc;
1299 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
1304 f->f_fsdata = (void *)currfd;
1310 printf("nfs_open: %s lookupfh failed: %s\n",
1311 path, strerror(error));
1313 #ifndef NFS_NOSYMLINK
1314 if (currfd != &nfs_root_node)
1322 nfs_close(struct open_file *f)
1324 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1328 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
1331 if (fp != &nfs_root_node && fp)
1333 f->f_fsdata = (void *)0;
1339 * read a portion of a file
1342 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
1344 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1350 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
1353 while ((int)size > 0) {
1355 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
1356 /* XXX maybe should retry on certain errors */
1360 printf("nfs_read: read: %s", strerror(errno));
1362 return (errno); /* XXX - from nfs_readdata */
1367 printf("nfs_read: hit EOF unexpectantly");
1386 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
1392 nfs_seek(struct open_file *f, off_t offset, int where)
1394 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
1395 uint32_t size = ntohl(d->fa.fa_size.val[1]);
1405 d->off = size - offset;
1415 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
1416 int nfs_stat_types[9] = {
1417 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, 0 };
1420 nfs_stat(struct open_file *f, struct stat *sb)
1422 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1423 uint32_t ftype, mode;
1425 ftype = ntohl(fp->fa.fa_type);
1426 mode = ntohl(fp->fa.fa_mode);
1427 mode |= nfs_stat_types[ftype & 7];
1430 sb->st_nlink = ntohl(fp->fa.fa_nlink);
1431 sb->st_uid = ntohl(fp->fa.fa_uid);
1432 sb->st_gid = ntohl(fp->fa.fa_gid);
1433 sb->st_size = ntohl(fp->fa.fa_size.val[1]);
1439 nfs_readdir(struct open_file *f, struct dirent *d)
1441 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
1442 struct nfsv3_readdir_repl *repl;
1443 struct nfsv3_readdir_entry *rent;
1445 static uint32_t cookie0 = 0;
1446 static uint32_t cookie1 = 0;
1448 static uint32_t cookieverf0 = 0;
1449 static uint32_t cookieverf1 = 0;
1454 uint32_t fhpluscookie[5 + NFS_V3MAXFHSIZE];
1457 uint32_t h[RPC_HEADER_WORDS];
1461 uint32_t h[RPC_HEADER_WORDS];
1462 u_char d[NFS_READDIRSIZE];
1465 if (cookie0 == 0 && cookie1 == 0) {
1468 bzero(args, sizeof(*args));
1470 args->fhsize = htonl(fp->fhsize);
1471 bcopy(fp->fh, args->fhpluscookie, fp->fhsize);
1472 pos = roundup(fp->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
1473 args->fhpluscookie[pos++] = cookie0;
1474 args->fhpluscookie[pos++] = cookie1;
1475 args->fhpluscookie[pos++] = cookieverf0;
1476 args->fhpluscookie[pos++] = cookieverf1;
1477 args->fhpluscookie[pos] = htonl(NFS_READDIRSIZE);
1479 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READDIR,
1480 args, 6 * sizeof(uint32_t) +
1481 roundup(fp->fhsize, sizeof(uint32_t)),
1482 rdata.d, sizeof(rdata.d));
1484 repl = (struct nfsv3_readdir_repl *)buf;
1485 if (repl->errno != 0)
1486 return (ntohl(repl->errno));
1487 cookieverf0 = repl->cookiev0;
1488 cookieverf1 = repl->cookiev1;
1489 buf += sizeof (struct nfsv3_readdir_repl);
1491 rent = (struct nfsv3_readdir_entry *)buf;
1493 if (rent->follows == 0) {
1494 /* fid0 is actually eof */
1495 if (rent->fid0 != 0) {
1505 d->d_namlen = ntohl(rent->len);
1506 bcopy(rent->nameplus, d->d_name, d->d_namlen);
1507 d->d_name[d->d_namlen] = '\0';
1509 pos = roundup(d->d_namlen, sizeof(uint32_t)) / sizeof(uint32_t);
1510 cookie0 = rent->nameplus[pos++];
1511 cookie1 = rent->nameplus[pos++];
1512 buf = (u_char *)&rent->nameplus[pos];
1515 #endif /* OLD_NFSV2 */