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 #ifndef NFS_NOSYMLINK
391 struct nfs_iodesc *newfd;
392 struct nfsv2_fattrs *fa;
395 char namebuf[NFS_MAXPATHLEN + 1];
396 char linkbuf[NFS_MAXPATHLEN + 1];
404 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
407 printf("no rootpath, no nfs\n");
411 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
414 /* Bind to a reserved port. */
415 desc->myport = htons(--rpc_port);
416 desc->destip = rootip;
417 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
419 nfs_root_node.iodesc = desc;
421 #ifndef NFS_NOSYMLINK
422 /* Fake up attributes for the root dir. */
423 fa = &nfs_root_node.fa;
424 fa->fa_type = htonl(NFDIR);
425 fa->fa_mode = htonl(0755);
426 fa->fa_nlink = htonl(2);
428 currfd = &nfs_root_node;
431 cp = path = strdup(upath);
438 * Remove extra separators
446 * Check that current node is a directory.
448 if (currfd->fa.fa_type != htonl(NFDIR)) {
453 /* allocate file system specific data structure */
454 newfd = malloc(sizeof(*newfd));
455 newfd->iodesc = currfd->iodesc;
459 * Get next component of path name.
465 while ((c = *cp) != '\0' && c != '/') {
466 if (++len > NFS_MAXNAMLEN) {
475 /* lookup a file handle */
476 error = nfs_lookupfh(currfd, ncp, newfd);
482 * Check for symbolic link
484 if (newfd->fa.fa_type == htonl(NFLNK)) {
487 error = nfs_readlink(newfd, linkbuf);
491 link_len = strlen(linkbuf);
494 if (link_len + len > MAXPATHLEN
495 || ++nlinks > MAXSYMLINKS) {
500 bcopy(cp, &namebuf[link_len], len + 1);
501 bcopy(linkbuf, namebuf, link_len);
504 * If absolute pathname, restart at root.
505 * If relative pathname, restart at parent directory.
509 if (currfd != &nfs_root_node)
511 currfd = &nfs_root_node;
520 if (currfd != &nfs_root_node)
534 /* allocate file system specific data structure */
535 currfd = malloc(sizeof(*currfd));
536 currfd->iodesc = desc;
539 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
542 f->f_fsdata = (void *)currfd;
548 printf("nfs_open: %s lookupfh failed: %s\n",
549 path, strerror(error));
551 #ifndef NFS_NOSYMLINK
552 if (currfd != &nfs_root_node)
563 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
567 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
570 if (fp != &nfs_root_node && fp)
572 f->f_fsdata = (void *)0;
578 * read a portion of a file
581 nfs_read(f, buf, size, resid)
585 size_t *resid; /* out */
587 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
593 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
596 while ((int)size > 0) {
598 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
599 /* XXX maybe should retry on certain errors */
603 printf("nfs_read: read: %s", strerror(errno));
605 return (errno); /* XXX - from nfs_readdata */
610 printf("nfs_read: hit EOF unexpectantly");
629 nfs_write(f, buf, size, resid)
633 size_t *resid; /* out */
639 nfs_seek(f, offset, where)
644 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
645 n_long size = ntohl(d->fa.fa_size);
655 d->off = size - offset;
664 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
665 int nfs_stat_types[8] = {
666 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
673 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
676 ftype = ntohl(fp->fa.fa_type);
677 mode = ntohl(fp->fa.fa_mode);
678 mode |= nfs_stat_types[ftype & 7];
681 sb->st_nlink = ntohl(fp->fa.fa_nlink);
682 sb->st_uid = ntohl(fp->fa.fa_uid);
683 sb->st_gid = ntohl(fp->fa.fa_gid);
684 sb->st_size = ntohl(fp->fa.fa_size);
690 nfs_readdir(struct open_file *f, struct dirent *d)
692 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
693 struct nfs_readdir_args *args;
694 struct nfs_readdir_data *rd;
695 struct nfs_readdir_off *roff = NULL;
697 static n_long cookie = 0;
702 n_long h[RPC_HEADER_WORDS];
703 struct nfs_readdir_args d;
706 n_long h[RPC_HEADER_WORDS];
707 u_char d[NFS_READDIRSIZE];
713 bzero(args, sizeof(*args));
715 bcopy(fp->fh, args->fh, NFS_FHSIZE);
716 args->cookie = htonl(cookie);
717 args->count = htonl(NFS_READDIRSIZE);
719 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
721 rdata.d, sizeof(rdata.d));
723 roff = (struct nfs_readdir_off *)buf;
724 if (ntohl(roff->cookie) != 0)
727 roff = (struct nfs_readdir_off *)buf;
729 if (ntohl(roff->follows) == 0) {
730 eof = ntohl((roff+1)->cookie);
738 buf += sizeof(struct nfs_readdir_off);
739 rd = (struct nfs_readdir_data *)buf;
740 d->d_namlen = ntohl(rd->len);
741 bcopy(rd->name, d->d_name, d->d_namlen);
742 d->d_name[d->d_namlen] = '\0';
744 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
745 roff = (struct nfs_readdir_off *)buf;
746 cookie = ntohl(roff->cookie);