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/param.h>
33 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
50 /* Define our own NFS attributes without NQNFS stuff. */
63 struct nfsv2_time fa_atime;
64 struct nfsv2_time fa_mtime;
65 struct nfsv2_time fa_ctime;
69 struct nfs_read_args {
70 u_char fh[NFS_FHSIZE];
73 n_long xxx; /* XXX what's this for? */
76 /* Data part of nfs rpc reply (also the largest thing we receive) */
77 #define NFSREAD_SIZE 1024
78 struct nfs_read_repl {
80 struct nfsv2_fattrs fa;
82 u_char data[NFSREAD_SIZE];
86 struct nfs_readlnk_repl {
89 char path[NFS_MAXPATHLEN];
94 struct iodesc *iodesc;
96 u_char fh[NFS_FHSIZE];
97 struct nfsv2_fattrs fa; /* all in network order */
101 * XXX interactions with tftp? See nfswrapper.c for a confusing
104 int nfs_open(const char *path, struct open_file *f);
105 static int nfs_close(struct open_file *f);
106 static int nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
107 static int nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
108 static off_t nfs_seek(struct open_file *f, off_t offset, int where);
109 static int nfs_stat(struct open_file *f, struct stat *sb);
111 struct fs_ops nfs_fsops = {
112 "nfs", nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
117 * Fetch the root file handle (call mount daemon)
118 * Return zero or error number.
121 nfs_getrootfh(d, path, fhp)
122 register struct iodesc *d;
129 char path[FNAME_SIZE];
133 u_char fh[NFS_FHSIZE];
136 n_long h[RPC_HEADER_WORDS];
140 n_long h[RPC_HEADER_WORDS];
147 printf("nfs_getrootfh: %s\n", path);
153 bzero(args, sizeof(*args));
155 if (len > sizeof(args->path))
156 len = sizeof(args->path);
157 args->len = htonl(len);
158 bcopy(path, args->path, len);
159 len = 4 + roundup(len, 4);
161 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
162 args, len, repl, sizeof(*repl));
164 /* errno was set by rpc_call */
170 return (ntohl(repl->errno));
171 bcopy(repl->fh, fhp, sizeof(repl->fh));
176 * Lookup a file. Store handle and attributes.
177 * Return zero or error number.
180 nfs_lookupfh(d, name, newfd)
181 struct nfs_iodesc *d;
183 struct nfs_iodesc *newfd;
185 register int len, rlen;
187 u_char fh[NFS_FHSIZE];
189 char name[FNAME_SIZE];
193 u_char fh[NFS_FHSIZE];
194 struct nfsv2_fattrs fa;
197 n_long h[RPC_HEADER_WORDS];
201 n_long h[RPC_HEADER_WORDS];
208 printf("lookupfh: called\n");
214 bzero(args, sizeof(*args));
215 bcopy(d->fh, args->fh, sizeof(args->fh));
217 if (len > sizeof(args->name))
218 len = sizeof(args->name);
219 bcopy(name, args->name, len);
220 args->len = htonl(len);
221 len = 4 + roundup(len, 4);
224 rlen = sizeof(*repl);
226 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
227 args, len, repl, rlen);
229 return (errno); /* XXX - from rpc_call */
233 /* saerrno.h now matches NFS error numbers. */
234 return (ntohl(repl->errno));
236 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
237 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
241 #ifndef NFS_NOSYMLINK
243 * Get the destination of a symbolic link.
247 struct nfs_iodesc *d;
251 n_long h[RPC_HEADER_WORDS];
252 u_char fh[NFS_FHSIZE];
255 n_long h[RPC_HEADER_WORDS];
256 struct nfs_readlnk_repl d;
262 printf("readlink: called\n");
265 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
266 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
267 sdata.fh, NFS_FHSIZE,
268 &rdata.d, sizeof(rdata.d));
276 return (ntohl(rdata.d.errno));
278 rdata.d.len = ntohl(rdata.d.len);
279 if (rdata.d.len > NFS_MAXPATHLEN)
280 return (ENAMETOOLONG);
282 bcopy(rdata.d.path, buf, rdata.d.len);
283 buf[rdata.d.len] = 0;
289 * Read data from a file.
290 * Return transfer count or -1 (and set errno)
293 nfs_readdata(d, off, addr, len)
294 struct nfs_iodesc *d;
299 struct nfs_read_args *args;
300 struct nfs_read_repl *repl;
302 n_long h[RPC_HEADER_WORDS];
303 struct nfs_read_args d;
306 n_long h[RPC_HEADER_WORDS];
307 struct nfs_read_repl d;
316 bcopy(d->fh, args->fh, NFS_FHSIZE);
317 args->off = htonl((n_long)off);
318 if (len > NFSREAD_SIZE)
320 args->len = htonl((n_long)len);
321 args->xxx = htonl((n_long)0);
322 hlen = sizeof(*repl) - NFSREAD_SIZE;
324 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
326 repl, sizeof(*repl));
328 /* errno was already set by rpc_call */
336 errno = ntohl(repl->errno);
340 x = ntohl(repl->count);
342 printf("nfsread: short packet, %d < %ld\n", rlen, x);
346 bcopy(repl->data, addr, x);
352 * return zero or error number
359 static struct nfs_iodesc nfs_root_node;
361 struct nfs_iodesc *currfd;
362 #ifndef NFS_NOSYMLINK
363 struct nfs_iodesc *newfd;
364 struct nfsv2_fattrs *fa;
365 register char *cp, *ncp;
367 char namebuf[NFS_MAXPATHLEN + 1];
368 char linkbuf[NFS_MAXPATHLEN + 1];
376 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
379 printf("no rootpath, no nfs\n");
383 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
386 /* Bind to a reserved port. */
387 desc->myport = htons(--rpc_port);
388 desc->destip = rootip;
389 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
391 nfs_root_node.iodesc = desc;
393 #ifndef NFS_NOSYMLINK
394 /* Fake up attributes for the root dir. */
395 fa = &nfs_root_node.fa;
396 fa->fa_type = htonl(NFDIR);
397 fa->fa_mode = htonl(0755);
398 fa->fa_nlink = htonl(2);
400 currfd = &nfs_root_node;
403 cp = path = strdup(upath);
410 * Remove extra separators
418 * Check that current node is a directory.
420 if (currfd->fa.fa_type != htonl(NFDIR)) {
425 /* allocate file system specific data structure */
426 newfd = malloc(sizeof(*newfd));
427 newfd->iodesc = currfd->iodesc;
431 * Get next component of path name.
434 register int len = 0;
437 while ((c = *cp) != '\0' && c != '/') {
438 if (++len > NFS_MAXNAMLEN) {
447 /* lookup a file handle */
448 error = nfs_lookupfh(currfd, ncp, newfd);
454 * Check for symbolic link
456 if (newfd->fa.fa_type == htonl(NFLNK)) {
459 error = nfs_readlink(newfd, linkbuf);
463 link_len = strlen(linkbuf);
466 if (link_len + len > MAXPATHLEN
467 || ++nlinks > MAXSYMLINKS) {
472 bcopy(cp, &namebuf[link_len], len + 1);
473 bcopy(linkbuf, namebuf, link_len);
476 * If absolute pathname, restart at root.
477 * If relative pathname, restart at parent directory.
481 if (currfd != &nfs_root_node)
483 currfd = &nfs_root_node;
492 if (currfd != &nfs_root_node)
506 /* allocate file system specific data structure */
507 currfd = malloc(sizeof(*currfd));
508 currfd->iodesc = desc;
511 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
514 f->f_fsdata = (void *)currfd;
520 printf("nfs_open: %s lookupfh failed: %s\n",
521 path, strerror(error));
523 #ifndef NFS_NOSYMLINK
524 if (currfd != &nfs_root_node)
535 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
539 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
544 f->f_fsdata = (void *)0;
550 * read a portion of a file
553 nfs_read(f, buf, size, resid)
557 size_t *resid; /* out */
559 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
561 register char *addr = buf;
565 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
568 while ((int)size > 0) {
570 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
571 /* XXX maybe should retry on certain errors */
575 printf("nfs_read: read: %s", strerror(errno));
577 return (errno); /* XXX - from nfs_readdata */
582 printf("nfs_read: hit EOF unexpectantly");
601 nfs_write(f, buf, size, resid)
605 size_t *resid; /* out */
611 nfs_seek(f, offset, where)
616 register struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
617 n_long size = ntohl(d->fa.fa_size);
627 d->off = size - offset;
636 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
637 int nfs_stat_types[8] = {
638 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
645 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
646 register n_long ftype, mode;
648 ftype = ntohl(fp->fa.fa_type);
649 mode = ntohl(fp->fa.fa_mode);
650 mode |= nfs_stat_types[ftype & 7];
653 sb->st_nlink = ntohl(fp->fa.fa_nlink);
654 sb->st_uid = ntohl(fp->fa.fa_uid);
655 sb->st_gid = ntohl(fp->fa.fa_gid);
656 sb->st_size = ntohl(fp->fa.fa_size);