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>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
54 #define NFSREAD_MIN_SIZE 1024
55 #define NFSREAD_MAX_SIZE 16384
57 /* NFSv3 definitions */
58 #define NFS_V3MAXFHSIZE 64
61 #define NFSPROCV3_LOOKUP 3
62 #define NFSPROCV3_READLINK 5
63 #define NFSPROCV3_READ 6
64 #define NFSPROCV3_READDIR 16
86 struct nfsv3_time fa_atime;
87 struct nfsv3_time fa_mtime;
88 struct nfsv3_time fa_ctime;
92 * For NFSv3, the file handle is variable in size, so most fixed sized
93 * structures for arguments won't work. For most cases, a structure
94 * that starts with any fixed size section is followed by an array
95 * that covers the maximum size required.
97 struct nfsv3_readdir_repl {
100 struct nfsv3_fattrs fa;
105 struct nfsv3_readdir_entry {
110 uint32_t nameplus[0];
114 struct iodesc *iodesc;
117 u_char fh[NFS_V3MAXFHSIZE];
118 struct nfsv3_fattrs fa; /* all in network order */
123 * XXX interactions with tftp? See nfswrapper.c for a confusing
126 int nfs_open(const char *path, struct open_file *f);
127 static int nfs_close(struct open_file *f);
128 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
129 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
130 static int nfs_stat(struct open_file *f, struct stat *sb);
131 static int nfs_readdir(struct open_file *f, struct dirent *d);
133 struct nfs_iodesc nfs_root_node;
135 struct fs_ops nfs_fsops = {
138 .fo_close = nfs_close,
140 .fo_write = null_write,
143 .fo_readdir = nfs_readdir,
146 static int nfs_read_size = NFSREAD_MIN_SIZE;
149 * Improve boot performance over NFS
152 set_nfs_read_size(void)
157 if ((env = getenv("nfs.read_size")) != NULL) {
159 nfs_read_size = (int)strtol(env, &end, 0);
160 if (errno != 0 || *env == '\0' || *end != '\0') {
161 printf("%s: bad value: \"%s\", defaulting to %d\n",
162 "nfs.read_size", env, NFSREAD_MIN_SIZE);
163 nfs_read_size = NFSREAD_MIN_SIZE;
166 if (nfs_read_size < NFSREAD_MIN_SIZE) {
167 printf("%s: bad value: \"%d\", defaulting to %d\n",
168 "nfs.read_size", nfs_read_size, NFSREAD_MIN_SIZE);
169 nfs_read_size = NFSREAD_MIN_SIZE;
171 if (nfs_read_size > NFSREAD_MAX_SIZE) {
172 printf("%s: bad value: \"%d\", defaulting to %d\n",
173 "nfs.read_size", nfs_read_size, NFSREAD_MIN_SIZE);
174 nfs_read_size = NFSREAD_MAX_SIZE;
176 snprintf(buf, sizeof (buf), "%d", nfs_read_size);
177 setenv("nfs.read_size", buf, 1);
181 * Fetch the root file handle (call mount daemon)
182 * Return zero or error number.
185 nfs_getrootfh(struct iodesc *d, char *path, uint32_t *fhlenp, u_char *fhp)
191 char path[FNAME_SIZE];
196 u_char fh[NFS_V3MAXFHSIZE];
201 uint32_t h[RPC_HEADER_WORDS];
208 printf("nfs_getrootfh: %s\n", path);
213 bzero(args, sizeof(*args));
215 if (len > sizeof(args->path))
216 len = sizeof(args->path);
217 args->len = htonl(len);
218 bcopy(path, args->path, len);
219 len = sizeof(uint32_t) + roundup(len, sizeof(uint32_t));
221 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER3, RPCMNT_MOUNT,
222 args, len, (void **)&repl, &pkt);
225 /* errno was set by rpc_call */
228 if (cc < 2 * sizeof (uint32_t)) {
232 if (repl->errno != 0) {
234 return (ntohl(repl->errno));
236 *fhlenp = ntohl(repl->fhsize);
237 bcopy(repl->fh, fhp, *fhlenp);
245 * Lookup a file. Store handle and attributes.
246 * Return zero or error number.
249 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
255 uint32_t fhplusname[1 +
256 (NFS_V3MAXFHSIZE + FNAME_SIZE) / sizeof(uint32_t)];
261 uint32_t fhplusattr[(NFS_V3MAXFHSIZE +
262 2 * (sizeof(uint32_t) +
263 sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
266 uint32_t h[RPC_HEADER_WORDS];
273 printf("lookupfh: called\n");
278 bzero(args, sizeof(*args));
279 args->fhsize = htonl(d->fhsize);
280 bcopy(d->fh, args->fhplusname, d->fhsize);
282 if (len > FNAME_SIZE)
284 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
285 args->fhplusname[pos++] = htonl(len);
286 bcopy(name, &args->fhplusname[pos], len);
287 len = sizeof(uint32_t) + pos * sizeof(uint32_t) +
288 roundup(len, sizeof(uint32_t));
290 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_LOOKUP,
291 args, len, (void **)&repl, &pkt);
294 return (errno); /* XXX - from rpc_call */
296 if (cc < 2 * sizeof(uint32_t)) {
300 if (repl->errno != 0) {
302 /* saerrno.h now matches NFS error numbers. */
303 return (ntohl(repl->errno));
305 newfd->fhsize = ntohl(repl->fhsize);
306 bcopy(repl->fhplusattr, &newfd->fh, newfd->fhsize);
307 pos = roundup(newfd->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
308 if (repl->fhplusattr[pos++] == 0) {
312 bcopy(&repl->fhplusattr[pos], &newfd->fa, sizeof(newfd->fa));
317 #ifndef NFS_NOSYMLINK
319 * Get the destination of a symbolic link.
322 nfs_readlink(struct nfs_iodesc *d, char *buf)
327 u_char fh[NFS_V3MAXFHSIZE];
332 struct nfsv3_fattrs fa;
334 u_char path[NFS_MAXPATHLEN];
337 uint32_t h[RPC_HEADER_WORDS];
345 printf("readlink: called\n");
350 bzero(args, sizeof(*args));
351 args->fhsize = htonl(d->fhsize);
352 bcopy(d->fh, args->fh, d->fhsize);
353 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READLINK,
354 args, sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
355 (void **)&repl, &pkt);
359 if (cc < 2 * sizeof(uint32_t)) {
364 if (repl->errno != 0) {
365 rc = ntohl(repl->errno);
374 repl->len = ntohl(repl->len);
375 if (repl->len > NFS_MAXPATHLEN) {
380 bcopy(repl->path, buf, repl->len);
389 * Read data from a file.
390 * Return transfer count or -1 (and set errno)
393 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
398 uint32_t fhoffcnt[NFS_V3MAXFHSIZE / sizeof(uint32_t) + 3];
403 struct nfsv3_fattrs fa;
407 u_char data[NFSREAD_MAX_SIZE];
410 uint32_t h[RPC_HEADER_WORDS];
419 bzero(args, sizeof(*args));
420 args->fhsize = htonl(d->fhsize);
421 bcopy(d->fh, args->fhoffcnt, d->fhsize);
422 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
423 args->fhoffcnt[pos++] = 0;
424 args->fhoffcnt[pos++] = htonl((uint32_t)off);
425 if (len > nfs_read_size)
427 args->fhoffcnt[pos] = htonl((uint32_t)len);
428 hlen = offsetof(struct repl, data[0]);
430 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READ,
431 args, 4 * sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
432 (void **)&repl, &pkt);
434 /* errno was already set by rpc_call */
442 if (repl->errno != 0) {
443 errno = ntohl(repl->errno);
448 x = ntohl(repl->count);
450 printf("nfsread: short packet, %d < %ld\n", rlen, x);
455 bcopy(repl->data, addr, x);
462 * return zero or error number
465 nfs_open(const char *upath, struct open_file *f)
469 struct nfs_iodesc *currfd = NULL;
470 char buf[2 * NFS_V3MAXFHSIZE + 3];
474 #ifndef NFS_NOSYMLINK
475 struct nfs_iodesc *newfd = NULL;
478 char namebuf[NFS_MAXPATHLEN + 1];
479 char linkbuf[NFS_MAXPATHLEN + 1];
485 if (netproto != NET_NFS)
491 printf("nfs_open: %s (rootip=%s rootpath=%s)\n", upath,
492 inet_ntoa(rootip), rootpath);
495 printf("no rootpath, no nfs\n");
499 if (f->f_dev->dv_type != DEVT_NET)
502 if (!(desc = socktodesc(*(int *)(dev->d_opendata))))
505 /* Bind to a reserved port. */
506 desc->myport = htons(--rpc_port);
507 desc->destip = rootip;
508 if ((error = nfs_getrootfh(desc, rootpath, &nfs_root_node.fhsize,
511 nfs_root_node.fa.fa_type = htonl(NFDIR);
512 nfs_root_node.fa.fa_mode = htonl(0755);
513 nfs_root_node.fa.fa_nlink = htonl(2);
514 nfs_root_node.iodesc = desc;
516 fh = &nfs_root_node.fh[0];
519 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
520 sprintf(cp, "%02x", fh[i]);
522 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
523 setenv("boot.nfsroot.path", rootpath, 1);
524 setenv("boot.nfsroot.nfshandle", buf, 1);
525 sprintf(buf, "%d", nfs_root_node.fhsize);
526 setenv("boot.nfsroot.nfshandlelen", buf, 1);
528 /* Allocate file system specific data structure */
529 currfd = malloc(sizeof(*newfd));
530 if (currfd == NULL) {
534 #ifndef NFS_NOSYMLINK
535 bcopy(&nfs_root_node, currfd, sizeof(*currfd));
538 cp = path = strdup(upath);
545 * Remove extra separators
553 * Check that current node is a directory.
555 if (currfd->fa.fa_type != htonl(NFDIR)) {
560 /* allocate file system specific data structure */
561 newfd = malloc(sizeof(*newfd));
566 newfd->iodesc = currfd->iodesc;
569 * Get next component of path name.
575 while ((c = *cp) != '\0' && c != '/') {
576 if (++len > NFS_MAXNAMLEN) {
585 /* lookup a file handle */
586 error = nfs_lookupfh(currfd, ncp, newfd);
592 * Check for symbolic link
594 if (newfd->fa.fa_type == htonl(NFLNK)) {
597 error = nfs_readlink(newfd, linkbuf);
601 link_len = strlen(linkbuf);
604 if (link_len + len > MAXPATHLEN
605 || ++nlinks > MAXSYMLINKS) {
610 bcopy(cp, &namebuf[link_len], len + 1);
611 bcopy(linkbuf, namebuf, link_len);
614 * If absolute pathname, restart at root.
615 * If relative pathname, restart at parent directory.
619 bcopy(&nfs_root_node, currfd, sizeof(*currfd));
638 currfd->iodesc = desc;
640 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
645 f->f_fsdata = (void *)currfd;
651 printf("nfs_open: %s lookupfh failed: %s\n",
652 path, strerror(error));
660 nfs_close(struct open_file *f)
662 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
666 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
676 * read a portion of a file
679 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
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\n", strerror(errno));
699 return (errno); /* XXX - from nfs_readdata */
704 printf("nfs_read: hit EOF unexpectedly\n");
720 nfs_seek(struct open_file *f, off_t offset, int where)
722 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
723 uint32_t size = ntohl(d->fa.fa_size.val[1]);
733 d->off = size - offset;
743 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
744 int nfs_stat_types[9] = {
745 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, 0 };
748 nfs_stat(struct open_file *f, struct stat *sb)
750 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
751 uint32_t ftype, mode;
753 ftype = ntohl(fp->fa.fa_type);
754 mode = ntohl(fp->fa.fa_mode);
755 mode |= nfs_stat_types[ftype & 7];
758 sb->st_nlink = ntohl(fp->fa.fa_nlink);
759 sb->st_uid = ntohl(fp->fa.fa_uid);
760 sb->st_gid = ntohl(fp->fa.fa_gid);
761 sb->st_size = ntohl(fp->fa.fa_size.val[1]);
767 nfs_readdir(struct open_file *f, struct dirent *d)
769 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
770 struct nfsv3_readdir_repl *repl;
771 struct nfsv3_readdir_entry *rent;
772 static void *pkt = NULL;
774 static struct nfs_iodesc *pfp = NULL;
775 static uint64_t cookie = 0;
781 uint32_t fhpluscookie[5 + NFS_V3MAXFHSIZE];
784 uint32_t h[RPC_HEADER_WORDS];
788 if (fp != pfp || fp->off != cookie) {
794 bzero(args, sizeof(*args));
796 args->fhsize = htonl(fp->fhsize);
797 bcopy(fp->fh, args->fhpluscookie, fp->fhsize);
798 pos = roundup(fp->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
799 args->fhpluscookie[pos++] = htonl(fp->off >> 32);
800 args->fhpluscookie[pos++] = htonl(fp->off);
801 args->fhpluscookie[pos++] = htonl(fp->cookie >> 32);
802 args->fhpluscookie[pos++] = htonl(fp->cookie);
803 args->fhpluscookie[pos] = htonl(NFS_READDIRSIZE);
805 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READDIR,
806 args, 6 * sizeof(uint32_t) +
807 roundup(fp->fhsize, sizeof(uint32_t)),
808 (void **)&buf, &pkt);
813 repl = (struct nfsv3_readdir_repl *)buf;
814 if (repl->errno != 0) {
815 rc = ntohl(repl->errno);
820 fp->cookie = ((uint64_t)ntohl(repl->cookiev0) << 32) |
821 ntohl(repl->cookiev1);
822 buf += sizeof (struct nfsv3_readdir_repl);
824 rent = (struct nfsv3_readdir_entry *)buf;
826 if (rent->follows == 0) {
827 /* fid0 is actually eof */
828 if (rent->fid0 != 0) {
835 d->d_namlen = ntohl(rent->len);
836 bcopy(rent->nameplus, d->d_name, d->d_namlen);
837 d->d_name[d->d_namlen] = '\0';
839 pos = roundup(d->d_namlen, sizeof(uint32_t)) / sizeof(uint32_t);
840 fp->off = cookie = ((uint64_t)ntohl(rent->nameplus[pos]) << 32) |
841 ntohl(rent->nameplus[pos + 1]);
843 buf = (char *)&rent->nameplus[pos];