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)
420 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
423 /* Bind to a reserved port. */
424 desc->myport = htons(--rpc_port);
425 desc->destip = rootip;
426 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
428 nfs_root_node.iodesc = desc;
430 fh = &nfs_root_node.fh[0];
433 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
434 sprintf(cp, "%02x", fh[i]);
436 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
437 setenv("boot.nfsroot.path", rootpath, 1);
438 setenv("boot.nfsroot.nfshandle", buf, 1);
440 #ifndef NFS_NOSYMLINK
441 /* Fake up attributes for the root dir. */
442 fa = &nfs_root_node.fa;
443 fa->fa_type = htonl(NFDIR);
444 fa->fa_mode = htonl(0755);
445 fa->fa_nlink = htonl(2);
447 currfd = &nfs_root_node;
450 cp = path = strdup(upath);
457 * Remove extra separators
465 * Check that current node is a directory.
467 if (currfd->fa.fa_type != htonl(NFDIR)) {
472 /* allocate file system specific data structure */
473 newfd = malloc(sizeof(*newfd));
474 newfd->iodesc = currfd->iodesc;
478 * Get next component of path name.
484 while ((c = *cp) != '\0' && c != '/') {
485 if (++len > NFS_MAXNAMLEN) {
494 /* lookup a file handle */
495 error = nfs_lookupfh(currfd, ncp, newfd);
501 * Check for symbolic link
503 if (newfd->fa.fa_type == htonl(NFLNK)) {
506 error = nfs_readlink(newfd, linkbuf);
510 link_len = strlen(linkbuf);
513 if (link_len + len > MAXPATHLEN
514 || ++nlinks > MAXSYMLINKS) {
519 bcopy(cp, &namebuf[link_len], len + 1);
520 bcopy(linkbuf, namebuf, link_len);
523 * If absolute pathname, restart at root.
524 * If relative pathname, restart at parent directory.
528 if (currfd != &nfs_root_node)
530 currfd = &nfs_root_node;
539 if (currfd != &nfs_root_node)
553 /* allocate file system specific data structure */
554 currfd = malloc(sizeof(*currfd));
555 currfd->iodesc = desc;
558 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
561 f->f_fsdata = (void *)currfd;
567 printf("nfs_open: %s lookupfh failed: %s\n",
568 path, strerror(error));
570 #ifndef NFS_NOSYMLINK
571 if (currfd != &nfs_root_node)
582 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
586 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
589 if (fp != &nfs_root_node && fp)
591 f->f_fsdata = (void *)0;
597 * read a portion of a file
600 nfs_read(f, buf, size, resid)
604 size_t *resid; /* out */
606 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
612 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
615 while ((int)size > 0) {
617 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
618 /* XXX maybe should retry on certain errors */
622 printf("nfs_read: read: %s", strerror(errno));
624 return (errno); /* XXX - from nfs_readdata */
629 printf("nfs_read: hit EOF unexpectantly");
648 nfs_write(f, buf, size, resid)
652 size_t *resid; /* out */
658 nfs_seek(f, offset, where)
663 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
664 n_long size = ntohl(d->fa.fa_size);
674 d->off = size - offset;
684 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
685 int nfs_stat_types[8] = {
686 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
693 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
696 ftype = ntohl(fp->fa.fa_type);
697 mode = ntohl(fp->fa.fa_mode);
698 mode |= nfs_stat_types[ftype & 7];
701 sb->st_nlink = ntohl(fp->fa.fa_nlink);
702 sb->st_uid = ntohl(fp->fa.fa_uid);
703 sb->st_gid = ntohl(fp->fa.fa_gid);
704 sb->st_size = ntohl(fp->fa.fa_size);
710 nfs_readdir(struct open_file *f, struct dirent *d)
712 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
713 struct nfs_readdir_args *args;
714 struct nfs_readdir_data *rd;
715 struct nfs_readdir_off *roff = NULL;
717 static n_long cookie = 0;
722 n_long h[RPC_HEADER_WORDS];
723 struct nfs_readdir_args d;
726 n_long h[RPC_HEADER_WORDS];
727 u_char d[NFS_READDIRSIZE];
733 bzero(args, sizeof(*args));
735 bcopy(fp->fh, args->fh, NFS_FHSIZE);
736 args->cookie = htonl(cookie);
737 args->count = htonl(NFS_READDIRSIZE);
739 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
741 rdata.d, sizeof(rdata.d));
743 roff = (struct nfs_readdir_off *)buf;
744 if (ntohl(roff->cookie) != 0)
747 roff = (struct nfs_readdir_off *)buf;
749 if (ntohl(roff->follows) == 0) {
750 eof = ntohl((roff+1)->cookie);
758 buf += sizeof(struct nfs_readdir_off);
759 rd = (struct nfs_readdir_data *)buf;
760 d->d_namlen = ntohl(rd->len);
761 bcopy(rd->name, d->d_name, d->d_namlen);
762 d->d_name[d->d_namlen] = '\0';
764 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
765 roff = (struct nfs_readdir_off *)buf;
766 cookie = ntohl(roff->cookie);