priv: Use PRIV_NETINET_RAW
[dragonfly.git] / lib / libstand / nfs.c
blob1ee2fd3c064d0f5d2c4e6241acc3dd0b016a3370
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.4 2005/12/11 02:27:26 swildner Exp $ */
3 /* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
5 /*-
6 * Copyright (c) 1993 John Brezak
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <string.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
42 #include "rpcv2.h"
43 #include "nfsv2.h"
45 #include "stand.h"
46 #include "net.h"
47 #include "netif.h"
48 #include "rpc.h"
50 #define NFS_DEBUGxx
52 /* Define our own NFS attributes without NQNFS stuff. */
53 struct nfsv2_fattrs {
54 n_long fa_type;
55 n_long fa_mode;
56 n_long fa_nlink;
57 n_long fa_uid;
58 n_long fa_gid;
59 n_long fa_size;
60 n_long fa_blocksize;
61 n_long fa_rdev;
62 n_long fa_blocks;
63 n_long fa_fsid;
64 n_long fa_fileid;
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];
73 n_long off;
74 n_long len;
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 {
81 n_long errno;
82 struct nfsv2_fattrs fa;
83 n_long count;
84 u_char data[NFSREAD_SIZE];
87 #ifndef NFS_NOSYMLINK
88 struct nfs_readlnk_repl {
89 n_long errno;
90 n_long len;
91 char path[NFS_MAXPATHLEN];
93 #endif
95 struct nfs_readdir_args {
96 u_char fh[NFS_FHSIZE];
97 n_long cookie;
98 n_long count;
101 struct nfs_readdir_data {
102 n_long fileid;
103 n_long len;
104 char name[0];
107 struct nfs_readdir_off {
108 n_long cookie;
109 n_long follows;
112 struct nfs_iodesc {
113 struct iodesc *iodesc;
114 off_t off;
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
121 * issue.
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 = {
134 "nfs",
135 nfs_open,
136 nfs_close,
137 nfs_read,
138 nfs_write,
139 nfs_seek,
140 nfs_stat,
141 nfs_readdir
145 * Fetch the root file handle (call mount daemon)
146 * Return zero or error number.
149 nfs_getrootfh(struct iodesc *d, char *path, u_char *fhp)
151 int len;
152 struct args {
153 n_long len;
154 char path[FNAME_SIZE];
155 } *args;
156 struct repl {
157 n_long errno;
158 u_char fh[NFS_FHSIZE];
159 } *repl;
160 struct {
161 n_long h[RPC_HEADER_WORDS];
162 struct args d;
163 } sdata;
164 struct {
165 n_long h[RPC_HEADER_WORDS];
166 struct repl d;
167 } rdata;
168 size_t cc;
170 #ifdef NFS_DEBUG
171 if (debug)
172 printf("nfs_getrootfh: %s\n", path);
173 #endif
175 args = &sdata.d;
176 repl = &rdata.d;
178 bzero(args, sizeof(*args));
179 len = strlen(path);
180 if (len > sizeof(args->path))
181 len = sizeof(args->path);
182 args->len = htonl(len);
183 bcopy(path, args->path, len);
184 len = 4 + roundup(len, 4);
186 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
187 args, len, repl, sizeof(*repl));
188 if (cc == -1) {
189 /* errno was set by rpc_call */
190 return (errno);
192 if (cc < 4)
193 return (EBADRPC);
194 if (repl->errno)
195 return (ntohl(repl->errno));
196 bcopy(repl->fh, fhp, sizeof(repl->fh));
197 return (0);
201 * Lookup a file. Store handle and attributes.
202 * Return zero or error number.
205 nfs_lookupfh(struct nfs_iodesc *d, const char *name, struct nfs_iodesc *newfd)
207 int len, rlen;
208 struct args {
209 u_char fh[NFS_FHSIZE];
210 n_long len;
211 char name[FNAME_SIZE];
212 } *args;
213 struct repl {
214 n_long errno;
215 u_char fh[NFS_FHSIZE];
216 struct nfsv2_fattrs fa;
217 } *repl;
218 struct {
219 n_long h[RPC_HEADER_WORDS];
220 struct args d;
221 } sdata;
222 struct {
223 n_long h[RPC_HEADER_WORDS];
224 struct repl d;
225 } rdata;
226 ssize_t cc;
228 #ifdef NFS_DEBUG
229 if (debug)
230 printf("lookupfh: called\n");
231 #endif
233 args = &sdata.d;
234 repl = &rdata.d;
236 bzero(args, sizeof(*args));
237 bcopy(d->fh, args->fh, sizeof(args->fh));
238 len = strlen(name);
239 if (len > sizeof(args->name))
240 len = sizeof(args->name);
241 bcopy(name, args->name, len);
242 args->len = htonl(len);
243 len = 4 + roundup(len, 4);
244 len += NFS_FHSIZE;
246 rlen = sizeof(*repl);
248 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
249 args, len, repl, rlen);
250 if (cc == -1)
251 return (errno); /* XXX - from rpc_call */
252 if (cc < 4)
253 return (EIO);
254 if (repl->errno) {
255 /* saerrno.h now matches NFS error numbers. */
256 return (ntohl(repl->errno));
258 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
259 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
260 return (0);
263 #ifndef NFS_NOSYMLINK
265 * Get the destination of a symbolic link.
268 nfs_readlink(struct nfs_iodesc *d, char *buf)
270 struct {
271 n_long h[RPC_HEADER_WORDS];
272 u_char fh[NFS_FHSIZE];
273 } sdata;
274 struct {
275 n_long h[RPC_HEADER_WORDS];
276 struct nfs_readlnk_repl d;
277 } rdata;
278 ssize_t cc;
280 #ifdef NFS_DEBUG
281 if (debug)
282 printf("readlink: called\n");
283 #endif
285 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
286 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
287 sdata.fh, NFS_FHSIZE,
288 &rdata.d, sizeof(rdata.d));
289 if (cc == -1)
290 return (errno);
292 if (cc < 4)
293 return (EIO);
295 if (rdata.d.errno)
296 return (ntohl(rdata.d.errno));
298 rdata.d.len = ntohl(rdata.d.len);
299 if (rdata.d.len > NFS_MAXPATHLEN)
300 return (ENAMETOOLONG);
302 bcopy(rdata.d.path, buf, rdata.d.len);
303 buf[rdata.d.len] = 0;
304 return (0);
306 #endif
309 * Read data from a file.
310 * Return transfer count or -1 (and set errno)
312 ssize_t
313 nfs_readdata(struct nfs_iodesc *d, off_t off, void *addr, size_t len)
315 struct nfs_read_args *args;
316 struct nfs_read_repl *repl;
317 struct {
318 n_long h[RPC_HEADER_WORDS];
319 struct nfs_read_args d;
320 } sdata;
321 struct {
322 n_long h[RPC_HEADER_WORDS];
323 struct nfs_read_repl d;
324 } rdata;
325 size_t cc;
326 long x;
327 int hlen, rlen;
329 args = &sdata.d;
330 repl = &rdata.d;
332 bcopy(d->fh, args->fh, NFS_FHSIZE);
333 args->off = htonl((n_long)off);
334 if (len > NFSREAD_SIZE)
335 len = NFSREAD_SIZE;
336 args->len = htonl((n_long)len);
337 args->xxx = htonl((n_long)0);
338 hlen = sizeof(*repl) - NFSREAD_SIZE;
340 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
341 args, sizeof(*args),
342 repl, sizeof(*repl));
343 if (cc == -1) {
344 /* errno was already set by rpc_call */
345 return (-1);
347 if (cc < hlen) {
348 errno = EBADRPC;
349 return (-1);
351 if (repl->errno) {
352 errno = ntohl(repl->errno);
353 return (-1);
355 rlen = cc - hlen;
356 x = ntohl(repl->count);
357 if (rlen < x) {
358 printf("nfsread: short packet, %d < %ld\n", rlen, x);
359 errno = EBADRPC;
360 return(-1);
362 bcopy(repl->data, addr, x);
363 return (x);
367 * Open a file.
368 * return zero or error number
371 nfs_open(const char *upath, struct open_file *f)
373 struct iodesc *desc;
374 struct nfs_iodesc *currfd;
375 #ifndef NFS_NOSYMLINK
376 struct nfs_iodesc *newfd;
377 struct nfsv2_fattrs *fa;
378 char *cp, *ncp;
379 int c;
380 char namebuf[NFS_MAXPATHLEN + 1];
381 char linkbuf[NFS_MAXPATHLEN + 1];
382 int nlinks = 0;
383 #endif
384 int error;
385 char *path;
387 #ifdef NFS_DEBUG
388 if (debug)
389 printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
390 #endif
391 if (!rootpath[0]) {
392 printf("no rootpath, no nfs\n");
393 return (ENXIO);
396 if (!(desc = socktodesc(*(int *)(f->f_devdata))))
397 return(EINVAL);
399 /* Bind to a reserved port. */
400 desc->myport = htons(--rpc_port);
401 desc->destip = rootip;
402 if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
403 return (error);
404 nfs_root_node.iodesc = desc;
406 #ifndef NFS_NOSYMLINK
407 /* Fake up attributes for the root dir. */
408 fa = &nfs_root_node.fa;
409 fa->fa_type = htonl(NFDIR);
410 fa->fa_mode = htonl(0755);
411 fa->fa_nlink = htonl(2);
413 currfd = &nfs_root_node;
414 newfd = 0;
416 cp = path = strdup(upath);
417 if (path == NULL) {
418 error = ENOMEM;
419 goto out;
421 while (*cp) {
423 * Remove extra separators
425 while (*cp == '/')
426 cp++;
428 if (*cp == '\0')
429 break;
431 * Check that current node is a directory.
433 if (currfd->fa.fa_type != htonl(NFDIR)) {
434 error = ENOTDIR;
435 goto out;
438 /* allocate file system specific data structure */
439 newfd = malloc(sizeof(*newfd));
440 newfd->iodesc = currfd->iodesc;
441 newfd->off = 0;
444 * Get next component of path name.
447 int len = 0;
449 ncp = cp;
450 while ((c = *cp) != '\0' && c != '/') {
451 if (++len > NFS_MAXNAMLEN) {
452 error = ENOENT;
453 goto out;
455 cp++;
457 *cp = '\0';
460 /* lookup a file handle */
461 error = nfs_lookupfh(currfd, ncp, newfd);
462 *cp = c;
463 if (error)
464 goto out;
467 * Check for symbolic link
469 if (newfd->fa.fa_type == htonl(NFLNK)) {
470 int link_len, len;
472 error = nfs_readlink(newfd, linkbuf);
473 if (error)
474 goto out;
476 link_len = strlen(linkbuf);
477 len = strlen(cp);
479 if (link_len + len > MAXPATHLEN
480 || ++nlinks > MAXSYMLINKS) {
481 error = ENOENT;
482 goto out;
485 bcopy(cp, &namebuf[link_len], len + 1);
486 bcopy(linkbuf, namebuf, link_len);
489 * If absolute pathname, restart at root.
490 * If relative pathname, restart at parent directory.
492 cp = namebuf;
493 if (*cp == '/') {
494 if (currfd != &nfs_root_node)
495 free(currfd);
496 currfd = &nfs_root_node;
499 free(newfd);
500 newfd = 0;
502 continue;
505 if (currfd != &nfs_root_node)
506 free(currfd);
507 currfd = newfd;
508 newfd = 0;
511 error = 0;
513 out:
514 if (newfd)
515 free(newfd);
516 if (path)
517 free(path);
518 #else
519 /* allocate file system specific data structure */
520 currfd = malloc(sizeof(*currfd));
521 currfd->iodesc = desc;
522 currfd->off = 0;
524 error = nfs_lookupfh(&nfs_root_node, upath, currfd);
525 #endif
526 if (!error) {
527 f->f_fsdata = (void *)currfd;
528 return (0);
531 #ifdef NFS_DEBUG
532 if (debug)
533 printf("nfs_open: %s lookupfh failed: %s\n",
534 path, strerror(error));
535 #endif
536 #ifndef NFS_NOSYMLINK
537 if (currfd != &nfs_root_node)
538 #endif
539 free(currfd);
541 return (error);
545 nfs_close(struct open_file *f)
547 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
549 #ifdef NFS_DEBUG
550 if (debug)
551 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
552 #endif
554 f->f_fsdata = NULL;
555 if (fp && fp != &nfs_root_node)
556 free(fp);
558 return (0);
562 * read a portion of a file
564 * Parameters:
565 * resid: out
568 nfs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
570 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
571 ssize_t cc;
572 char *addr = buf;
574 #ifdef NFS_DEBUG
575 if (debug)
576 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
577 (int)fp->off);
578 #endif
579 while ((int)size > 0) {
580 twiddle();
581 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
582 /* XXX maybe should retry on certain errors */
583 if (cc == -1) {
584 #ifdef NFS_DEBUG
585 if (debug)
586 printf("nfs_read: read: %s", strerror(errno));
587 #endif
588 return (errno); /* XXX - from nfs_readdata */
590 if (cc == 0) {
591 #ifdef NFS_DEBUG
592 if (debug)
593 printf("nfs_read: hit EOF unexpectantly");
594 #endif
595 goto ret;
597 fp->off += cc;
598 addr += cc;
599 size -= cc;
601 ret:
602 if (resid)
603 *resid = size;
605 return (0);
609 * Not implemented.
611 * Parameters:
612 * resid: out
615 nfs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
617 return (EROFS);
620 off_t
621 nfs_seek(struct open_file *f, off_t offset, int where)
623 struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
624 n_long size = ntohl(d->fa.fa_size);
626 switch (where) {
627 case SEEK_SET:
628 d->off = offset;
629 break;
630 case SEEK_CUR:
631 d->off += offset;
632 break;
633 case SEEK_END:
634 d->off = size - offset;
635 break;
636 default:
637 return (-1);
640 return (d->off);
643 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
644 int nfs_stat_types[8] = {
645 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
648 nfs_stat(struct open_file *f, struct stat *sb)
650 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
651 n_long ftype, mode;
653 ftype = ntohl(fp->fa.fa_type);
654 mode = ntohl(fp->fa.fa_mode);
655 mode |= nfs_stat_types[ftype & 7];
657 sb->st_mode = mode;
658 sb->st_nlink = ntohl(fp->fa.fa_nlink);
659 sb->st_uid = ntohl(fp->fa.fa_uid);
660 sb->st_gid = ntohl(fp->fa.fa_gid);
661 sb->st_size = ntohl(fp->fa.fa_size);
663 return (0);
666 static int
667 nfs_readdir(struct open_file *f, struct dirent *d)
669 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
670 struct nfs_readdir_args *args;
671 struct nfs_readdir_data *rd;
672 struct nfs_readdir_off *roff = NULL;
673 static char *buf;
674 static n_long cookie = 0;
675 size_t cc;
676 n_long eof;
678 struct {
679 n_long h[RPC_HEADER_WORDS];
680 struct nfs_readdir_args d;
681 } sdata;
682 static struct {
683 n_long h[RPC_HEADER_WORDS];
684 u_char d[NFS_READDIRSIZE];
685 } rdata;
687 if (cookie == 0) {
688 refill:
689 args = &sdata.d;
690 bzero(args, sizeof(*args));
692 bcopy(fp->fh, args->fh, NFS_FHSIZE);
693 args->cookie = htonl(cookie);
694 args->count = htonl(NFS_READDIRSIZE);
696 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
697 args, sizeof(*args),
698 rdata.d, sizeof(rdata.d));
699 buf = rdata.d;
700 roff = (struct nfs_readdir_off *)buf;
701 if (ntohl(roff->cookie) != 0)
702 return 1;
704 roff = (struct nfs_readdir_off *)buf;
706 if (ntohl(roff->follows) == 0) {
707 eof = ntohl((roff+1)->cookie);
708 if (eof) {
709 cookie = 0;
710 return 1;
712 goto refill;
715 buf += sizeof(struct nfs_readdir_off);
716 rd = (struct nfs_readdir_data *)buf;
717 d->d_namlen = ntohl(rd->len);
718 bcopy(rd->name, d->d_name, d->d_namlen);
719 d->d_name[d->d_namlen] = '\0';
721 buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
722 roff = (struct nfs_readdir_off *)buf;
723 cookie = ntohl(roff->cookie);
724 return 0;