nfs.c revision 1.16 1 /* $NetBSD: nfs.c,v 1.16 1996/09/30 16:01:20 ws 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/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <string.h>
36
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39
40 #include "rpcv2.h"
41 #include "nfsv2.h"
42
43 #include "stand.h"
44 #include "net.h"
45 #include "netif.h"
46 #include "nfs.h"
47 #include "rpc.h"
48
49 /* Define our own NFS attributes without NQNFS stuff. */
50 struct nfsv2_fattrs {
51 n_long fa_type;
52 n_long fa_mode;
53 n_long fa_nlink;
54 n_long fa_uid;
55 n_long fa_gid;
56 n_long fa_size;
57 n_long fa_blocksize;
58 n_long fa_rdev;
59 n_long fa_blocks;
60 n_long fa_fsid;
61 n_long fa_fileid;
62 struct nfsv2_time fa_atime;
63 struct nfsv2_time fa_mtime;
64 struct nfsv2_time fa_ctime;
65 };
66
67
68 struct nfs_read_args {
69 u_char fh[NFS_FHSIZE];
70 n_long off;
71 n_long len;
72 n_long xxx; /* XXX what's this for? */
73 };
74
75 /* Data part of nfs rpc reply (also the largest thing we receive) */
76 #define NFSREAD_SIZE 1024
77 struct nfs_read_repl {
78 n_long errno;
79 struct nfsv2_fattrs fa;
80 n_long count;
81 u_char data[NFSREAD_SIZE];
82 };
83
84 struct nfs_readlnk_repl {
85 n_long errno;
86 n_long len;
87 char path[NFS_MAXPATHLEN];
88 };
89
90 struct nfs_iodesc {
91 struct iodesc *iodesc;
92 off_t off;
93 u_char fh[NFS_FHSIZE];
94 struct nfsv2_fattrs fa; /* all in network order */
95 };
96
97 struct nfs_iodesc nfs_root_node;
98
99
100 /*
101 * Fetch the root file handle (call mount daemon)
102 * On error, return non-zero and set errno.
103 */
104 int
105 nfs_getrootfh(d, path, fhp)
106 register struct iodesc *d;
107 char *path;
108 u_char *fhp;
109 {
110 register int len;
111 struct args {
112 n_long len;
113 char path[FNAME_SIZE];
114 } *args;
115 struct repl {
116 n_long errno;
117 u_char fh[NFS_FHSIZE];
118 } *repl;
119 struct {
120 n_long h[RPC_HEADER_WORDS];
121 struct args d;
122 } sdata;
123 struct {
124 n_long h[RPC_HEADER_WORDS];
125 struct repl d;
126 } rdata;
127 size_t cc;
128
129 #ifdef NFS_DEBUG
130 if (debug)
131 printf("nfs_getrootfh: %s\n", path);
132 #endif
133
134 args = &sdata.d;
135 repl = &rdata.d;
136
137 bzero(args, sizeof(*args));
138 len = strlen(path);
139 if (len > sizeof(args->path))
140 len = sizeof(args->path);
141 args->len = htonl(len);
142 bcopy(path, args->path, len);
143 len = 4 + roundup(len, 4);
144
145 cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
146 args, len, repl, sizeof(*repl));
147 if (cc == -1) {
148 /* errno was set by rpc_call */
149 return (-1);
150 }
151 if (cc < 4) {
152 errno = EBADRPC;
153 return (-1);
154 }
155 if (repl->errno) {
156 errno = ntohl(repl->errno);
157 return (-1);
158 }
159 bcopy(repl->fh, fhp, sizeof(repl->fh));
160 return (0);
161 }
162
163 /*
164 * Lookup a file. Store handle and attributes.
165 * Return zero or error number.
166 */
167 int
168 nfs_lookupfh(d, name, newfd)
169 struct nfs_iodesc *d;
170 char *name;
171 struct nfs_iodesc *newfd;
172 {
173 register int len, rlen;
174 struct args {
175 u_char fh[NFS_FHSIZE];
176 n_long len;
177 char name[FNAME_SIZE];
178 } *args;
179 struct repl {
180 n_long errno;
181 u_char fh[NFS_FHSIZE];
182 struct nfsv2_fattrs fa;
183 } *repl;
184 struct {
185 n_long h[RPC_HEADER_WORDS];
186 struct args d;
187 } sdata;
188 struct {
189 n_long h[RPC_HEADER_WORDS];
190 struct repl d;
191 } rdata;
192 ssize_t cc;
193
194 #ifdef NFS_DEBUG
195 if (debug)
196 printf("lookupfh: called\n");
197 #endif
198
199 args = &sdata.d;
200 repl = &rdata.d;
201
202 bzero(args, sizeof(*args));
203 bcopy(d->fh, args->fh, sizeof(args->fh));
204 len = strlen(name);
205 if (len > sizeof(args->name))
206 len = sizeof(args->name);
207 bcopy(name, args->name, len);
208 args->len = htonl(len);
209 len = 4 + roundup(len, 4);
210 len += NFS_FHSIZE;
211
212 rlen = sizeof(*repl);
213
214 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
215 args, len, repl, rlen);
216 if (cc == -1)
217 return (errno); /* XXX - from rpc_call */
218 if (cc < 4)
219 return (EIO);
220 if (repl->errno) {
221 /* saerrno.h now matches NFS error numbers. */
222 return (ntohl(repl->errno));
223 }
224 bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
225 bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
226 return (0);
227 }
228
229 /*
230 * Get the destination of a symbolic link.
231 */
232 int
233 nfs_readlink(d, buf)
234 struct nfs_iodesc *d;
235 char *buf;
236 {
237 struct {
238 n_long h[RPC_HEADER_WORDS];
239 u_char fh[NFS_FHSIZE];
240 } sdata;
241 struct {
242 n_long h[RPC_HEADER_WORDS];
243 struct nfs_readlnk_repl d;
244 } rdata;
245 ssize_t cc;
246
247 #ifdef NFS_DEBUG
248 if (debug)
249 printf("readlink: called\n");
250 #endif
251
252 bcopy(d->fh, sdata.fh, NFS_FHSIZE);
253 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
254 sdata.fh, NFS_FHSIZE,
255 &rdata.d, sizeof(rdata.d));
256 if (cc == -1)
257 return (errno);
258
259 if (cc < 4)
260 return (EIO);
261
262 if (rdata.d.errno)
263 return (ntohl(rdata.d.errno));
264
265 if (rdata.d.len > NFS_MAXPATHLEN)
266 return (ENAMETOOLONG);
267
268 bcopy(rdata.d.path, buf, rdata.d.len);
269 buf[rdata.d.len] = 0;
270 return (0);
271 }
272
273 /*
274 * Read data from a file.
275 * Return transfer count or -1 (and set errno)
276 */
277 ssize_t
278 nfs_readdata(d, off, addr, len)
279 struct nfs_iodesc *d;
280 off_t off;
281 void *addr;
282 size_t len;
283 {
284 struct nfs_read_args *args;
285 struct nfs_read_repl *repl;
286 struct {
287 n_long h[RPC_HEADER_WORDS];
288 struct nfs_read_args d;
289 } sdata;
290 struct {
291 n_long h[RPC_HEADER_WORDS];
292 struct nfs_read_repl d;
293 } rdata;
294 size_t cc;
295 long x;
296 int hlen, rlen;
297
298 args = &sdata.d;
299 repl = &rdata.d;
300
301 bcopy(d->fh, args->fh, NFS_FHSIZE);
302 args->off = htonl((n_long)off);
303 if (len > NFSREAD_SIZE)
304 len = NFSREAD_SIZE;
305 args->len = htonl((n_long)len);
306 args->xxx = htonl((n_long)0);
307 hlen = sizeof(*repl) - NFSREAD_SIZE;
308
309 cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
310 args, sizeof(*args),
311 repl, sizeof(*repl));
312 if (cc == -1) {
313 /* errno was already set by rpc_call */
314 return (-1);
315 }
316 if (cc < hlen) {
317 errno = EBADRPC;
318 return (-1);
319 }
320 if (repl->errno) {
321 errno = ntohl(repl->errno);
322 return (-1);
323 }
324 rlen = cc - hlen;
325 x = ntohl(repl->count);
326 if (rlen < x) {
327 printf("nfsread: short packet, %d < %ld\n", rlen, x);
328 errno = EBADRPC;
329 return(-1);
330 }
331 bcopy(repl->data, addr, x);
332 return (x);
333 }
334
335 /*
336 * nfs_mount - mount this nfs filesystem to a host
337 * On error, return non-zero and set errno.
338 */
339 int
340 nfs_mount(sock, ip, path)
341 int sock;
342 struct in_addr ip;
343 char *path;
344 {
345 struct iodesc *desc;
346 struct nfsv2_fattrs *fa;
347
348 if (!(desc = socktodesc(sock))) {
349 errno = EINVAL;
350 return(-1);
351 }
352
353 /* Bind to a reserved port. */
354 desc->myport = htons(--rpc_port);
355 desc->destip = ip;
356 if (nfs_getrootfh(desc, path, nfs_root_node.fh))
357 return (-1);
358 nfs_root_node.iodesc = desc;
359 /* Fake up attributes for the root dir. */
360 fa = &nfs_root_node.fa;
361 fa->fa_type = htonl(NFDIR);
362 fa->fa_mode = htonl(0755);
363 fa->fa_nlink = htonl(2);
364
365 #ifdef NFS_DEBUG
366 if (debug)
367 printf("nfs_mount: got fh for %s\n", path);
368 #endif
369
370 return(0);
371 }
372
373 /*
374 * Open a file.
375 * return zero or error number
376 */
377 int
378 nfs_open(path, f)
379 char *path;
380 struct open_file *f;
381 {
382 struct nfs_iodesc *newfd, *currfd;
383 register char *cp, *ncp;
384 register int c;
385 char namebuf[NFS_MAXPATHLEN + 1];
386 char linkbuf[NFS_MAXPATHLEN + 1];
387 int nlinks = 0;
388 int error = 0;
389
390 #ifdef NFS_DEBUG
391 if (debug)
392 printf("nfs_open: %s\n", path);
393 #endif
394 if (nfs_root_node.iodesc == NULL) {
395 printf("nfs_open: must mount first.\n");
396 return (ENXIO);
397 }
398
399 currfd = &nfs_root_node;
400 newfd = 0;
401
402 cp = path;
403 while (*cp) {
404 /*
405 * Remove extra separators
406 */
407 while (*cp == '/')
408 cp++;
409
410 if (*cp == '\0')
411 break;
412 /*
413 * Check that current node is a directory.
414 */
415 if (currfd->fa.fa_type != NFDIR) {
416 error = ENOTDIR;
417 goto out;
418 }
419
420 /* allocate file system specific data structure */
421 newfd = alloc(sizeof(*newfd));
422 newfd->iodesc = currfd->iodesc;
423 newfd->off = 0;
424
425 /*
426 * Get next component of path name.
427 */
428 {
429 register int len = 0;
430
431 ncp = cp;
432 while ((c = *cp) != '\0' && c != '/') {
433 if (++len > NFS_MAXNAMLEN) {
434 error = ENOENT;
435 goto out;
436 }
437 cp++;
438 }
439 *cp = '\0';
440 }
441
442 /* lookup a file handle */
443 error = nfs_lookupfh(currfd, ncp, newfd);
444 *cp = c;
445 if (error)
446 goto out;
447
448 /*
449 * Check for symbolic link
450 */
451 if (newfd->fa.fa_type == NFLNK) {
452 int link_len, len;
453
454 error = nfs_readlink(newfd, linkbuf);
455 if (error)
456 goto out;
457
458 link_len = strlen(linkbuf);
459 len = strlen(cp);
460
461 if (link_len + len > MAXPATHLEN
462 || ++nlinks > MAXSYMLINKS) {
463 error = ENOENT;
464 goto out;
465 }
466
467 bcopy(cp, &namebuf[link_len], len + 1);
468 bcopy(linkbuf, namebuf, link_len);
469
470 /*
471 * If absolute pathname, restart at root.
472 * If relative pathname, restart at parent directory.
473 */
474 cp = namebuf;
475 if (*cp == '/') {
476 if (currfd != &nfs_root_node)
477 free(currfd, sizeof(*currfd));
478 currfd = &nfs_root_node;
479 }
480
481 free(newfd, sizeof(*newfd));
482 newfd = 0;
483
484 continue;
485 }
486
487 if (currfd != &nfs_root_node)
488 free(currfd, sizeof(*currfd));
489 currfd = newfd;
490 newfd = 0;
491 }
492
493 error = 0;
494
495 out:
496 if (!error) {
497 f->f_fsdata = (void *)currfd;
498 return (0);
499 }
500
501 #ifdef NFS_DEBUG
502 if (debug)
503 printf("nfs_open: %s lookupfh failed: %s\n",
504 path, strerror(error));
505 #endif
506 if (currfd != &nfs_root_node)
507 free(currfd, sizeof(*currfd));
508 if (newfd)
509 free(newfd, sizeof(*newfd));
510
511 return (error);
512 }
513
514 int
515 nfs_close(f)
516 struct open_file *f;
517 {
518 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
519
520 #ifdef NFS_DEBUG
521 if (debug)
522 printf("nfs_close: fp=0x%x\n", fp);
523 #endif
524
525 if (fp)
526 free(fp, sizeof(struct nfs_iodesc));
527 f->f_fsdata = (void *)0;
528
529 return (0);
530 }
531
532 /*
533 * read a portion of a file
534 */
535 int
536 nfs_read(f, buf, size, resid)
537 struct open_file *f;
538 void *buf;
539 size_t size;
540 size_t *resid; /* out */
541 {
542 register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
543 register ssize_t cc;
544 register char *addr = buf;
545
546 #ifdef NFS_DEBUG
547 if (debug)
548 printf("nfs_read: size=%d off=%d\n", size, (int)fp->off);
549 #endif
550 while ((int)size > 0) {
551 twiddle();
552 cc = nfs_readdata(fp, fp->off, (void *)addr, size);
553 /* XXX maybe should retry on certain errors */
554 if (cc == -1) {
555 #ifdef NFS_DEBUG
556 if (debug)
557 printf("nfs_read: read: %s", strerror(errno));
558 #endif
559 return (errno); /* XXX - from nfs_readdata */
560 }
561 if (cc == 0) {
562 if (debug)
563 printf("nfs_read: hit EOF unexpectantly");
564 goto ret;
565 }
566 fp->off += cc;
567 addr += cc;
568 size -= cc;
569 }
570 ret:
571 if (resid)
572 *resid = size;
573
574 return (0);
575 }
576
577 /*
578 * Not implemented.
579 */
580 int
581 nfs_write(f, buf, size, resid)
582 struct open_file *f;
583 void *buf;
584 size_t size;
585 size_t *resid; /* out */
586 {
587 return (EROFS);
588 }
589
590 off_t
591 nfs_seek(f, offset, where)
592 struct open_file *f;
593 off_t offset;
594 int where;
595 {
596 register struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
597 n_long size = ntohl(d->fa.fa_size);
598
599 switch (where) {
600 case SEEK_SET:
601 d->off = offset;
602 break;
603 case SEEK_CUR:
604 d->off += offset;
605 break;
606 case SEEK_END:
607 d->off = size - offset;
608 break;
609 default:
610 return (-1);
611 }
612
613 return (d->off);
614 }
615
616 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
617 int nfs_stat_types[8] = {
618 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
619
620 int
621 nfs_stat(f, sb)
622 struct open_file *f;
623 struct stat *sb;
624 {
625 struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
626 register n_long ftype, mode;
627
628 ftype = ntohl(fp->fa.fa_type);
629 mode = ntohl(fp->fa.fa_mode);
630 mode |= nfs_stat_types[ftype & 7];
631
632 sb->st_mode = mode;
633 sb->st_nlink = ntohl(fp->fa.fa_nlink);
634 sb->st_uid = ntohl(fp->fa.fa_uid);
635 sb->st_gid = ntohl(fp->fa.fa_gid);
636 sb->st_size = ntohl(fp->fa.fa_size);
637
638 return (0);
639 }
640