]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/nfs.c
This commit was generated by cvs2svn to compensate for changes in r96539,
[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 #ifndef NFS_NOSYMLINK
391         struct nfs_iodesc *newfd;
392         struct nfsv2_fattrs *fa;
393         char *cp, *ncp;
394         int c;
395         char namebuf[NFS_MAXPATHLEN + 1];
396         char linkbuf[NFS_MAXPATHLEN + 1];
397         int nlinks = 0;
398 #endif
399         int error;
400         char *path;
401
402 #ifdef NFS_DEBUG
403         if (debug)
404             printf("nfs_open: %s (rootpath=%s)\n", path, rootpath);
405 #endif
406         if (!rootpath[0]) {
407                 printf("no rootpath, no nfs\n");
408                 return (ENXIO);
409         }
410
411         if (!(desc = socktodesc(*(int *)(f->f_devdata))))
412                 return(EINVAL);
413
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)))
418                 return (error);
419         nfs_root_node.iodesc = desc;
420
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);
427
428         currfd = &nfs_root_node;
429         newfd = 0;
430
431         cp = path = strdup(upath);
432         if (path == NULL) {
433             error = ENOMEM;
434             goto out;
435         }
436         while (*cp) {
437                 /*
438                  * Remove extra separators
439                  */
440                 while (*cp == '/')
441                         cp++;
442
443                 if (*cp == '\0')
444                         break;
445                 /*
446                  * Check that current node is a directory.
447                  */
448                 if (currfd->fa.fa_type != htonl(NFDIR)) {
449                         error = ENOTDIR;
450                         goto out;
451                 }
452                 
453                 /* allocate file system specific data structure */
454                 newfd = malloc(sizeof(*newfd));
455                 newfd->iodesc = currfd->iodesc;
456                 newfd->off = 0;
457         
458                 /*
459                  * Get next component of path name.
460                  */
461                 {
462                         int len = 0;
463                         
464                         ncp = cp;
465                         while ((c = *cp) != '\0' && c != '/') {
466                                 if (++len > NFS_MAXNAMLEN) {
467                                         error = ENOENT;
468                                         goto out;
469                                 }
470                                 cp++;
471                         }
472                         *cp = '\0';
473                 }
474                 
475                 /* lookup a file handle */
476                 error = nfs_lookupfh(currfd, ncp, newfd);
477                 *cp = c;
478                 if (error)
479                         goto out;
480                 
481                 /*
482                  * Check for symbolic link
483                  */
484                 if (newfd->fa.fa_type == htonl(NFLNK)) {
485                         int link_len, len;
486                         
487                         error = nfs_readlink(newfd, linkbuf);
488                         if (error)
489                                 goto out;
490
491                         link_len = strlen(linkbuf);
492                         len = strlen(cp);
493
494                         if (link_len + len > MAXPATHLEN
495                             || ++nlinks > MAXSYMLINKS) {
496                                 error = ENOENT;
497                                 goto out;
498                         }
499
500                         bcopy(cp, &namebuf[link_len], len + 1);
501                         bcopy(linkbuf, namebuf, link_len);
502                         
503                         /*
504                          * If absolute pathname, restart at root.
505                          * If relative pathname, restart at parent directory.
506                          */
507                         cp = namebuf;
508                         if (*cp == '/') {
509                                 if (currfd != &nfs_root_node)
510                                         free(currfd);
511                                 currfd = &nfs_root_node;
512                         }
513
514                         free(newfd);
515                         newfd = 0;
516                         
517                         continue;
518                 }
519                 
520                 if (currfd != &nfs_root_node)
521                         free(currfd);
522                 currfd = newfd;
523                 newfd = 0;
524         }
525
526         error = 0;
527
528 out:
529         if (newfd)
530                 free(newfd);
531         if (path)
532                 free(path);
533 #else
534         /* allocate file system specific data structure */
535         currfd = malloc(sizeof(*currfd));
536         currfd->iodesc = desc;
537         currfd->off = 0;
538
539         error = nfs_lookupfh(&nfs_root_node, upath, currfd);
540 #endif
541         if (!error) {
542                 f->f_fsdata = (void *)currfd;
543                 return (0);
544         }
545                 
546 #ifdef NFS_DEBUG
547         if (debug)
548                 printf("nfs_open: %s lookupfh failed: %s\n",
549                     path, strerror(error));
550 #endif
551 #ifndef NFS_NOSYMLINK
552         if (currfd != &nfs_root_node)
553 #endif
554                 free(currfd);
555
556         return (error);
557 }
558
559 int
560 nfs_close(f)
561         struct open_file *f;
562 {
563         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
564
565 #ifdef NFS_DEBUG
566         if (debug)
567                 printf("nfs_close: fp=0x%lx\n", (u_long)fp);
568 #endif
569
570         if (fp != &nfs_root_node && fp)
571                 free(fp);
572         f->f_fsdata = (void *)0;
573         
574         return (0);
575 }
576
577 /*
578  * read a portion of a file
579  */
580 int
581 nfs_read(f, buf, size, resid)
582         struct open_file *f;
583         void *buf;
584         size_t size;
585         size_t *resid;  /* out */
586 {
587         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
588         ssize_t cc;
589         char *addr = buf;
590         
591 #ifdef NFS_DEBUG
592         if (debug)
593                 printf("nfs_read: size=%lu off=%d\n", (u_long)size,
594                        (int)fp->off);
595 #endif
596         while ((int)size > 0) {
597                 twiddle();
598                 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
599                 /* XXX maybe should retry on certain errors */
600                 if (cc == -1) {
601 #ifdef NFS_DEBUG
602                         if (debug)
603                                 printf("nfs_read: read: %s", strerror(errno));
604 #endif
605                         return (errno); /* XXX - from nfs_readdata */
606                 }
607                 if (cc == 0) {
608 #ifdef NFS_DEBUG
609                         if (debug)
610                                 printf("nfs_read: hit EOF unexpectantly");
611 #endif
612                         goto ret;
613                 }
614                 fp->off += cc;
615                 addr += cc;
616                 size -= cc;
617         }
618 ret:
619         if (resid)
620                 *resid = size;
621
622         return (0);
623 }
624
625 /*
626  * Not implemented.
627  */
628 int
629 nfs_write(f, buf, size, resid)
630         struct open_file *f;
631         void *buf;
632         size_t size;
633         size_t *resid;  /* out */
634 {
635         return (EROFS);
636 }
637
638 off_t
639 nfs_seek(f, offset, where)
640         struct open_file *f;
641         off_t offset;
642         int where;
643 {
644         struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
645         n_long size = ntohl(d->fa.fa_size);
646
647         switch (where) {
648         case SEEK_SET:
649                 d->off = offset;
650                 break;
651         case SEEK_CUR:
652                 d->off += offset;
653                 break;
654         case SEEK_END:
655                 d->off = size - offset;
656                 break;
657         default:
658                 return (-1);
659         }
660
661         return (d->off);
662 }
663
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 };
667
668 int
669 nfs_stat(f, sb)
670         struct open_file *f;
671         struct stat *sb;
672 {
673         struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
674         n_long ftype, mode;
675
676         ftype = ntohl(fp->fa.fa_type);
677         mode  = ntohl(fp->fa.fa_mode);
678         mode |= nfs_stat_types[ftype & 7];
679
680         sb->st_mode  = mode;
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);
685
686         return (0);
687 }
688
689 static int
690 nfs_readdir(struct open_file *f, struct dirent *d)
691 {
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;
696         static char *buf;
697         static n_long cookie = 0;
698         size_t cc;
699         n_long eof;
700         
701         struct {
702                 n_long h[RPC_HEADER_WORDS];
703                 struct nfs_readdir_args d;
704         } sdata;
705         static struct {
706                 n_long h[RPC_HEADER_WORDS];
707                 u_char d[NFS_READDIRSIZE];
708         } rdata;
709
710         if (cookie == 0) {
711         refill:
712                 args = &sdata.d;
713                 bzero(args, sizeof(*args));
714
715                 bcopy(fp->fh, args->fh, NFS_FHSIZE);
716                 args->cookie = htonl(cookie);
717                 args->count  = htonl(NFS_READDIRSIZE);
718                 
719                 cc = rpc_call(fp->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READDIR,
720                               args, sizeof(*args),
721                               rdata.d, sizeof(rdata.d));
722                 buf  = rdata.d;
723                 roff = (struct nfs_readdir_off *)buf;
724                 if (ntohl(roff->cookie) != 0)
725                         return 1;
726         }
727         roff = (struct nfs_readdir_off *)buf;
728
729         if (ntohl(roff->follows) == 0) {
730                 eof = ntohl((roff+1)->cookie);
731                 if (eof) {
732                         cookie = 0;
733                         return 1;
734                 }
735                 goto refill;
736         }
737
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';
743
744         buf += (sizeof(struct nfs_readdir_data) + roundup(htonl(rd->len),4));
745         roff = (struct nfs_readdir_off *)buf;
746         cookie = ntohl(roff->cookie);
747         return 0;
748 }