]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/nfs.c
This commit was generated by cvs2svn to compensate for changes in r102786,
[FreeBSD/FreeBSD.git] / lib / libstand / nfs.c
1 /*      $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
2
3 /*-
4  *  Copyright (c) 1993 John Brezak
5  *  All rights reserved.
6  * 
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
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.
17  * 
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.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <string.h>
39
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42
43 #include "rpcv2.h"
44 #include "nfsv2.h"
45
46 #include "stand.h"
47 #include "net.h"
48 #include "netif.h"
49 #include "rpc.h"
50
51 #define NFS_DEBUGxx
52
53 /* Define our own NFS attributes without NQNFS stuff. */
54 struct nfsv2_fattrs {
55         n_long  fa_type;
56         n_long  fa_mode;
57         n_long  fa_nlink;
58         n_long  fa_uid;
59         n_long  fa_gid;
60         n_long  fa_size;
61         n_long  fa_blocksize;
62         n_long  fa_rdev;
63         n_long  fa_blocks;
64         n_long  fa_fsid;
65         n_long  fa_fileid;
66         struct nfsv2_time fa_atime;
67         struct nfsv2_time fa_mtime;
68         struct nfsv2_time fa_ctime;
69 };
70
71
72 struct nfs_read_args {
73         u_char  fh[NFS_FHSIZE];
74         n_long  off;
75         n_long  len;
76         n_long  xxx;                    /* XXX what's this for? */
77 };
78
79 /* Data part of nfs rpc reply (also the largest thing we receive) */
80 #define NFSREAD_SIZE 1024
81 struct nfs_read_repl {
82         n_long  errno;
83         struct  nfsv2_fattrs fa;
84         n_long  count;
85         u_char  data[NFSREAD_SIZE];
86 };
87
88 #ifndef NFS_NOSYMLINK
89 struct nfs_readlnk_repl {
90         n_long  errno;
91         n_long  len;
92         char    path[NFS_MAXPATHLEN];
93 };
94 #endif
95
96 struct nfs_readdir_args {
97         u_char  fh[NFS_FHSIZE];
98         n_long  cookie;
99         n_long  count;
100 };
101
102 struct nfs_readdir_data {
103         n_long  fileid;
104         n_long  len;
105         char    name[0];
106 };
107
108 struct nfs_readdir_off {
109         n_long  cookie;
110         n_long  follows;
111 };
112
113 struct nfs_iodesc {
114         struct  iodesc  *iodesc;
115         off_t   off;
116         u_char  fh[NFS_FHSIZE];
117         struct nfsv2_fattrs fa; /* all in network order */
118 };
119
120 /*
121  * XXX interactions with tftp? See nfswrapper.c for a confusing
122  *     issue.
123  */
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);
131
132 struct  nfs_iodesc nfs_root_node;
133
134 struct fs_ops nfs_fsops = {
135         "nfs",
136         nfs_open,
137         nfs_close,
138         nfs_read,
139         nfs_write,
140         nfs_seek,
141         nfs_stat,
142         nfs_readdir
143 };
144
145 /*
146  * Fetch the root file handle (call mount daemon)
147  * Return zero or error number.
148  */
149 int
150 nfs_getrootfh(d, path, fhp)
151         struct iodesc *d;
152         char *path;
153         u_char *fhp;
154 {
155         int len;
156         struct args {
157                 n_long  len;
158                 char    path[FNAME_SIZE];
159         } *args;
160         struct repl {
161                 n_long  errno;
162                 u_char  fh[NFS_FHSIZE];
163         } *repl;
164         struct {
165                 n_long  h[RPC_HEADER_WORDS];
166                 struct args d;
167         } sdata;
168         struct {
169                 n_long  h[RPC_HEADER_WORDS];
170                 struct repl d;
171         } rdata;
172         size_t cc;
173         
174 #ifdef NFS_DEBUG
175         if (debug)
176                 printf("nfs_getrootfh: %s\n", path);
177 #endif
178
179         args = &sdata.d;
180         repl = &rdata.d;
181
182         bzero(args, sizeof(*args));
183         len = strlen(path);
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);
189
190         cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
191             args, len, repl, sizeof(*repl));
192         if (cc == -1) {
193                 /* errno was set by rpc_call */
194                 return (errno);
195         }
196         if (cc < 4)
197                 return (EBADRPC);
198         if (repl->errno)
199                 return (ntohl(repl->errno));
200         bcopy(repl->fh, fhp, sizeof(repl->fh));
201         return (0);
202 }
203
204 /*
205  * Lookup a file.  Store handle and attributes.
206  * Return zero or error number.
207  */
208 int
209 nfs_lookupfh(d, name, newfd)
210         struct nfs_iodesc *d;
211         const char *name;
212         struct nfs_iodesc *newfd;
213 {
214         int len, rlen;
215         struct args {
216                 u_char  fh[NFS_FHSIZE];
217                 n_long  len;
218                 char    name[FNAME_SIZE];
219         } *args;
220         struct repl {
221                 n_long  errno;
222                 u_char  fh[NFS_FHSIZE];
223                 struct  nfsv2_fattrs fa;
224         } *repl;
225         struct {
226                 n_long  h[RPC_HEADER_WORDS];
227                 struct args d;
228         } sdata;
229         struct {
230                 n_long  h[RPC_HEADER_WORDS];
231                 struct repl d;
232         } rdata;
233         ssize_t cc;
234         
235 #ifdef NFS_DEBUG
236         if (debug)
237                 printf("lookupfh: called\n");
238 #endif
239
240         args = &sdata.d;
241         repl = &rdata.d;
242
243         bzero(args, sizeof(*args));
244         bcopy(d->fh, args->fh, sizeof(args->fh));
245         len = strlen(name);
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);
251         len += NFS_FHSIZE;
252
253         rlen = sizeof(*repl);
254
255         cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
256             args, len, repl, rlen);
257         if (cc == -1)
258                 return (errno);         /* XXX - from rpc_call */
259         if (cc < 4)
260                 return (EIO);
261         if (repl->errno) {
262                 /* saerrno.h now matches NFS error numbers. */
263                 return (ntohl(repl->errno));
264         }
265         bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
266         bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
267         return (0);
268 }
269
270 #ifndef NFS_NOSYMLINK
271 /*
272  * Get the destination of a symbolic link.
273  */
274 int
275 nfs_readlink(d, buf)
276         struct nfs_iodesc *d;
277         char *buf;
278 {
279         struct {
280                 n_long  h[RPC_HEADER_WORDS];
281                 u_char fh[NFS_FHSIZE];
282         } sdata;
283         struct {
284                 n_long  h[RPC_HEADER_WORDS];
285                 struct nfs_readlnk_repl d;
286         } rdata;
287         ssize_t cc;
288
289 #ifdef NFS_DEBUG
290         if (debug)
291                 printf("readlink: called\n");
292 #endif
293
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));
298         if (cc == -1)
299                 return (errno);
300
301         if (cc < 4)
302                 return (EIO);
303         
304         if (rdata.d.errno)
305                 return (ntohl(rdata.d.errno));
306
307         rdata.d.len = ntohl(rdata.d.len);
308         if (rdata.d.len > NFS_MAXPATHLEN)
309                 return (ENAMETOOLONG);
310
311         bcopy(rdata.d.path, buf, rdata.d.len);
312         buf[rdata.d.len] = 0;
313         return (0);
314 }
315 #endif
316
317 /*
318  * Read data from a file.
319  * Return transfer count or -1 (and set errno)
320  */
321 ssize_t
322 nfs_readdata(d, off, addr, len)
323         struct nfs_iodesc *d;
324         off_t off;
325         void *addr;
326         size_t len;
327 {
328         struct nfs_read_args *args;
329         struct nfs_read_repl *repl;
330         struct {
331                 n_long  h[RPC_HEADER_WORDS];
332                 struct nfs_read_args d;
333         } sdata;
334         struct {
335                 n_long  h[RPC_HEADER_WORDS];
336                 struct nfs_read_repl d;
337         } rdata;
338         size_t cc;
339         long x;
340         int hlen, rlen;
341
342         args = &sdata.d;
343         repl = &rdata.d;
344
345         bcopy(d->fh, args->fh, NFS_FHSIZE);
346         args->off = htonl((n_long)off);
347         if (len > NFSREAD_SIZE)
348                 len = NFSREAD_SIZE;
349         args->len = htonl((n_long)len);
350         args->xxx = htonl((n_long)0);
351         hlen = sizeof(*repl) - NFSREAD_SIZE;
352
353         cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
354             args, sizeof(*args),
355             repl, sizeof(*repl));
356         if (cc == -1) {
357                 /* errno was already set by rpc_call */
358                 return (-1);
359         }
360         if (cc < hlen) {
361                 errno = EBADRPC;
362                 return (-1);
363         }
364         if (repl->errno) {
365                 errno = ntohl(repl->errno);
366                 return (-1);
367         }
368         rlen = cc - hlen;
369         x = ntohl(repl->count);
370         if (rlen < x) {
371                 printf("nfsread: short packet, %d < %ld\n", rlen, x);
372                 errno = EBADRPC;
373                 return(-1);
374         }
375         bcopy(repl->data, addr, x);
376         return (x);
377 }
378
379 /*
380  * Open a file.
381  * return zero or error number
382  */
383 int
384 nfs_open(upath, f)
385         const char *upath;
386         struct open_file *f;
387 {
388         struct iodesc *desc;
389         struct nfs_iodesc *currfd;
390         char buf[2 * NFS_FHSIZE + 3];
391         u_char *fh;
392         char *cp;
393         int i;
394 #ifndef NFS_NOSYMLINK
395         struct nfs_iodesc *newfd;
396         struct nfsv2_fattrs *fa;
397         char *ncp;
398         int c;
399         char namebuf[NFS_MAXPATHLEN + 1];
400         char linkbuf[NFS_MAXPATHLEN + 1];
401         int nlinks = 0;
402 #endif
403         int error;
404         char *path;
405
406 #ifdef NFS_DEBUG
407         if (debug)
408             printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
409 #endif
410         if (!rootpath[0]) {
411                 printf("no rootpath, no nfs\n");
412                 return (ENXIO);
413         }
414
415 #ifdef __sparc64__
416         if (strcmp(f->f_dev->dv_name, "net") != 0)
417                 return(EINVAL);
418 #endif
419         if (!(desc = socktodesc(*(int *)(f->f_devdata))))
420                 return(EINVAL);
421
422         /* Bind to a reserved port. */
423         desc->myport = htons(--rpc_port);
424         desc->destip = rootip;
425         if ((error = nfs_getrootfh(desc, rootpath, nfs_root_node.fh)))
426                 return (error);
427         nfs_root_node.iodesc = desc;
428
429         fh = &nfs_root_node.fh[0];
430         buf[0] = 'X';
431         cp = &buf[1];
432         for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
433                 sprintf(cp, "%02x", fh[i]);
434         sprintf(cp, "X");
435         setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
436         setenv("boot.nfsroot.path", rootpath, 1);
437         setenv("boot.nfsroot.nfshandle", buf, 1);
438
439 #ifndef NFS_NOSYMLINK
440         /* Fake up attributes for the root dir. */
441         fa = &nfs_root_node.fa;
442         fa->fa_type  = htonl(NFDIR);
443         fa->fa_mode  = htonl(0755);
444         fa->fa_nlink = htonl(2);
445
446         currfd = &nfs_root_node;
447         newfd = 0;
448
449         cp = path = strdup(upath);
450         if (path == NULL) {
451             error = ENOMEM;
452             goto out;
453         }
454         while (*cp) {
455                 /*
456                  * Remove extra separators
457                  */
458                 while (*cp == '/')
459                         cp++;
460
461                 if (*cp == '\0')
462                         break;
463                 /*
464                  * Check that current node is a directory.
465                  */
466                 if (currfd->fa.fa_type != htonl(NFDIR)) {
467                         error = ENOTDIR;
468                         goto out;
469                 }
470                 
471                 /* allocate file system specific data structure */
472                 newfd = malloc(sizeof(*newfd));
473                 newfd->iodesc = currfd->iodesc;
474                 newfd->off = 0;
475         
476                 /*
477                  * Get next component of path name.
478                  */
479                 {
480                         int len = 0;
481                         
482                         ncp = cp;
483                         while ((c = *cp) != '\0' && c != '/') {
484                                 if (++len > NFS_MAXNAMLEN) {
485                                         error = ENOENT;
486                                         goto out;
487                                 }
488                                 cp++;
489                         }
490                         *cp = '\0';
491                 }
492                 
493                 /* lookup a file handle */
494                 error = nfs_lookupfh(currfd, ncp, newfd);
495                 *cp = c;
496                 if (error)
497                         goto out;
498                 
499                 /*
500                  * Check for symbolic link
501                  */
502                 if (newfd->fa.fa_type == htonl(NFLNK)) {
503                         int link_len, len;
504                         
505                         error = nfs_readlink(newfd, linkbuf);
506                         if (error)
507                                 goto out;
508
509                         link_len = strlen(linkbuf);
510                         len = strlen(cp);
511
512                         if (link_len + len > MAXPATHLEN
513                             || ++nlinks > MAXSYMLINKS) {
514                                 error = ENOENT;
515                                 goto out;
516                         }
517
518                         bcopy(cp, &namebuf[link_len], len + 1);
519                         bcopy(linkbuf, namebuf, link_len);
520                         
521                         /*
522                          * If absolute pathname, restart at root.
523                          * If relative pathname, restart at parent directory.
524                          */
525                         cp = namebuf;
526                         if (*cp == '/') {
527                                 if (currfd != &nfs_root_node)
528                                         free(currfd);
529                                 currfd = &nfs_root_node;
530                         }
531
532                         free(newfd);
533                         newfd = 0;
534                         
535                         continue;
536                 }
537                 
538                 if (currfd != &nfs_root_node)
539                         free(currfd);
540                 currfd = newfd;
541                 newfd = 0;
542         }
543
544         error = 0;
545
546 out:
547         if (newfd)
548                 free(newfd);
549         if (path)
550                 free(path);
551 #else
552         /* allocate file system specific data structure */
553         currfd = malloc(sizeof(*currfd));
554         currfd->iodesc = desc;
555         currfd->off = 0;
556
557         error = nfs_lookupfh(&nfs_root_node, upath, currfd);
558 #endif
559         if (!error) {
560                 f->f_fsdata = (void *)currfd;
561                 return (0);
562         }
563                 
564 #ifdef NFS_DEBUG
565         if (debug)
566                 printf("nfs_open: %s lookupfh failed: %s\n",
567                     path, strerror(error));
568 #endif
569 #ifndef NFS_NOSYMLINK
570         if (currfd != &nfs_root_node)
571 #endif
572                 free(currfd);
573
574         return (error);
575 }
576
577 int
578 nfs_close(f)
579         struct open_file *f;
580 {
581         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
582
583 #ifdef NFS_DEBUG
584         if (debug)
585                 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
586 #endif
587
588         if (fp != &nfs_root_node && fp)
589                 free(fp);
590         f->f_fsdata = (void *)0;
591         
592         return (0);
593 }
594
595 /*
596  * read a portion of a file
597  */
598 int
599 nfs_read(f, buf, size, resid)
600         struct open_file *f;
601         void *buf;
602         size_t size;
603         size_t *resid;  /* out */
604 {
605         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
606         ssize_t cc;
607         char *addr = buf;
608         
609 #ifdef NFS_DEBUG
610         if (debug)
611                 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
612                        (int)fp->off);
613 #endif
614         while ((int)size > 0) {
615                 twiddle();
616                 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
617                 /* XXX maybe should retry on certain errors */
618                 if (cc == -1) {
619 #ifdef NFS_DEBUG
620                         if (debug)
621                                 printf("nfs_read: read: %s", strerror(errno));
622 #endif
623                         return (errno); /* XXX - from nfs_readdata */
624                 }
625                 if (cc == 0) {
626 #ifdef NFS_DEBUG
627                         if (debug)
628                                 printf("nfs_read: hit EOF unexpectantly");
629 #endif
630                         goto ret;
631                 }
632                 fp->off += cc;
633                 addr += cc;
634                 size -= cc;
635         }
636 ret:
637         if (resid)
638                 *resid = size;
639
640         return (0);
641 }
642
643 /*
644  * Not implemented.
645  */
646 int
647 nfs_write(f, buf, size, resid)
648         struct open_file *f;
649         void *buf;
650         size_t size;
651         size_t *resid;  /* out */
652 {
653         return (EROFS);
654 }
655
656 off_t
657 nfs_seek(f, offset, where)
658         struct open_file *f;
659         off_t offset;
660         int where;
661 {
662         struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
663         n_long size = ntohl(d->fa.fa_size);
664
665         switch (where) {
666         case SEEK_SET:
667                 d->off = offset;
668                 break;
669         case SEEK_CUR:
670                 d->off += offset;
671                 break;
672         case SEEK_END:
673                 d->off = size - offset;
674                 break;
675         default:
676                 return (-1);
677         }
678
679         return (d->off);
680 }
681
682 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
683 int nfs_stat_types[8] = {
684         0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
685
686 int
687 nfs_stat(f, sb)
688         struct open_file *f;
689         struct stat *sb;
690 {
691         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
692         n_long ftype, mode;
693
694         ftype = ntohl(fp->fa.fa_type);
695         mode  = ntohl(fp->fa.fa_mode);
696         mode |= nfs_stat_types[ftype & 7];
697
698         sb->st_mode  = mode;
699         sb->st_nlink = ntohl(fp->fa.fa_nlink);
700         sb->st_uid   = ntohl(fp->fa.fa_uid);
701         sb->st_gid   = ntohl(fp->fa.fa_gid);
702         sb->st_size  = ntohl(fp->fa.fa_size);
703
704         return (0);
705 }
706
707 static int
708 nfs_readdir(struct open_file *f, struct dirent *d)
709 {
710         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
711         struct nfs_readdir_args *args;
712         struct nfs_readdir_data *rd;
713         struct nfs_readdir_off  *roff = NULL;
714         static char *buf;
715         static n_long cookie = 0;
716         size_t cc;
717         n_long eof;
718         
719         struct {
720                 n_long h[RPC_HEADER_WORDS];
721                 struct nfs_readdir_args d;
722         } sdata;
723         static struct {
724                 n_long h[RPC_HEADER_WORDS];
725                 u_char d[NFS_READDIRSIZE];
726         } rdata;
727
728         if (cookie == 0) {
729         refill:
730                 args = &sdata.d;
731                 bzero(args, sizeof(*args));
732
733                 bcopy(fp->fh, args->fh, NFS_FHSIZE);
734                 args->cookie = htonl(cookie);
735                 args->count  = htonl(NFS_READDIRSIZE);
736                 
737                 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
738                               args, sizeof(*args),
739                               rdata.d, sizeof(rdata.d));
740                 buf  = rdata.d;
741                 roff = (struct nfs_readdir_off *)buf;
742                 if (ntohl(roff->cookie) != 0)
743                         return 1;
744         }
745         roff = (struct nfs_readdir_off *)buf;
746
747         if (ntohl(roff->follows) == 0) {
748                 eof = ntohl((roff+1)->cookie);
749                 if (eof) {
750                         cookie = 0;
751                         return 1;
752                 }
753                 goto refill;
754         }
755
756         buf += sizeof(struct nfs_readdir_off);
757         rd = (struct nfs_readdir_data *)buf;
758         d->d_namlen = ntohl(rd->len);
759         bcopy(rd->name, d->d_name, d->d_namlen);
760         d->d_name[d->d_namlen] = '\0';
761
762         buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
763         roff = (struct nfs_readdir_off *)buf;
764         cookie = ntohl(roff->cookie);
765         return 0;
766 }