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/cdefs.h>
33 #include <sys/param.h>
35 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
53 #define NFSREAD_MIN_SIZE 1024
54 #define NFSREAD_MAX_SIZE 16384
56 /* NFSv3 definitions */
57 #define NFS_V3MAXFHSIZE 64
60 #define NFSPROCV3_LOOKUP 3
61 #define NFSPROCV3_READLINK 5
62 #define NFSPROCV3_READ 6
63 #define NFSPROCV3_READDIR 16
85 struct nfsv3_time fa_atime
;
86 struct nfsv3_time fa_mtime
;
87 struct nfsv3_time fa_ctime
;
91 * For NFSv3, the file handle is variable in size, so most fixed sized
92 * structures for arguments won't work. For most cases, a structure
93 * that starts with any fixed size section is followed by an array
94 * that covers the maximum size required.
96 struct nfsv3_readdir_repl
{
99 struct nfsv3_fattrs fa
;
104 struct nfsv3_readdir_entry
{
109 uint32_t nameplus
[0];
113 struct iodesc
*iodesc
;
116 u_char fh
[NFS_V3MAXFHSIZE
];
117 struct nfsv3_fattrs fa
; /* all in network order */
122 * XXX interactions with tftp? See nfswrapper.c for a confusing
125 int nfs_open(const char *path
, struct open_file
*f
);
126 static int nfs_close(struct open_file
*f
);
127 static int nfs_read(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
);
132 struct nfs_iodesc nfs_root_node
;
134 struct fs_ops nfs_fsops
= {
145 static int nfs_read_size
= NFSREAD_MIN_SIZE
;
148 * Improve boot performance over NFS
151 set_nfs_read_size(void)
156 if ((env
= getenv("nfs.read_size")) != NULL
) {
158 nfs_read_size
= strtol(env
, &end
, 0);
159 if (errno
!= 0 || *env
== '\0' || *end
!= '\0') {
160 printf("%s: bad value: \"%s\", defaulting to %d\n",
161 "nfs.read_size", env
, NFSREAD_MIN_SIZE
);
162 nfs_read_size
= NFSREAD_MIN_SIZE
;
165 if (nfs_read_size
< NFSREAD_MIN_SIZE
) {
166 printf("%s: bad value: \"%d\", defaulting to %d\n",
167 "nfs.read_size", nfs_read_size
, NFSREAD_MIN_SIZE
);
168 nfs_read_size
= NFSREAD_MIN_SIZE
;
170 if (nfs_read_size
> NFSREAD_MAX_SIZE
) {
171 printf("%s: bad value: \"%d\", defaulting to %d\n",
172 "nfs.read_size", nfs_read_size
, NFSREAD_MIN_SIZE
);
173 nfs_read_size
= NFSREAD_MAX_SIZE
;
175 snprintf(buf
, sizeof (buf
), "%d", nfs_read_size
);
176 setenv("nfs.read_size", buf
, 1);
180 * Fetch the root file handle (call mount daemon)
181 * Return zero or error number.
184 nfs_getrootfh(struct iodesc
*d
, char *path
, uint32_t *fhlenp
, u_char
*fhp
)
190 char path
[FNAME_SIZE
];
195 u_char fh
[NFS_V3MAXFHSIZE
];
200 uint32_t h
[RPC_HEADER_WORDS
];
207 printf("nfs_getrootfh: %s\n", path
);
212 bzero(args
, sizeof(*args
));
214 if (len
> sizeof(args
->path
))
215 len
= sizeof(args
->path
);
216 args
->len
= htonl(len
);
217 bcopy(path
, args
->path
, len
);
218 len
= sizeof(uint32_t) + roundup(len
, sizeof(uint32_t));
220 cc
= rpc_call(d
, RPCPROG_MNT
, RPCMNT_VER3
, RPCMNT_MOUNT
,
221 args
, len
, (void **)&repl
, &pkt
);
224 /* errno was set by rpc_call */
227 if (cc
< 2 * sizeof (uint32_t)) {
231 if (repl
->errno
!= 0) {
233 return (ntohl(repl
->errno
));
235 *fhlenp
= ntohl(repl
->fhsize
);
236 bcopy(repl
->fh
, fhp
, *fhlenp
);
244 * Lookup a file. Store handle and attributes.
245 * Return zero or error number.
248 nfs_lookupfh(struct nfs_iodesc
*d
, const char *name
, struct nfs_iodesc
*newfd
)
254 uint32_t fhplusname
[1 +
255 (NFS_V3MAXFHSIZE
+ FNAME_SIZE
) / sizeof(uint32_t)];
260 uint32_t fhplusattr
[(NFS_V3MAXFHSIZE
+
261 2 * (sizeof(uint32_t) +
262 sizeof(struct nfsv3_fattrs
))) / sizeof(uint32_t)];
265 uint32_t h
[RPC_HEADER_WORDS
];
272 printf("lookupfh: called\n");
277 bzero(args
, sizeof(*args
));
278 args
->fhsize
= htonl(d
->fhsize
);
279 bcopy(d
->fh
, args
->fhplusname
, d
->fhsize
);
281 if (len
> FNAME_SIZE
)
283 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
284 args
->fhplusname
[pos
++] = htonl(len
);
285 bcopy(name
, &args
->fhplusname
[pos
], len
);
286 len
= sizeof(uint32_t) + pos
* sizeof(uint32_t) +
287 roundup(len
, sizeof(uint32_t));
289 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_LOOKUP
,
290 args
, len
, (void **)&repl
, &pkt
);
293 return (errno
); /* XXX - from rpc_call */
295 if (cc
< 2 * sizeof(uint32_t)) {
299 if (repl
->errno
!= 0) {
301 /* saerrno.h now matches NFS error numbers. */
302 return (ntohl(repl
->errno
));
304 newfd
->fhsize
= ntohl(repl
->fhsize
);
305 bcopy(repl
->fhplusattr
, &newfd
->fh
, newfd
->fhsize
);
306 pos
= roundup(newfd
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
307 if (repl
->fhplusattr
[pos
++] == 0) {
311 bcopy(&repl
->fhplusattr
[pos
], &newfd
->fa
, sizeof(newfd
->fa
));
317 * Get the destination of a symbolic link.
320 nfs_readlink(struct nfs_iodesc
*d
, char *buf
)
325 u_char fh
[NFS_V3MAXFHSIZE
];
330 struct nfsv3_fattrs fa
;
332 u_char path
[NFS_MAXPATHLEN
];
335 uint32_t h
[RPC_HEADER_WORDS
];
343 printf("readlink: called\n");
348 bzero(args
, sizeof(*args
));
349 args
->fhsize
= htonl(d
->fhsize
);
350 bcopy(d
->fh
, args
->fh
, d
->fhsize
);
351 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READLINK
,
352 args
, sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
353 (void **)&repl
, &pkt
);
357 if (cc
< 2 * sizeof(uint32_t)) {
362 if (repl
->errno
!= 0) {
363 rc
= ntohl(repl
->errno
);
372 repl
->len
= ntohl(repl
->len
);
373 if (repl
->len
> NFS_MAXPATHLEN
) {
378 bcopy(repl
->path
, buf
, repl
->len
);
386 * Read data from a file.
387 * Return transfer count or -1 (and set errno)
390 nfs_readdata(struct nfs_iodesc
*d
, off_t off
, void *addr
, size_t len
)
395 uint32_t fhoffcnt
[NFS_V3MAXFHSIZE
/ sizeof(uint32_t) + 3];
400 struct nfsv3_fattrs fa
;
404 u_char data
[NFSREAD_MAX_SIZE
];
407 uint32_t h
[RPC_HEADER_WORDS
];
416 bzero(args
, sizeof(*args
));
417 args
->fhsize
= htonl(d
->fhsize
);
418 bcopy(d
->fh
, args
->fhoffcnt
, d
->fhsize
);
419 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
420 args
->fhoffcnt
[pos
++] = 0;
421 args
->fhoffcnt
[pos
++] = htonl((uint32_t)off
);
422 if (len
> nfs_read_size
)
424 args
->fhoffcnt
[pos
] = htonl((uint32_t)len
);
425 hlen
= offsetof(struct repl
, data
[0]);
427 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READ
,
428 args
, 4 * sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
429 (void **)&repl
, &pkt
);
431 /* errno was already set by rpc_call */
439 if (repl
->errno
!= 0) {
440 errno
= ntohl(repl
->errno
);
445 x
= ntohl(repl
->count
);
447 printf("nfsread: short packet, %d < %ld\n", rlen
, x
);
452 bcopy(repl
->data
, addr
, x
);
459 * return zero or error number
462 nfs_open(const char *upath
, struct open_file
*f
)
465 struct nfs_iodesc
*currfd
= NULL
;
466 char buf
[2 * NFS_V3MAXFHSIZE
+ 3];
470 struct nfs_iodesc
*newfd
= NULL
;
473 char namebuf
[NFS_MAXPATHLEN
+ 1];
474 char linkbuf
[NFS_MAXPATHLEN
+ 1];
479 if (netproto
!= NET_NFS
)
484 printf("nfs_open: %s (rootpath=%s)\n", upath
, rootpath
);
487 printf("no rootpath, no nfs\n");
491 if (f
->f_dev
->dv_type
!= DEVT_NET
)
494 if (!(desc
= socktodesc(*(int *)(f
->f_devdata
))))
497 /* Bind to a reserved port. */
498 desc
->myport
= htons(--rpc_port
);
499 desc
->destip
= rootip
;
500 if ((error
= nfs_getrootfh(desc
, rootpath
, &nfs_root_node
.fhsize
,
503 nfs_root_node
.fa
.fa_type
= htonl(NFDIR
);
504 nfs_root_node
.fa
.fa_mode
= htonl(0755);
505 nfs_root_node
.fa
.fa_nlink
= htonl(2);
506 nfs_root_node
.iodesc
= desc
;
508 fh
= &nfs_root_node
.fh
[0];
511 for (i
= 0; i
< nfs_root_node
.fhsize
; i
++, cp
+= 2)
512 sprintf(cp
, "%02x", fh
[i
]);
514 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
515 setenv("boot.nfsroot.path", rootpath
, 1);
516 setenv("boot.nfsroot.nfshandle", buf
, 1);
517 sprintf(buf
, "%d", nfs_root_node
.fhsize
);
518 setenv("boot.nfsroot.nfshandlelen", buf
, 1);
520 /* Allocate file system specific data structure */
521 currfd
= malloc(sizeof(*newfd
));
522 if (currfd
== NULL
) {
526 bcopy(&nfs_root_node
, currfd
, sizeof(*currfd
));
529 cp
= path
= strdup(upath
);
536 * Remove extra separators
544 * Check that current node is a directory.
546 if (currfd
->fa
.fa_type
!= htonl(NFDIR
)) {
551 /* allocate file system specific data structure */
552 newfd
= malloc(sizeof(*newfd
));
557 newfd
->iodesc
= currfd
->iodesc
;
560 * Get next component of path name.
566 while ((c
= *cp
) != '\0' && c
!= '/') {
567 if (++len
> NFS_MAXNAMLEN
) {
576 /* lookup a file handle */
577 error
= nfs_lookupfh(currfd
, ncp
, newfd
);
583 * Check for symbolic link
585 if (newfd
->fa
.fa_type
== htonl(NFLNK
)) {
588 error
= nfs_readlink(newfd
, linkbuf
);
592 link_len
= strlen(linkbuf
);
595 if (link_len
+ len
> MAXPATHLEN
596 || ++nlinks
> MAXSYMLINKS
) {
601 bcopy(cp
, &namebuf
[link_len
], len
+ 1);
602 bcopy(linkbuf
, namebuf
, link_len
);
605 * If absolute pathname, restart at root.
606 * If relative pathname, restart at parent directory.
610 bcopy(&nfs_root_node
, currfd
, sizeof(*currfd
));
631 f
->f_fsdata
= (void *)currfd
;
637 printf("nfs_open: %s lookupfh failed: %s\n",
638 path
, strerror(error
));
646 nfs_close(struct open_file
*f
)
648 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
652 printf("nfs_close: fp=0x%lx\n", (u_long
)fp
);
662 * read a portion of a file
665 nfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
667 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
673 printf("nfs_read: size=%lu off=%d\n", (u_long
)size
,
676 while ((int)size
> 0) {
678 cc
= nfs_readdata(fp
, fp
->off
, (void *)addr
, size
);
679 /* XXX maybe should retry on certain errors */
683 printf("nfs_read: read: %s", strerror(errno
));
685 return (errno
); /* XXX - from nfs_readdata */
690 printf("nfs_read: hit EOF unexpectantly");
706 nfs_seek(struct open_file
*f
, off_t offset
, int where
)
708 struct nfs_iodesc
*d
= (struct nfs_iodesc
*)f
->f_fsdata
;
709 uint32_t size
= ntohl(d
->fa
.fa_size
.val
[1]);
719 d
->off
= size
- offset
;
729 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
730 int nfs_stat_types
[9] = {
731 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
, S_IFSOCK
, S_IFIFO
, 0 };
734 nfs_stat(struct open_file
*f
, struct stat
*sb
)
736 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
737 uint32_t ftype
, mode
;
739 ftype
= ntohl(fp
->fa
.fa_type
);
740 mode
= ntohl(fp
->fa
.fa_mode
);
741 mode
|= nfs_stat_types
[ftype
& 7];
744 sb
->st_nlink
= ntohl(fp
->fa
.fa_nlink
);
745 sb
->st_uid
= ntohl(fp
->fa
.fa_uid
);
746 sb
->st_gid
= ntohl(fp
->fa
.fa_gid
);
747 sb
->st_size
= ntohl(fp
->fa
.fa_size
.val
[1]);
753 nfs_readdir(struct open_file
*f
, struct dirent
*d
)
755 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
756 struct nfsv3_readdir_repl
*repl
;
757 struct nfsv3_readdir_entry
*rent
;
758 static void *pkt
= NULL
;
760 static struct nfs_iodesc
*pfp
= NULL
;
761 static uint64_t cookie
= 0;
767 uint32_t fhpluscookie
[5 + NFS_V3MAXFHSIZE
];
770 uint32_t h
[RPC_HEADER_WORDS
];
774 if (fp
!= pfp
|| fp
->off
!= cookie
) {
780 bzero(args
, sizeof(*args
));
782 args
->fhsize
= htonl(fp
->fhsize
);
783 bcopy(fp
->fh
, args
->fhpluscookie
, fp
->fhsize
);
784 pos
= roundup(fp
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
785 args
->fhpluscookie
[pos
++] = htonl(fp
->off
>> 32);
786 args
->fhpluscookie
[pos
++] = htonl(fp
->off
);
787 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
>> 32);
788 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
);
789 args
->fhpluscookie
[pos
] = htonl(NFS_READDIRSIZE
);
791 cc
= rpc_call(fp
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READDIR
,
792 args
, 6 * sizeof(uint32_t) +
793 roundup(fp
->fhsize
, sizeof(uint32_t)),
794 (void **)&buf
, &pkt
);
799 repl
= (struct nfsv3_readdir_repl
*)buf
;
800 if (repl
->errno
!= 0) {
801 rc
= ntohl(repl
->errno
);
806 fp
->cookie
= ((uint64_t)ntohl(repl
->cookiev0
) << 32) |
807 ntohl(repl
->cookiev1
);
808 buf
+= sizeof (struct nfsv3_readdir_repl
);
810 rent
= (struct nfsv3_readdir_entry
*)buf
;
812 if (rent
->follows
== 0) {
813 /* fid0 is actually eof */
814 if (rent
->fid0
!= 0) {
821 d
->d_namlen
= ntohl(rent
->len
);
822 bcopy(rent
->nameplus
, d
->d_name
, d
->d_namlen
);
823 d
->d_name
[d
->d_namlen
] = '\0';
825 pos
= roundup(d
->d_namlen
, sizeof(uint32_t)) / sizeof(uint32_t);
826 fp
->off
= cookie
= ((uint64_t)ntohl(rent
->nameplus
[pos
]) << 32) |
827 ntohl(rent
->nameplus
[pos
+ 1]);
829 buf
= (char *)&rent
->nameplus
[pos
];