1 /* $FreeBSD: src/lib/libstand/nfs.c,v 1.2.6.3 2000/09/10 01:33:25 ps Exp $ */
2 /* $DragonFly: src/lib/libstand/nfs.c,v 1.2 2003/06/17 04:26:51 dillon Exp $ */
3 /* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
6 * Copyright (c) 1993 John Brezak
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/param.h>
35 #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? */
78 /* Data part of nfs rpc reply (also the largest thing we receive) */
79 #define NFSREAD_SIZE 1024
80 struct nfs_read_repl {
82 struct nfsv2_fattrs fa;
84 u_char data[NFSREAD_SIZE];
88 struct nfs_readlnk_repl {
91 char path[NFS_MAXPATHLEN];
95 struct nfs_readdir_args {
96 u_char fh[NFS_FHSIZE];
101 struct nfs_readdir_data {
107 struct nfs_readdir_off {
113 struct iodesc *iodesc;
115 u_char fh[NFS_FHSIZE];
116 struct nfsv2_fattrs fa; /* all in network order */
120 * XXX interactions with tftp? See nfswrapper.c for a confusing
123 int nfs_open(const char *path, struct open_file *f);
124 static int nfs_close(struct open_file *f);
125 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
126 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
127 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
128 static int nfs_stat(struct open_file *f, struct stat *sb);
129 static int nfs_readdir(struct open_file *f, struct dirent *d);
131 struct nfs_iodesc nfs_root_node;
133 struct fs_ops nfs_fsops = {
145 * Fetch the root file handle (call mount daemon)
146 * Return zero or error number.
149 nfs_getrootfh(d, path, fhp)
150 register struct iodesc *d;
157 char path[FNAME_SIZE];
161 u_char fh[NFS_FHSIZE];
164 n_long h[RPC_HEADER_WORDS];
168 n_long h[RPC_HEADER_WORDS];
175 printf("nfs_getrootfh: %s\n", path);
181 bzero(args, sizeof(*args));
183 if (len > sizeof(args->path))
184 len = sizeof(args->path);
185 args->len = htonl(len);
186 bcopy(path, args->path, len);
187 len = 4 + roundup(len, 4);
189 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
190 args, len, repl, sizeof(*repl));
192 /* errno was set by rpc_call */
198 return (ntohl(repl->errno));
199 bcopy(repl->fh, fhp, sizeof(repl->fh));
204 * Lookup a file. Store handle and attributes.
205 * Return zero or error number.
208 nfs_lookupfh(d, name, newfd)
209 struct nfs_iodesc *d;
211 struct nfs_iodesc *newfd;
213 register int len, rlen;
215 u_char fh[NFS_FHSIZE];
217 char name[FNAME_SIZE];
221 u_char fh[NFS_FHSIZE];
222 struct nfsv2_fattrs fa;
225 n_long h[RPC_HEADER_WORDS];
229 n_long h[RPC_HEADER_WORDS];
236 printf("lookupfh: called\n");
242 bzero(args, sizeof(*args));
243 bcopy(d->fh, args->fh, sizeof(args->fh));
245 if (len > sizeof(args->name))
246 len = sizeof(args->name);
247 bcopy(name, args->name, len);
248 args->len = htonl(len);
249 len = 4 + roundup(len, 4);
252 rlen = sizeof(*repl);
254 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
255 args, len, repl, rlen);
257 return (errno); /* XXX - from rpc_call */
261 /* saerrno.h now matches NFS error numbers. */
262 return (ntohl(repl->errno));
264 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
265 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
269 #ifndef NFS_NOSYMLINK
271 * Get the destination of a symbolic link.
275 struct nfs_iodesc *d;
279 n_long h[RPC_HEADER_WORDS];
280 u_char fh[NFS_FHSIZE];
283 n_long h[RPC_HEADER_WORDS];
284 struct nfs_readlnk_repl d;
290 printf("readlink: called\n");
293 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
294 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
295 sdata.fh, NFS_FHSIZE,
296 &rdata.d, sizeof(rdata.d));
304 return (ntohl(rdata.d.errno));
306 rdata.d.len = ntohl(rdata.d.len);
307 if (rdata.d.len > NFS_MAXPATHLEN)
308 return (ENAMETOOLONG);
310 bcopy(rdata.d.path, buf, rdata.d.len);
311 buf[rdata.d.len] = 0;
317 * Read data from a file.
318 * Return transfer count or -1 (and set errno)
321 nfs_readdata(d, off, addr, len)
322 struct nfs_iodesc *d;
327 struct nfs_read_args *args;
328 struct nfs_read_repl *repl;
330 n_long h[RPC_HEADER_WORDS];
331 struct nfs_read_args d;
334 n_long h[RPC_HEADER_WORDS];
335 struct nfs_read_repl d;
344 bcopy(d->fh, args->fh, NFS_FHSIZE);
345 args->off = htonl((n_long)off);
346 if (len > NFSREAD_SIZE)
348 args->len = htonl((n_long)len);
349 args->xxx = htonl((n_long)0);
350 hlen = sizeof(*repl) - NFSREAD_SIZE;
352 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
354 repl, sizeof(*repl));
356 /* errno was already set by rpc_call */
364 errno = ntohl(repl->errno);
368 x = ntohl(repl->count);
370 printf("nfsread: short packet, %d < %ld\n", rlen, x);
374 bcopy(repl->data, addr, x);
380 * return zero or error number
388 struct nfs_iodesc *currfd;
389 #ifndef NFS_NOSYMLINK
390 struct nfs_iodesc *newfd;
391 struct nfsv2_fattrs *fa;
392 register char *cp, *ncp;
394 char namebuf[NFS_MAXPATHLEN + 1];
395 char linkbuf[NFS_MAXPATHLEN + 1];
403 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
406 printf("no rootpath, no nfs\n");
410 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
413 /* Bind to a reserved port. */
414 desc->myport = htons(--rpc_port);
415 desc->destip = rootip;
416 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
418 nfs_root_node.iodesc = desc;
420 #ifndef NFS_NOSYMLINK
421 /* Fake up attributes for the root dir. */
422 fa = &nfs_root_node.fa;
423 fa->fa_type = htonl(NFDIR);
424 fa->fa_mode = htonl(0755);
425 fa->fa_nlink = htonl(2);
427 currfd = &nfs_root_node;
430 cp = path = strdup(upath);
437 * Remove extra separators
445 * Check that current node is a directory.
447 if (currfd->fa.fa_type != htonl(NFDIR)) {
452 /* allocate file system specific data structure */
453 newfd = malloc(sizeof(*newfd));
454 newfd->iodesc = currfd->iodesc;
458 * Get next component of path name.
461 register int len = 0;
464 while ((c = *cp) != '\0' && c != '/') {
465 if (++len > NFS_MAXNAMLEN) {
474 /* lookup a file handle */
475 error = nfs_lookupfh(currfd, ncp, newfd);
481 * Check for symbolic link
483 if (newfd->fa.fa_type == htonl(NFLNK)) {
486 error = nfs_readlink(newfd, linkbuf);
490 link_len = strlen(linkbuf);
493 if (link_len + len > MAXPATHLEN
494 || ++nlinks > MAXSYMLINKS) {
499 bcopy(cp, &namebuf[link_len], len + 1);
500 bcopy(linkbuf, namebuf, link_len);
503 * If absolute pathname, restart at root.
504 * If relative pathname, restart at parent directory.
508 if (currfd != &nfs_root_node)
510 currfd = &nfs_root_node;
519 if (currfd != &nfs_root_node)
533 /* allocate file system specific data structure */
534 currfd = malloc(sizeof(*currfd));
535 currfd->iodesc = desc;
538 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
541 f->f_fsdata = (void *)currfd;
547 printf("nfs_open: %s lookupfh failed: %s\n",
548 path, strerror(error));
550 #ifndef NFS_NOSYMLINK
551 if (currfd != &nfs_root_node)
562 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
566 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
569 if (fp != &nfs_root_node && fp)
571 f->f_fsdata = (void *)0;
577 * read a portion of a file
580 nfs_read(f, buf, size, resid)
584 size_t *resid; /* out */
586 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
588 register char *addr = buf;
592 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
595 while ((int)size > 0) {
597 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
598 /* XXX maybe should retry on certain errors */
602 printf("nfs_read: read: %s", strerror(errno));
604 return (errno); /* XXX - from nfs_readdata */
609 printf("nfs_read: hit EOF unexpectantly");
628 nfs_write(f, buf, size, resid)
632 size_t *resid; /* out */
638 nfs_seek(f, offset, where)
643 register struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
644 n_long size = ntohl(d->fa.fa_size);
654 d->off = size - offset;
663 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
664 int nfs_stat_types[8] = {
665 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
672 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
673 register n_long ftype, mode;
675 ftype = ntohl(fp->fa.fa_type);
676 mode = ntohl(fp->fa.fa_mode);
677 mode |= nfs_stat_types[ftype & 7];
680 sb->st_nlink = ntohl(fp->fa.fa_nlink);
681 sb->st_uid = ntohl(fp->fa.fa_uid);
682 sb->st_gid = ntohl(fp->fa.fa_gid);
683 sb->st_size = ntohl(fp->fa.fa_size);
689 nfs_readdir(struct open_file *f, struct dirent *d)
691 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
692 struct nfs_readdir_args *args;
693 struct nfs_readdir_data *rd;
694 struct nfs_readdir_off *roff = NULL;
696 static n_long cookie = 0;
701 n_long h[RPC_HEADER_WORDS];
702 struct nfs_readdir_args d;
705 n_long h[RPC_HEADER_WORDS];
706 u_char d[NFS_READDIRSIZE];
712 bzero(args, sizeof(*args));
714 bcopy(fp->fh, args->fh, NFS_FHSIZE);
715 args->cookie = htonl(cookie);
716 args->count = htonl(NFS_READDIRSIZE);
718 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
720 rdata.d, sizeof(rdata.d));
722 roff = (struct nfs_readdir_off *)buf;
723 if (ntohl(roff->cookie) != 0)
726 roff = (struct nfs_readdir_off *)buf;
728 if (ntohl(roff->follows) == 0) {
729 eof = ntohl((roff+1)->cookie);
737 buf += sizeof(struct nfs_readdir_off);
738 rd = (struct nfs_readdir_data *)buf;
739 d->d_namlen = ntohl(rd->len);
740 bcopy(rd->name, d->d_name, d->d_namlen);
741 d->d_name[d->d_namlen] = '\0';
743 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
744 roff = (struct nfs_readdir_off *)buf;
745 cookie = ntohl(roff->cookie);