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 our own NFS attributes without NQNFS stuff. */
66 struct nfsv2_time fa_atime;
67 struct nfsv2_time fa_mtime;
68 struct nfsv2_time fa_ctime;
72 struct nfs_read_args {
73 u_char fh[NFS_FHSIZE];
76 n_long xxx; /* XXX what's this for? */
79 /* Data part of nfs rpc reply (also the largest thing we receive) */
80 #define NFSREAD_SIZE 1024
81 struct nfs_read_repl {
83 struct nfsv2_fattrs fa;
85 u_char data[NFSREAD_SIZE];
89 struct nfs_readlnk_repl {
92 char path[NFS_MAXPATHLEN];
96 struct nfs_readdir_args {
97 u_char fh[NFS_FHSIZE];
102 struct nfs_readdir_data {
108 struct nfs_readdir_off {
114 struct iodesc *iodesc;
116 u_char fh[NFS_FHSIZE];
117 struct nfsv2_fattrs fa; /* all in network order */
121 * XXX interactions with tftp? See nfswrapper.c for a confusing
124 int nfs_open(const char *path, struct open_file *f);
125 static int nfs_close(struct open_file *f);
126 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
127 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
128 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
129 static int nfs_stat(struct open_file *f, struct stat *sb);
130 static int nfs_readdir(struct open_file *f, struct dirent *d);
132 struct nfs_iodesc nfs_root_node;
134 struct fs_ops nfs_fsops = {
146 * Fetch the root file handle (call mount daemon)
147 * Return zero or error number.
150 nfs_getrootfh(d, path, fhp)
158 char path[FNAME_SIZE];
162 u_char fh[NFS_FHSIZE];
165 n_long h[RPC_HEADER_WORDS];
169 n_long h[RPC_HEADER_WORDS];
176 printf("nfs_getrootfh: %s\n", path);
182 bzero(args, sizeof(*args));
184 if (len > sizeof(args->path))
185 len = sizeof(args->path);
186 args->len = htonl(len);
187 bcopy(path, args->path, len);
188 len = 4 + roundup(len, 4);
190 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
191 args, len, repl, sizeof(*repl));
193 /* errno was set by rpc_call */
199 return (ntohl(repl->errno));
200 bcopy(repl->fh, fhp, sizeof(repl->fh));
205 * Lookup a file. Store handle and attributes.
206 * Return zero or error number.
209 nfs_lookupfh(d, name, newfd)
210 struct nfs_iodesc *d;
212 struct nfs_iodesc *newfd;
216 u_char fh[NFS_FHSIZE];
218 char name[FNAME_SIZE];
222 u_char fh[NFS_FHSIZE];
223 struct nfsv2_fattrs fa;
226 n_long h[RPC_HEADER_WORDS];
230 n_long h[RPC_HEADER_WORDS];
237 printf("lookupfh: called\n");
243 bzero(args, sizeof(*args));
244 bcopy(d->fh, args->fh, sizeof(args->fh));
246 if (len > sizeof(args->name))
247 len = sizeof(args->name);
248 bcopy(name, args->name, len);
249 args->len = htonl(len);
250 len = 4 + roundup(len, 4);
253 rlen = sizeof(*repl);
255 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
256 args, len, repl, rlen);
258 return (errno); /* XXX - from rpc_call */
262 /* saerrno.h now matches NFS error numbers. */
263 return (ntohl(repl->errno));
265 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
266 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
270 #ifndef NFS_NOSYMLINK
272 * Get the destination of a symbolic link.
276 struct nfs_iodesc *d;
280 n_long h[RPC_HEADER_WORDS];
281 u_char fh[NFS_FHSIZE];
284 n_long h[RPC_HEADER_WORDS];
285 struct nfs_readlnk_repl d;
291 printf("readlink: called\n");
294 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
295 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
296 sdata.fh, NFS_FHSIZE,
297 &rdata.d, sizeof(rdata.d));
305 return (ntohl(rdata.d.errno));
307 rdata.d.len = ntohl(rdata.d.len);
308 if (rdata.d.len > NFS_MAXPATHLEN)
309 return (ENAMETOOLONG);
311 bcopy(rdata.d.path, buf, rdata.d.len);
312 buf[rdata.d.len] = 0;
318 * Read data from a file.
319 * Return transfer count or -1 (and set errno)
322 nfs_readdata(d, off, addr, len)
323 struct nfs_iodesc *d;
328 struct nfs_read_args *args;
329 struct nfs_read_repl *repl;
331 n_long h[RPC_HEADER_WORDS];
332 struct nfs_read_args d;
335 n_long h[RPC_HEADER_WORDS];
336 struct nfs_read_repl d;
345 bcopy(d->fh, args->fh, NFS_FHSIZE);
346 args->off = htonl((n_long)off);
347 if (len > NFSREAD_SIZE)
349 args->len = htonl((n_long)len);
350 args->xxx = htonl((n_long)0);
351 hlen = sizeof(*repl) - NFSREAD_SIZE;
353 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
355 repl, sizeof(*repl));
357 /* errno was already set by rpc_call */
365 errno = ntohl(repl->errno);
369 x = ntohl(repl->count);
371 printf("nfsread: short packet, %d < %ld\n", rlen, x);
375 bcopy(repl->data, addr, x);
381 * return zero or error number
389 struct nfs_iodesc *currfd;
390 char buf[2 * NFS_FHSIZE + 3];
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");
416 if (strcmp(f->f_dev->dv_name, "net") != 0)
419 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
422 /* Bind to a reserved port. */
423 desc->myport = htons(--rpc_port);
424 desc->destip = rootip;
425 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
427 nfs_root_node.iodesc = desc;
429 fh = &nfs_root_node.fh[0];
432 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
433 sprintf(cp, "%02x", fh[i]);
435 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
436 setenv("boot.nfsroot.path", rootpath, 1);
437 setenv("boot.nfsroot.nfshandle", buf, 1);
439 #ifndef NFS_NOSYMLINK
440 /* Fake up attributes for the root dir. */
441 fa = &nfs_root_node.fa;
442 fa->fa_type = htonl(NFDIR);
443 fa->fa_mode = htonl(0755);
444 fa->fa_nlink = htonl(2);
446 currfd = &nfs_root_node;
449 cp = path = strdup(upath);
456 * Remove extra separators
464 * Check that current node is a directory.
466 if (currfd->fa.fa_type != htonl(NFDIR)) {
471 /* allocate file system specific data structure */
472 newfd = malloc(sizeof(*newfd));
473 newfd->iodesc = currfd->iodesc;
477 * Get next component of path name.
483 while ((c = *cp) != '\0' && c != '/') {
484 if (++len > NFS_MAXNAMLEN) {
493 /* lookup a file handle */
494 error = nfs_lookupfh(currfd, ncp, newfd);
500 * Check for symbolic link
502 if (newfd->fa.fa_type == htonl(NFLNK)) {
505 error = nfs_readlink(newfd, linkbuf);
509 link_len = strlen(linkbuf);
512 if (link_len + len > MAXPATHLEN
513 || ++nlinks > MAXSYMLINKS) {
518 bcopy(cp, &namebuf[link_len], len + 1);
519 bcopy(linkbuf, namebuf, link_len);
522 * If absolute pathname, restart at root.
523 * If relative pathname, restart at parent directory.
527 if (currfd != &nfs_root_node)
529 currfd = &nfs_root_node;
538 if (currfd != &nfs_root_node)
552 /* allocate file system specific data structure */
553 currfd = malloc(sizeof(*currfd));
554 currfd->iodesc = desc;
557 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
560 f->f_fsdata = (void *)currfd;
566 printf("nfs_open: %s lookupfh failed: %s\n",
567 path, strerror(error));
569 #ifndef NFS_NOSYMLINK
570 if (currfd != &nfs_root_node)
581 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
585 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
588 if (fp != &nfs_root_node && fp)
590 f->f_fsdata = (void *)0;
596 * read a portion of a file
599 nfs_read(f, buf, size, resid)
603 size_t *resid; /* out */
605 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
611 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
614 while ((int)size > 0) {
616 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
617 /* XXX maybe should retry on certain errors */
621 printf("nfs_read: read: %s", strerror(errno));
623 return (errno); /* XXX - from nfs_readdata */
628 printf("nfs_read: hit EOF unexpectantly");
647 nfs_write(f, buf, size, resid)
651 size_t *resid; /* out */
657 nfs_seek(f, offset, where)
662 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
663 n_long size = ntohl(d->fa.fa_size);
673 d->off = size - offset;
682 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
683 int nfs_stat_types[8] = {
684 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
691 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
694 ftype = ntohl(fp->fa.fa_type);
695 mode = ntohl(fp->fa.fa_mode);
696 mode |= nfs_stat_types[ftype & 7];
699 sb->st_nlink = ntohl(fp->fa.fa_nlink);
700 sb->st_uid = ntohl(fp->fa.fa_uid);
701 sb->st_gid = ntohl(fp->fa.fa_gid);
702 sb->st_size = ntohl(fp->fa.fa_size);
708 nfs_readdir(struct open_file *f, struct dirent *d)
710 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
711 struct nfs_readdir_args *args;
712 struct nfs_readdir_data *rd;
713 struct nfs_readdir_off *roff = NULL;
715 static n_long cookie = 0;
720 n_long h[RPC_HEADER_WORDS];
721 struct nfs_readdir_args d;
724 n_long h[RPC_HEADER_WORDS];
725 u_char d[NFS_READDIRSIZE];
731 bzero(args, sizeof(*args));
733 bcopy(fp->fh, args->fh, NFS_FHSIZE);
734 args->cookie = htonl(cookie);
735 args->count = htonl(NFS_READDIRSIZE);
737 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
739 rdata.d, sizeof(rdata.d));
741 roff = (struct nfs_readdir_off *)buf;
742 if (ntohl(roff->cookie) != 0)
745 roff = (struct nfs_readdir_off *)buf;
747 if (ntohl(roff->follows) == 0) {
748 eof = ntohl((roff+1)->cookie);
756 buf += sizeof(struct nfs_readdir_off);
757 rd = (struct nfs_readdir_data *)buf;
758 d->d_namlen = ntohl(rd->len);
759 bcopy(rd->name, d->d_name, d->d_namlen);
760 d->d_name[d->d_namlen] = '\0';
762 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
763 roff = (struct nfs_readdir_off *)buf;
764 cookie = ntohl(roff->cookie);