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 4096
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 int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
130 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
131 static int nfs_stat(struct open_file *f, struct stat *sb);
132 static int nfs_readdir(struct open_file *f, struct dirent *d);
134 struct nfs_iodesc nfs_root_node;
136 struct fs_ops nfs_fsops = {
147 static int nfs_read_size = NFSREAD_MIN_SIZE;
150 * Improve boot performance over NFS
153 set_nfs_read_size(void)
158 if ((env = getenv("nfs.read_size")) != NULL) {
160 nfs_read_size = (int)strtol(env, &end, 0);
161 if (errno != 0 || *env == '\0' || *end != '\0') {
162 printf("%s: bad value: \"%s\", defaulting to %d\n",
163 "nfs.read_size", env, NFSREAD_MIN_SIZE);
164 nfs_read_size = NFSREAD_MIN_SIZE;
167 if (nfs_read_size < NFSREAD_MIN_SIZE) {
168 printf("%s: bad value: \"%d\", defaulting to %d\n",
169 "nfs.read_size", nfs_read_size, NFSREAD_MIN_SIZE);
170 nfs_read_size = NFSREAD_MIN_SIZE;
172 if (nfs_read_size > NFSREAD_MAX_SIZE) {
173 printf("%s: bad value: \"%d\", defaulting to %d\n",
174 "nfs.read_size", nfs_read_size, NFSREAD_MIN_SIZE);
175 nfs_read_size = NFSREAD_MAX_SIZE;
177 snprintf(buf, sizeof (buf), "%d", nfs_read_size);
178 setenv("nfs.read_size", buf, 1);
182 * Fetch the root file handle (call mount daemon)
183 * Return zero or error number.
186 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];
205 uint32_t h[RPC_HEADER_WORDS];
212 printf("nfs_getrootfh: %s\n", path);
218 bzero(args, sizeof(*args));
220 if (len > sizeof(args->path))
221 len = sizeof(args->path);
222 args->len = htonl(len);
223 bcopy(path, args->path, len);
224 len = sizeof(uint32_t) + roundup(len, sizeof(uint32_t));
226 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER3, RPCMNT_MOUNT,
227 args, len, repl, sizeof(*repl));
229 /* errno was set by rpc_call */
231 if (cc < 2 * sizeof (uint32_t))
233 if (repl->errno != 0)
234 return (ntohl(repl->errno));
235 *fhlenp = ntohl(repl->fhsize);
236 bcopy(repl->fh, fhp, *fhlenp);
243 * Lookup a file. Store handle and attributes.
244 * Return zero or error number.
247 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
252 uint32_t fhplusname[1 +
253 (NFS_V3MAXFHSIZE + FNAME_SIZE) / sizeof(uint32_t)];
258 uint32_t fhplusattr[(NFS_V3MAXFHSIZE +
259 2 * (sizeof(uint32_t) +
260 sizeof(struct nfsv3_fattrs))) / sizeof(uint32_t)];
263 uint32_t h[RPC_HEADER_WORDS];
267 uint32_t h[RPC_HEADER_WORDS];
274 printf("lookupfh: called\n");
280 bzero(args, sizeof(*args));
281 args->fhsize = htonl(d->fhsize);
282 bcopy(d->fh, args->fhplusname, d->fhsize);
284 if (len > FNAME_SIZE)
286 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
287 args->fhplusname[pos++] = htonl(len);
288 bcopy(name, &args->fhplusname[pos], len);
289 len = sizeof(uint32_t) + pos * sizeof(uint32_t) +
290 roundup(len, sizeof(uint32_t));
292 rlen = sizeof(*repl);
294 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_LOOKUP,
295 args, len, repl, rlen);
297 return (errno); /* XXX - from rpc_call */
298 if (cc < 2 * sizeof(uint32_t))
300 if (repl->errno != 0)
301 /* saerrno.h now matches NFS error numbers. */
302 return (ntohl(repl->errno));
303 newfd->fhsize = ntohl(repl->fhsize);
304 bcopy(repl->fhplusattr, &newfd->fh, newfd->fhsize);
305 pos = roundup(newfd->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
306 if (repl->fhplusattr[pos++] == 0)
308 bcopy(&repl->fhplusattr[pos], &newfd->fa, sizeof(newfd->fa));
312 #ifndef NFS_NOSYMLINK
314 * Get the destination of a symbolic link.
317 nfs_readlink(struct nfs_iodesc *d, char *buf)
321 u_char fh[NFS_V3MAXFHSIZE];
326 struct nfsv3_fattrs fa;
328 u_char path[NFS_MAXPATHLEN];
331 uint32_t h[RPC_HEADER_WORDS];
335 uint32_t h[RPC_HEADER_WORDS];
342 printf("readlink: called\n");
348 bzero(args, sizeof(*args));
349 args->fhsize = htonl(d->fhsize);
350 bcopy(d->fh, args->fh, d->fhsize);
351 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READLINK,
352 args, sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
353 repl, sizeof(*repl));
357 if (cc < 2 * sizeof(uint32_t))
360 if (repl->errno != 0)
361 return (ntohl(repl->errno));
366 repl->len = ntohl(repl->len);
367 if (repl->len > NFS_MAXPATHLEN)
368 return (ENAMETOOLONG);
370 bcopy(repl->path, buf, repl->len);
377 * Read data from a file.
378 * Return transfer count or -1 (and set errno)
381 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
385 uint32_t fhoffcnt[NFS_V3MAXFHSIZE / sizeof(uint32_t) + 3];
390 struct nfsv3_fattrs fa;
394 u_char data[NFSREAD_MAX_SIZE];
397 uint32_t h[RPC_HEADER_WORDS];
401 uint32_t h[RPC_HEADER_WORDS];
411 bzero(args, sizeof(*args));
412 args->fhsize = htonl(d->fhsize);
413 bcopy(d->fh, args->fhoffcnt, d->fhsize);
414 pos = roundup(d->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
415 args->fhoffcnt[pos++] = 0;
416 args->fhoffcnt[pos++] = htonl((uint32_t)off);
417 if (len > nfs_read_size)
419 args->fhoffcnt[pos] = htonl((uint32_t)len);
420 hlen = offsetof(struct repl, data[0]);
422 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READ,
423 args, 4 * sizeof(uint32_t) + roundup(d->fhsize, sizeof(uint32_t)),
424 repl, sizeof(*repl));
426 /* errno was already set by rpc_call */
432 if (repl->errno != 0) {
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
452 nfs_open(const char *upath, struct open_file *f)
455 struct nfs_iodesc *currfd;
456 char buf[2 * NFS_V3MAXFHSIZE + 3];
460 #ifndef NFS_NOSYMLINK
461 struct nfs_iodesc *newfd;
462 struct nfsv3_fattrs *fa;
465 char namebuf[NFS_MAXPATHLEN + 1];
466 char linkbuf[NFS_MAXPATHLEN + 1];
472 if (netproto != NET_NFS)
477 printf("nfs_open: %s (rootpath=%s)\n", upath, rootpath);
480 printf("no rootpath, no nfs\n");
485 * This is silly - we should look at dv_type but that value is
486 * arch dependant and we can't use it here.
489 if (strcmp(f->f_dev->dv_name, "net") != 0)
492 if (strcmp(f->f_dev->dv_name, "pxe") != 0)
496 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
499 /* Bind to a reserved port. */
500 desc->myport = htons(--rpc_port);
501 desc->destip = rootip;
502 if ((error = nfs_getrootfh(desc, rootpath, &nfs_root_node.fhsize,
505 nfs_root_node.fa.fa_type = htonl(NFDIR);
506 nfs_root_node.fa.fa_mode = htonl(0755);
507 nfs_root_node.fa.fa_nlink = htonl(2);
508 nfs_root_node.iodesc = desc;
510 fh = &nfs_root_node.fh[0];
513 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
514 sprintf(cp, "%02x", fh[i]);
516 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
517 setenv("boot.nfsroot.path", rootpath, 1);
518 setenv("boot.nfsroot.nfshandle", buf, 1);
519 sprintf(buf, "%d", nfs_root_node.fhsize);
520 setenv("boot.nfsroot.nfshandlelen", buf, 1);
522 /* Allocate file system specific data structure */
523 currfd = malloc(sizeof(*newfd));
524 if (currfd == NULL) {
528 #ifndef NFS_NOSYMLINK
529 bcopy(&nfs_root_node, currfd, sizeof(*currfd));
532 cp = path = strdup(upath);
539 * Remove extra separators
547 * Check that current node is a directory.
549 if (currfd->fa.fa_type != htonl(NFDIR)) {
554 /* allocate file system specific data structure */
555 newfd = malloc(sizeof(*newfd));
560 newfd->iodesc = currfd->iodesc;
563 * Get next component of path name.
569 while ((c = *cp) != '\0' && c != '/') {
570 if (++len > NFS_MAXNAMLEN) {
579 /* lookup a file handle */
580 error = nfs_lookupfh(currfd, ncp, newfd);
586 * Check for symbolic link
588 if (newfd->fa.fa_type == htonl(NFLNK)) {
591 error = nfs_readlink(newfd, linkbuf);
595 link_len = strlen(linkbuf);
598 if (link_len + len > MAXPATHLEN
599 || ++nlinks > MAXSYMLINKS) {
604 bcopy(cp, &namebuf[link_len], len + 1);
605 bcopy(linkbuf, namebuf, link_len);
608 * If absolute pathname, restart at root.
609 * If relative pathname, restart at parent directory.
613 bcopy(&nfs_root_node, currfd, sizeof(*currfd));
632 currfd->iodesc = desc;
634 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
639 f->f_fsdata = (void *)currfd;
645 printf("nfs_open: %s lookupfh failed: %s\n",
646 path, strerror(error));
654 nfs_close(struct open_file *f)
656 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
660 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
665 f->f_fsdata = (void *)0;
671 * read a portion of a file
674 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
676 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
682 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
685 while ((int)size > 0) {
687 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
688 /* XXX maybe should retry on certain errors */
692 printf("nfs_read: read: %s", strerror(errno));
694 return (errno); /* XXX - from nfs_readdata */
699 printf("nfs_read: hit EOF unexpectantly");
718 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
724 nfs_seek(struct open_file *f, off_t offset, int where)
726 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
727 uint32_t size = ntohl(d->fa.fa_size.val[1]);
737 d->off = size - offset;
747 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
748 int nfs_stat_types[9] = {
749 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, 0 };
752 nfs_stat(struct open_file *f, struct stat *sb)
754 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
755 uint32_t ftype, mode;
757 ftype = ntohl(fp->fa.fa_type);
758 mode = ntohl(fp->fa.fa_mode);
759 mode |= nfs_stat_types[ftype & 7];
762 sb->st_nlink = ntohl(fp->fa.fa_nlink);
763 sb->st_uid = ntohl(fp->fa.fa_uid);
764 sb->st_gid = ntohl(fp->fa.fa_gid);
765 sb->st_size = ntohl(fp->fa.fa_size.val[1]);
771 nfs_readdir(struct open_file *f, struct dirent *d)
773 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
774 struct nfsv3_readdir_repl *repl;
775 struct nfsv3_readdir_entry *rent;
777 static struct nfs_iodesc *pfp = NULL;
778 static uint64_t cookie = 0;
784 uint32_t fhpluscookie[5 + NFS_V3MAXFHSIZE];
787 uint32_t h[RPC_HEADER_WORDS];
791 uint32_t h[RPC_HEADER_WORDS];
792 u_char d[NFS_READDIRSIZE];
795 if (fp != pfp || fp->off != cookie) {
799 bzero(args, sizeof(*args));
801 args->fhsize = htonl(fp->fhsize);
802 bcopy(fp->fh, args->fhpluscookie, fp->fhsize);
803 pos = roundup(fp->fhsize, sizeof(uint32_t)) / sizeof(uint32_t);
804 args->fhpluscookie[pos++] = htonl(fp->off >> 32);
805 args->fhpluscookie[pos++] = htonl(fp->off);
806 args->fhpluscookie[pos++] = htonl(fp->cookie >> 32);
807 args->fhpluscookie[pos++] = htonl(fp->cookie);
808 args->fhpluscookie[pos] = htonl(NFS_READDIRSIZE);
810 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER3, NFSPROCV3_READDIR,
811 args, 6 * sizeof(uint32_t) +
812 roundup(fp->fhsize, sizeof(uint32_t)),
813 rdata.d, sizeof(rdata.d));
815 repl = (struct nfsv3_readdir_repl *)buf;
816 if (repl->errno != 0)
817 return (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 = (u_char *)&rent->nameplus[pos];