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(struct iodesc *d, char *path, u_char *fhp)
153 char path[FNAME_SIZE];
157 u_char fh[NFS_FHSIZE];
160 n_long h[RPC_HEADER_WORDS];
164 n_long h[RPC_HEADER_WORDS];
171 printf("nfs_getrootfh: %s\n", path);
177 bzero(args, sizeof(*args));
179 if (len > sizeof(args->path))
180 len = sizeof(args->path);
181 args->len = htonl(len);
182 bcopy(path, args->path, len);
183 len = 4 + roundup(len, 4);
185 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
186 args, len, repl, sizeof(*repl));
188 /* errno was set by rpc_call */
194 return (ntohl(repl->errno));
195 bcopy(repl->fh, fhp, sizeof(repl->fh));
200 * Lookup a file. Store handle and attributes.
201 * Return zero or error number.
204 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
208 u_char fh[NFS_FHSIZE];
210 char name[FNAME_SIZE];
214 u_char fh[NFS_FHSIZE];
215 struct nfsv2_fattrs fa;
218 n_long h[RPC_HEADER_WORDS];
222 n_long h[RPC_HEADER_WORDS];
229 printf("lookupfh: called\n");
235 bzero(args, sizeof(*args));
236 bcopy(d->fh, args->fh, sizeof(args->fh));
238 if (len > sizeof(args->name))
239 len = sizeof(args->name);
240 bcopy(name, args->name, len);
241 args->len = htonl(len);
242 len = 4 + roundup(len, 4);
245 rlen = sizeof(*repl);
247 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
248 args, len, repl, rlen);
250 return (errno); /* XXX - from rpc_call */
254 /* saerrno.h now matches NFS error numbers. */
255 return (ntohl(repl->errno));
257 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
258 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
262 #ifndef NFS_NOSYMLINK
264 * Get the destination of a symbolic link.
267 nfs_readlink(struct nfs_iodesc *d, char *buf)
270 n_long h[RPC_HEADER_WORDS];
271 u_char fh[NFS_FHSIZE];
274 n_long h[RPC_HEADER_WORDS];
275 struct nfs_readlnk_repl d;
281 printf("readlink: called\n");
284 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
285 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
286 sdata.fh, NFS_FHSIZE,
287 &rdata.d, sizeof(rdata.d));
295 return (ntohl(rdata.d.errno));
297 rdata.d.len = ntohl(rdata.d.len);
298 if (rdata.d.len > NFS_MAXPATHLEN)
299 return (ENAMETOOLONG);
301 bcopy(rdata.d.path, buf, rdata.d.len);
302 buf[rdata.d.len] = 0;
308 * Read data from a file.
309 * Return transfer count or -1 (and set errno)
312 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
314 struct nfs_read_args *args;
315 struct nfs_read_repl *repl;
317 n_long h[RPC_HEADER_WORDS];
318 struct nfs_read_args d;
321 n_long h[RPC_HEADER_WORDS];
322 struct nfs_read_repl d;
331 bcopy(d->fh, args->fh, NFS_FHSIZE);
332 args->off = htonl((n_long)off);
333 if (len > NFSREAD_SIZE)
335 args->len = htonl((n_long)len);
336 args->xxx = htonl((n_long)0);
337 hlen = sizeof(*repl) - NFSREAD_SIZE;
339 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
341 repl, sizeof(*repl));
343 /* errno was already set by rpc_call */
351 errno = ntohl(repl->errno);
355 x = ntohl(repl->count);
357 printf("nfsread: short packet, %d < %ld\n", rlen, x);
361 bcopy(repl->data, addr, x);
367 * return zero or error number
370 nfs_open(const char *upath, struct open_file *f)
373 struct nfs_iodesc *currfd;
374 #ifndef NFS_NOSYMLINK
375 struct nfs_iodesc *newfd;
376 struct nfsv2_fattrs *fa;
379 char namebuf[NFS_MAXPATHLEN + 1];
380 char linkbuf[NFS_MAXPATHLEN + 1];
388 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
391 printf("no rootpath, no nfs\n");
395 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
398 /* Bind to a reserved port. */
399 desc->myport = htons(--rpc_port);
400 desc->destip = rootip;
401 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
403 nfs_root_node.iodesc = desc;
405 #ifndef NFS_NOSYMLINK
406 /* Fake up attributes for the root dir. */
407 fa = &nfs_root_node.fa;
408 fa->fa_type = htonl(NFDIR);
409 fa->fa_mode = htonl(0755);
410 fa->fa_nlink = htonl(2);
412 currfd = &nfs_root_node;
415 cp = path = strdup(upath);
422 * Remove extra separators
430 * Check that current node is a directory.
432 if (currfd->fa.fa_type != htonl(NFDIR)) {
437 /* allocate file system specific data structure */
438 newfd = malloc(sizeof(*newfd));
439 newfd->iodesc = currfd->iodesc;
443 * Get next component of path name.
449 while ((c = *cp) != '\0' && c != '/') {
450 if (++len > NFS_MAXNAMLEN) {
459 /* lookup a file handle */
460 error = nfs_lookupfh(currfd, ncp, newfd);
466 * Check for symbolic link
468 if (newfd->fa.fa_type == htonl(NFLNK)) {
471 error = nfs_readlink(newfd, linkbuf);
475 link_len = strlen(linkbuf);
478 if (link_len + len > MAXPATHLEN
479 || ++nlinks > MAXSYMLINKS) {
484 bcopy(cp, &namebuf[link_len], len + 1);
485 bcopy(linkbuf, namebuf, link_len);
488 * If absolute pathname, restart at root.
489 * If relative pathname, restart at parent directory.
493 if (currfd != &nfs_root_node)
495 currfd = &nfs_root_node;
504 if (currfd != &nfs_root_node)
518 /* allocate file system specific data structure */
519 currfd = malloc(sizeof(*currfd));
520 currfd->iodesc = desc;
523 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
526 f->f_fsdata = (void *)currfd;
532 printf("nfs_open: %s lookupfh failed: %s\n",
533 path, strerror(error));
535 #ifndef NFS_NOSYMLINK
536 if (currfd != &nfs_root_node)
544 nfs_close(struct open_file *f)
546 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
550 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
554 if (fp && fp != &nfs_root_node)
561 * read a portion of a file
567 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
569 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
575 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
578 while ((int)size > 0) {
580 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
581 /* XXX maybe should retry on certain errors */
585 printf("nfs_read: read: %s", strerror(errno));
587 return (errno); /* XXX - from nfs_readdata */
592 printf("nfs_read: hit EOF unexpectantly");
614 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
620 nfs_seek(struct open_file *f, off_t offset, int where)
622 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
623 n_long size = ntohl(d->fa.fa_size);
633 d->off = size - offset;
642 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
643 int nfs_stat_types[8] = {
644 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
647 nfs_stat(struct open_file *f, struct stat *sb)
649 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
652 ftype = ntohl(fp->fa.fa_type);
653 mode = ntohl(fp->fa.fa_mode);
654 mode |= nfs_stat_types[ftype & 7];
657 sb->st_nlink = ntohl(fp->fa.fa_nlink);
658 sb->st_uid = ntohl(fp->fa.fa_uid);
659 sb->st_gid = ntohl(fp->fa.fa_gid);
660 sb->st_size = ntohl(fp->fa.fa_size);
666 nfs_readdir(struct open_file *f, struct dirent *d)
668 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
669 struct nfs_readdir_args *args;
670 struct nfs_readdir_data *rd;
671 struct nfs_readdir_off *roff = NULL;
673 static n_long cookie = 0;
678 n_long h[RPC_HEADER_WORDS];
679 struct nfs_readdir_args d;
682 n_long h[RPC_HEADER_WORDS];
683 u_char d[NFS_READDIRSIZE];
689 bzero(args, sizeof(*args));
691 bcopy(fp->fh, args->fh, NFS_FHSIZE);
692 args->cookie = htonl(cookie);
693 args->count = htonl(NFS_READDIRSIZE);
695 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
697 rdata.d, sizeof(rdata.d));
699 roff = (struct nfs_readdir_off *)buf;
700 if (ntohl(roff->cookie) != 0)
703 roff = (struct nfs_readdir_off *)buf;
705 if (ntohl(roff->follows) == 0) {
706 eof = ntohl((roff+1)->cookie);
714 buf += sizeof(struct nfs_readdir_off);
715 rd = (struct nfs_readdir_data *)buf;
716 d->d_namlen = ntohl(rd->len);
717 bcopy(rd->name, d->d_name, d->d_namlen);
718 d->d_name[d->d_namlen] = '\0';
720 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
721 roff = (struct nfs_readdir_off *)buf;
722 cookie = ntohl(roff->cookie);