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>
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
51 /* Define our own NFS attributes without NQNFS stuff. */
64 struct nfsv2_time fa_atime;
65 struct nfsv2_time fa_mtime;
66 struct nfsv2_time fa_ctime;
70 struct nfs_read_args {
71 u_char fh[NFS_FHSIZE];
74 n_long xxx; /* XXX what's this for? */
77 /* Data part of nfs rpc reply (also the largest thing we receive) */
78 #define NFSREAD_SIZE 1024
79 struct nfs_read_repl {
81 struct nfsv2_fattrs fa;
83 u_char data[NFSREAD_SIZE];
87 struct nfs_readlnk_repl {
90 char path[NFS_MAXPATHLEN];
94 struct nfs_readdir_args {
95 u_char fh[NFS_FHSIZE];
100 struct nfs_readdir_data {
106 struct nfs_readdir_off {
112 struct iodesc *iodesc;
114 u_char fh[NFS_FHSIZE];
115 struct nfsv2_fattrs fa; /* all in network order */
119 * XXX interactions with tftp? See nfswrapper.c for a confusing
122 int nfs_open(const char *path, struct open_file *f);
123 static int nfs_close(struct open_file *f);
124 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
125 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
126 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
127 static int nfs_stat(struct open_file *f, struct stat *sb);
128 static int nfs_readdir(struct open_file *f, struct dirent *d);
130 struct nfs_iodesc nfs_root_node;
132 struct fs_ops nfs_fsops = {
144 * Fetch the root file handle (call mount daemon)
145 * Return zero or error number.
148 nfs_getrootfh(d, path, fhp)
149 register struct iodesc *d;
156 char path[FNAME_SIZE];
160 u_char fh[NFS_FHSIZE];
163 n_long h[RPC_HEADER_WORDS];
167 n_long h[RPC_HEADER_WORDS];
174 printf("nfs_getrootfh: %s\n", path);
180 bzero(args, sizeof(*args));
182 if (len > sizeof(args->path))
183 len = sizeof(args->path);
184 args->len = htonl(len);
185 bcopy(path, args->path, len);
186 len = 4 + roundup(len, 4);
188 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
189 args, len, repl, sizeof(*repl));
191 /* errno was set by rpc_call */
197 return (ntohl(repl->errno));
198 bcopy(repl->fh, fhp, sizeof(repl->fh));
203 * Lookup a file. Store handle and attributes.
204 * Return zero or error number.
207 nfs_lookupfh(d, name, newfd)
208 struct nfs_iodesc *d;
210 struct nfs_iodesc *newfd;
212 register int len, rlen;
214 u_char fh[NFS_FHSIZE];
216 char name[FNAME_SIZE];
220 u_char fh[NFS_FHSIZE];
221 struct nfsv2_fattrs fa;
224 n_long h[RPC_HEADER_WORDS];
228 n_long h[RPC_HEADER_WORDS];
235 printf("lookupfh: called\n");
241 bzero(args, sizeof(*args));
242 bcopy(d->fh, args->fh, sizeof(args->fh));
244 if (len > sizeof(args->name))
245 len = sizeof(args->name);
246 bcopy(name, args->name, len);
247 args->len = htonl(len);
248 len = 4 + roundup(len, 4);
251 rlen = sizeof(*repl);
253 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
254 args, len, repl, rlen);
256 return (errno); /* XXX - from rpc_call */
260 /* saerrno.h now matches NFS error numbers. */
261 return (ntohl(repl->errno));
263 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
264 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
268 #ifndef NFS_NOSYMLINK
270 * Get the destination of a symbolic link.
274 struct nfs_iodesc *d;
278 n_long h[RPC_HEADER_WORDS];
279 u_char fh[NFS_FHSIZE];
282 n_long h[RPC_HEADER_WORDS];
283 struct nfs_readlnk_repl d;
289 printf("readlink: called\n");
292 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
293 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
294 sdata.fh, NFS_FHSIZE,
295 &rdata.d, sizeof(rdata.d));
303 return (ntohl(rdata.d.errno));
305 rdata.d.len = ntohl(rdata.d.len);
306 if (rdata.d.len > NFS_MAXPATHLEN)
307 return (ENAMETOOLONG);
309 bcopy(rdata.d.path, buf, rdata.d.len);
310 buf[rdata.d.len] = 0;
316 * Read data from a file.
317 * Return transfer count or -1 (and set errno)
320 nfs_readdata(d, off, addr, len)
321 struct nfs_iodesc *d;
326 struct nfs_read_args *args;
327 struct nfs_read_repl *repl;
329 n_long h[RPC_HEADER_WORDS];
330 struct nfs_read_args d;
333 n_long h[RPC_HEADER_WORDS];
334 struct nfs_read_repl d;
343 bcopy(d->fh, args->fh, NFS_FHSIZE);
344 args->off = htonl((n_long)off);
345 if (len > NFSREAD_SIZE)
347 args->len = htonl((n_long)len);
348 args->xxx = htonl((n_long)0);
349 hlen = sizeof(*repl) - NFSREAD_SIZE;
351 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
353 repl, sizeof(*repl));
355 /* errno was already set by rpc_call */
363 errno = ntohl(repl->errno);
367 x = ntohl(repl->count);
369 printf("nfsread: short packet, %d < %ld\n", rlen, x);
373 bcopy(repl->data, addr, x);
379 * return zero or error number
387 struct nfs_iodesc *currfd;
388 #ifndef NFS_NOSYMLINK
389 struct nfs_iodesc *newfd;
390 struct nfsv2_fattrs *fa;
391 register char *cp, *ncp;
393 char namebuf[NFS_MAXPATHLEN + 1];
394 char linkbuf[NFS_MAXPATHLEN + 1];
402 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
405 printf("no rootpath, no nfs\n");
409 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
412 /* Bind to a reserved port. */
413 desc->myport = htons(--rpc_port);
414 desc->destip = rootip;
415 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
417 nfs_root_node.iodesc = desc;
419 #ifndef NFS_NOSYMLINK
420 /* Fake up attributes for the root dir. */
421 fa = &nfs_root_node.fa;
422 fa->fa_type = htonl(NFDIR);
423 fa->fa_mode = htonl(0755);
424 fa->fa_nlink = htonl(2);
426 currfd = &nfs_root_node;
429 cp = path = strdup(upath);
436 * Remove extra separators
444 * Check that current node is a directory.
446 if (currfd->fa.fa_type != htonl(NFDIR)) {
451 /* allocate file system specific data structure */
452 newfd = malloc(sizeof(*newfd));
453 newfd->iodesc = currfd->iodesc;
457 * Get next component of path name.
460 register int len = 0;
463 while ((c = *cp) != '\0' && c != '/') {
464 if (++len > NFS_MAXNAMLEN) {
473 /* lookup a file handle */
474 error = nfs_lookupfh(currfd, ncp, newfd);
480 * Check for symbolic link
482 if (newfd->fa.fa_type == htonl(NFLNK)) {
485 error = nfs_readlink(newfd, linkbuf);
489 link_len = strlen(linkbuf);
492 if (link_len + len > MAXPATHLEN
493 || ++nlinks > MAXSYMLINKS) {
498 bcopy(cp, &namebuf[link_len], len + 1);
499 bcopy(linkbuf, namebuf, link_len);
502 * If absolute pathname, restart at root.
503 * If relative pathname, restart at parent directory.
507 if (currfd != &nfs_root_node)
509 currfd = &nfs_root_node;
518 if (currfd != &nfs_root_node)
532 /* allocate file system specific data structure */
533 currfd = malloc(sizeof(*currfd));
534 currfd->iodesc = desc;
537 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
540 f->f_fsdata = (void *)currfd;
546 printf("nfs_open: %s lookupfh failed: %s\n",
547 path, strerror(error));
549 #ifndef NFS_NOSYMLINK
550 if (currfd != &nfs_root_node)
561 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
565 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
568 if (fp != &nfs_root_node && fp)
570 f->f_fsdata = (void *)0;
576 * read a portion of a file
579 nfs_read(f, buf, size, resid)
583 size_t *resid; /* out */
585 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
587 register char *addr = buf;
591 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
594 while ((int)size > 0) {
596 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
597 /* XXX maybe should retry on certain errors */
601 printf("nfs_read: read: %s", strerror(errno));
603 return (errno); /* XXX - from nfs_readdata */
608 printf("nfs_read: hit EOF unexpectantly");
627 nfs_write(f, buf, size, resid)
631 size_t *resid; /* out */
637 nfs_seek(f, offset, where)
642 register struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
643 n_long size = ntohl(d->fa.fa_size);
653 d->off = size - offset;
662 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
663 int nfs_stat_types[8] = {
664 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
671 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
672 register n_long ftype, mode;
674 ftype = ntohl(fp->fa.fa_type);
675 mode = ntohl(fp->fa.fa_mode);
676 mode |= nfs_stat_types[ftype & 7];
679 sb->st_nlink = ntohl(fp->fa.fa_nlink);
680 sb->st_uid = ntohl(fp->fa.fa_uid);
681 sb->st_gid = ntohl(fp->fa.fa_gid);
682 sb->st_size = ntohl(fp->fa.fa_size);
688 nfs_readdir(struct open_file *f, struct dirent *d)
690 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
691 struct nfs_readdir_args *args;
692 struct nfs_readdir_data *rd;
693 struct nfs_readdir_off *roff = NULL;
695 static n_long cookie = 0;
700 n_long h[RPC_HEADER_WORDS];
701 struct nfs_readdir_args d;
704 n_long h[RPC_HEADER_WORDS];
705 u_char d[NFS_READDIRSIZE];
711 bzero(args, sizeof(*args));
713 bcopy(fp->fh, args->fh, NFS_FHSIZE);
714 args->cookie = htonl(cookie);
715 args->count = htonl(NFS_READDIRSIZE);
717 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
719 rdata.d, sizeof(rdata.d));
721 roff = (struct nfs_readdir_off *)buf;
722 if (ntohl(roff->cookie) != 0)
725 roff = (struct nfs_readdir_off *)buf;
727 if (ntohl(roff->follows) == 0) {
728 eof = ntohl((roff+1)->cookie);
736 buf += sizeof(struct nfs_readdir_off);
737 rd = (struct nfs_readdir_data *)buf;
738 d->d_namlen = ntohl(rd->len);
739 bcopy(rd->name, d->d_name, d->d_namlen);
740 d->d_name[d->d_namlen] = '\0';
742 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
743 roff = (struct nfs_readdir_off *)buf;
744 cookie = ntohl(roff->cookie);