1 /* $FreeBSD: src/lib/libstand/nfs.c,v 1.2.6.3 2000/09/10 01:33:25 ps Exp $ */
2 /* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
5 * Copyright (c) 1993 John Brezak
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
34 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
52 /* Define our own NFS attributes without NQNFS stuff. */
65 struct nfsv2_time fa_atime;
66 struct nfsv2_time fa_mtime;
67 struct nfsv2_time fa_ctime;
71 struct nfs_read_args {
72 u_char fh[NFS_FHSIZE];
75 n_long xxx; /* XXX what's this for? */
79 * Data part of nfs rpc reply (also the largest thing we receive).
80 * Worry about the size of the structure declared on the stack.
83 #define NFSREAD_MIN_SIZE 1024
84 #define NFSREAD_MAX_SIZE 4096
86 struct nfs_read_repl {
88 struct nfsv2_fattrs fa;
90 u_char data[NFSREAD_MAX_SIZE];
94 struct nfs_readlnk_repl {
97 char path[NFS_MAXPATHLEN];
101 struct nfs_readdir_args {
102 u_char fh[NFS_FHSIZE];
107 struct nfs_readdir_data {
113 struct nfs_readdir_off {
119 struct iodesc *iodesc;
121 u_char fh[NFS_FHSIZE];
122 struct nfsv2_fattrs fa; /* all in network order */
126 * XXX interactions with tftp? See nfswrapper.c for a confusing
129 int nfs_open(const char *path, struct open_file *f);
130 static int nfs_close(struct open_file *f);
131 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
132 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
133 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
134 static int nfs_stat(struct open_file *f, struct stat *sb);
135 static int nfs_readdir(struct open_file *f, struct dirent *d);
137 struct nfs_iodesc nfs_root_node;
139 struct fs_ops nfs_fsops = {
150 static int nfs_read_size = NFSREAD_MIN_SIZE;
153 * Fetch the root file handle (call mount daemon)
154 * Return zero or error number.
157 nfs_getrootfh(struct iodesc *d, char *path, u_char *fhp)
162 char path[FNAME_SIZE];
166 u_char fh[NFS_FHSIZE];
169 n_long h[RPC_HEADER_WORDS];
173 n_long h[RPC_HEADER_WORDS];
180 printf("nfs_getrootfh: %s\n", path);
186 bzero(args, sizeof(*args));
188 if (len > sizeof(args->path))
189 len = sizeof(args->path);
190 args->len = htonl(len);
191 bcopy(path, args->path, len);
192 len = 4 + roundup(len, 4);
194 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
195 args, len, repl, sizeof(*repl));
197 /* errno was set by rpc_call */
203 return (ntohl(repl->errno));
204 bcopy(repl->fh, fhp, sizeof(repl->fh));
207 * Improve boot performance over NFS
209 if (getenv("nfs.read_size") != NULL)
210 nfs_read_size = strtol(getenv("nfs.read_size"), NULL, 0);
211 if (nfs_read_size < NFSREAD_MIN_SIZE)
212 nfs_read_size = NFSREAD_MIN_SIZE;
213 if (nfs_read_size > NFSREAD_MAX_SIZE)
214 nfs_read_size = NFSREAD_MAX_SIZE;
220 * Lookup a file. Store handle and attributes.
221 * Return zero or error number.
224 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
228 u_char fh[NFS_FHSIZE];
230 char name[FNAME_SIZE];
234 u_char fh[NFS_FHSIZE];
235 struct nfsv2_fattrs fa;
238 n_long h[RPC_HEADER_WORDS];
242 n_long h[RPC_HEADER_WORDS];
249 printf("lookupfh: called\n");
255 bzero(args, sizeof(*args));
256 bcopy(d->fh, args->fh, sizeof(args->fh));
258 if (len > sizeof(args->name))
259 len = sizeof(args->name);
260 bcopy(name, args->name, len);
261 args->len = htonl(len);
262 len = 4 + roundup(len, 4);
265 rlen = sizeof(*repl);
267 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
268 args, len, repl, rlen);
270 return (errno); /* XXX - from rpc_call */
274 /* saerrno.h now matches NFS error numbers. */
275 return (ntohl(repl->errno));
277 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
278 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
282 #ifndef NFS_NOSYMLINK
284 * Get the destination of a symbolic link.
287 nfs_readlink(struct nfs_iodesc *d, char *buf)
290 n_long h[RPC_HEADER_WORDS];
291 u_char fh[NFS_FHSIZE];
294 n_long h[RPC_HEADER_WORDS];
295 struct nfs_readlnk_repl d;
301 printf("readlink: called\n");
304 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
305 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
306 sdata.fh, NFS_FHSIZE,
307 &rdata.d, sizeof(rdata.d));
315 return (ntohl(rdata.d.errno));
317 rdata.d.len = ntohl(rdata.d.len);
318 if (rdata.d.len > NFS_MAXPATHLEN)
319 return (ENAMETOOLONG);
321 bcopy(rdata.d.path, buf, rdata.d.len);
322 buf[rdata.d.len] = 0;
328 * Read data from a file.
329 * Return transfer count or -1 (and set errno)
332 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
334 struct nfs_read_args *args;
335 struct nfs_read_repl *repl;
337 n_long h[RPC_HEADER_WORDS];
338 struct nfs_read_args d;
341 n_long h[RPC_HEADER_WORDS];
342 struct nfs_read_repl d;
351 bcopy(d->fh, args->fh, NFS_FHSIZE);
352 args->off = htonl((n_long)off);
353 if (len > nfs_read_size)
355 args->len = htonl((n_long)len);
356 args->xxx = htonl((n_long)0);
357 hlen = offsetof(struct nfs_read_repl, data[0]);
359 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
361 repl, sizeof(*repl));
363 /* errno was already set by rpc_call */
371 errno = ntohl(repl->errno);
375 x = ntohl(repl->count);
377 printf("nfsread: short packet, %d < %ld\n", rlen, x);
381 bcopy(repl->data, addr, x);
387 * return zero or error number
390 nfs_open(const char *upath, struct open_file *f)
393 struct nfs_iodesc *currfd;
394 #ifndef NFS_NOSYMLINK
395 struct nfs_iodesc *newfd;
396 struct nfsv2_fattrs *fa;
399 char namebuf[NFS_MAXPATHLEN + 1];
400 char linkbuf[NFS_MAXPATHLEN + 1];
408 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
411 printf("no rootpath, no nfs\n");
415 /* Avoid trying out nfs_open for disk devices in the EFI loader */
417 if (strcmp(f->f_dev->dv_name, "net") != 0)
421 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
424 /* Bind to a reserved port. */
425 desc->myport = htons(rpc_newport());
426 desc->destip = rootip;
427 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
429 nfs_root_node.iodesc = desc;
431 #ifndef NFS_NOSYMLINK
432 /* Fake up attributes for the root dir. */
433 fa = &nfs_root_node.fa;
434 fa->fa_type = htonl(NFDIR);
435 fa->fa_mode = htonl(0755);
436 fa->fa_nlink = htonl(2);
438 currfd = &nfs_root_node;
441 cp = path = strdup(upath);
448 * Remove extra separators
456 * Check that current node is a directory.
458 if (currfd->fa.fa_type != htonl(NFDIR)) {
463 /* allocate file system specific data structure */
464 newfd = malloc(sizeof(*newfd));
465 newfd->iodesc = currfd->iodesc;
469 * Get next component of path name.
475 while ((c = *cp) != '\0' && c != '/') {
476 if (++len > NFS_MAXNAMLEN) {
485 /* lookup a file handle */
486 error = nfs_lookupfh(currfd, ncp, newfd);
492 * Check for symbolic link
494 if (newfd->fa.fa_type == htonl(NFLNK)) {
497 error = nfs_readlink(newfd, linkbuf);
501 link_len = strlen(linkbuf);
504 if (link_len + len > MAXPATHLEN
505 || ++nlinks > MAXSYMLINKS) {
510 bcopy(cp, &namebuf[link_len], len + 1);
511 bcopy(linkbuf, namebuf, link_len);
514 * If absolute pathname, restart at root.
515 * If relative pathname, restart at parent directory.
519 if (currfd != &nfs_root_node)
521 currfd = &nfs_root_node;
530 if (currfd != &nfs_root_node)
544 /* allocate file system specific data structure */
545 currfd = malloc(sizeof(*currfd));
546 currfd->iodesc = desc;
549 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
552 f->f_fsdata = (void *)currfd;
558 printf("nfs_open: %s lookupfh failed: %s\n",
559 path, strerror(error));
561 #ifndef NFS_NOSYMLINK
562 if (currfd != &nfs_root_node)
570 nfs_close(struct open_file *f)
572 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
576 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
580 if (fp && fp != &nfs_root_node)
587 * read a portion of a file
593 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
595 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
601 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
604 while ((int)size > 0) {
606 cc = nfs_readdata(fp, fp->off, addr, size);
607 /* XXX maybe should retry on certain errors */
611 printf("nfs_read: read: %s", strerror(errno));
613 return (errno); /* XXX - from nfs_readdata */
618 printf("nfs_read: hit EOF unexpectantly");
640 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
646 nfs_seek(struct open_file *f, off_t offset, int where)
648 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
649 n_long size = ntohl(d->fa.fa_size);
659 d->off = size - offset;
668 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
669 int nfs_stat_types[8] = {
670 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
673 nfs_stat(struct open_file *f, struct stat *sb)
675 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
678 ftype = ntohl(fp->fa.fa_type);
679 mode = ntohl(fp->fa.fa_mode);
680 mode |= nfs_stat_types[ftype & 7];
683 sb->st_nlink = ntohl(fp->fa.fa_nlink);
684 sb->st_uid = ntohl(fp->fa.fa_uid);
685 sb->st_gid = ntohl(fp->fa.fa_gid);
686 sb->st_size = ntohl(fp->fa.fa_size);
692 nfs_readdir(struct open_file *f, struct dirent *d)
694 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
695 struct nfs_readdir_args *args;
696 struct nfs_readdir_data *rd;
697 struct nfs_readdir_off *roff = NULL;
699 static n_long cookie = 0;
704 n_long h[RPC_HEADER_WORDS];
705 struct nfs_readdir_args d;
708 n_long h[RPC_HEADER_WORDS];
709 u_char d[NFS_READDIRSIZE];
715 bzero(args, sizeof(*args));
717 bcopy(fp->fh, args->fh, NFS_FHSIZE);
718 args->cookie = htonl(cookie);
719 args->count = htonl(NFS_READDIRSIZE);
721 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
723 rdata.d, sizeof(rdata.d));
725 roff = (struct nfs_readdir_off *)buf;
726 if (ntohl(roff->cookie) != 0)
729 roff = (struct nfs_readdir_off *)buf;
731 if (ntohl(roff->follows) == 0) {
732 eof = ntohl((roff+1)->cookie);
740 buf += sizeof(struct nfs_readdir_off);
741 rd = (struct nfs_readdir_data *)buf;
742 d->d_namlen = ntohl(rd->len);
743 bcopy(rd->name, d->d_name, d->d_namlen);
744 d->d_name[d->d_namlen] = '\0';
746 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
747 roff = (struct nfs_readdir_off *)buf;
748 cookie = ntohl(roff->cookie);