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 4096
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 int nfs_write(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
129 static off_t
nfs_seek(struct open_file
*f
, off_t offset
, int where
);
130 static int nfs_stat(struct open_file
*f
, struct stat
*sb
);
131 static int nfs_readdir(struct open_file
*f
, struct dirent
*d
);
133 struct nfs_iodesc nfs_root_node
;
135 struct fs_ops nfs_fsops
= {
146 static int nfs_read_size
= NFSREAD_MIN_SIZE
;
149 * Improve boot performance over NFS
152 set_nfs_read_size(void)
157 if ((env
= getenv("nfs.read_size")) != NULL
) {
159 nfs_read_size
= strtol(env
, &end
, 0);
160 if (errno
!= 0 || *env
== '\0' || *end
!= '\0') {
161 printf("%s: bad value: \"%s\", defaulting to %d\n",
162 "nfs.read_size", env
, NFSREAD_MIN_SIZE
);
163 nfs_read_size
= NFSREAD_MIN_SIZE
;
166 if (nfs_read_size
< NFSREAD_MIN_SIZE
) {
167 printf("%s: bad value: \"%d\", defaulting to %d\n",
168 "nfs.read_size", nfs_read_size
, NFSREAD_MIN_SIZE
);
169 nfs_read_size
= NFSREAD_MIN_SIZE
;
171 if (nfs_read_size
> NFSREAD_MAX_SIZE
) {
172 printf("%s: bad value: \"%d\", defaulting to %d\n",
173 "nfs.read_size", nfs_read_size
, NFSREAD_MIN_SIZE
);
174 nfs_read_size
= NFSREAD_MAX_SIZE
;
176 snprintf(buf
, sizeof (buf
), "%d", nfs_read_size
);
177 setenv("nfs.read_size", buf
, 1);
181 * Fetch the root file handle (call mount daemon)
182 * Return zero or error number.
185 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
];
204 uint32_t h
[RPC_HEADER_WORDS
];
211 printf("nfs_getrootfh: %s\n", path
);
217 bzero(args
, sizeof(*args
));
219 if (len
> sizeof(args
->path
))
220 len
= sizeof(args
->path
);
221 args
->len
= htonl(len
);
222 bcopy(path
, args
->path
, len
);
223 len
= sizeof(uint32_t) + roundup(len
, sizeof(uint32_t));
225 cc
= rpc_call(d
, RPCPROG_MNT
, RPCMNT_VER3
, RPCMNT_MOUNT
,
226 args
, len
, repl
, sizeof(*repl
));
228 /* errno was set by rpc_call */
230 if (cc
< 2 * sizeof (uint32_t))
232 if (repl
->errno
!= 0)
233 return (ntohl(repl
->errno
));
234 *fhlenp
= ntohl(repl
->fhsize
);
235 bcopy(repl
->fh
, fhp
, *fhlenp
);
242 * Lookup a file. Store handle and attributes.
243 * Return zero or error number.
246 nfs_lookupfh(struct nfs_iodesc
*d
, const char *name
, struct nfs_iodesc
*newfd
)
251 uint32_t fhplusname
[1 +
252 (NFS_V3MAXFHSIZE
+ FNAME_SIZE
) / sizeof(uint32_t)];
257 uint32_t fhplusattr
[(NFS_V3MAXFHSIZE
+
258 2 * (sizeof(uint32_t) +
259 sizeof(struct nfsv3_fattrs
))) / sizeof(uint32_t)];
262 uint32_t h
[RPC_HEADER_WORDS
];
266 uint32_t h
[RPC_HEADER_WORDS
];
273 printf("lookupfh: called\n");
279 bzero(args
, sizeof(*args
));
280 args
->fhsize
= htonl(d
->fhsize
);
281 bcopy(d
->fh
, args
->fhplusname
, d
->fhsize
);
283 if (len
> FNAME_SIZE
)
285 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
286 args
->fhplusname
[pos
++] = htonl(len
);
287 bcopy(name
, &args
->fhplusname
[pos
], len
);
288 len
= sizeof(uint32_t) + pos
* sizeof(uint32_t) +
289 roundup(len
, sizeof(uint32_t));
291 rlen
= sizeof(*repl
);
293 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_LOOKUP
,
294 args
, len
, repl
, rlen
);
296 return (errno
); /* XXX - from rpc_call */
297 if (cc
< 2 * sizeof(uint32_t))
299 if (repl
->errno
!= 0)
300 /* saerrno.h now matches NFS error numbers. */
301 return (ntohl(repl
->errno
));
302 newfd
->fhsize
= ntohl(repl
->fhsize
);
303 bcopy(repl
->fhplusattr
, &newfd
->fh
, newfd
->fhsize
);
304 pos
= roundup(newfd
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
305 if (repl
->fhplusattr
[pos
++] == 0)
307 bcopy(&repl
->fhplusattr
[pos
], &newfd
->fa
, sizeof(newfd
->fa
));
311 #ifndef NFS_NOSYMLINK
313 * Get the destination of a symbolic link.
316 nfs_readlink(struct nfs_iodesc
*d
, char *buf
)
320 u_char fh
[NFS_V3MAXFHSIZE
];
325 struct nfsv3_fattrs fa
;
327 u_char path
[NFS_MAXPATHLEN
];
330 uint32_t h
[RPC_HEADER_WORDS
];
334 uint32_t h
[RPC_HEADER_WORDS
];
341 printf("readlink: called\n");
347 bzero(args
, sizeof(*args
));
348 args
->fhsize
= htonl(d
->fhsize
);
349 bcopy(d
->fh
, args
->fh
, d
->fhsize
);
350 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READLINK
,
351 args
, sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
352 repl
, sizeof(*repl
));
356 if (cc
< 2 * sizeof(uint32_t))
359 if (repl
->errno
!= 0)
360 return (ntohl(repl
->errno
));
365 repl
->len
= ntohl(repl
->len
);
366 if (repl
->len
> NFS_MAXPATHLEN
)
367 return (ENAMETOOLONG
);
369 bcopy(repl
->path
, buf
, repl
->len
);
376 * Read data from a file.
377 * Return transfer count or -1 (and set errno)
380 nfs_readdata(struct nfs_iodesc
*d
, off_t off
, void *addr
, size_t len
)
384 uint32_t fhoffcnt
[NFS_V3MAXFHSIZE
/ sizeof(uint32_t) + 3];
389 struct nfsv3_fattrs fa
;
393 u_char data
[NFSREAD_MAX_SIZE
];
396 uint32_t h
[RPC_HEADER_WORDS
];
400 uint32_t h
[RPC_HEADER_WORDS
];
410 bzero(args
, sizeof(*args
));
411 args
->fhsize
= htonl(d
->fhsize
);
412 bcopy(d
->fh
, args
->fhoffcnt
, d
->fhsize
);
413 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
414 args
->fhoffcnt
[pos
++] = 0;
415 args
->fhoffcnt
[pos
++] = htonl((uint32_t)off
);
416 if (len
> nfs_read_size
)
418 args
->fhoffcnt
[pos
] = htonl((uint32_t)len
);
419 hlen
= offsetof(struct repl
, data
[0]);
421 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READ
,
422 args
, 4 * sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
423 repl
, sizeof(*repl
));
425 /* errno was already set by rpc_call */
431 if (repl
->errno
!= 0) {
432 errno
= ntohl(repl
->errno
);
436 x
= ntohl(repl
->count
);
438 printf("nfsread: short packet, %d < %ld\n", rlen
, x
);
442 bcopy(repl
->data
, addr
, x
);
448 * return zero or error number
451 nfs_open(const char *upath
, struct open_file
*f
)
454 struct nfs_iodesc
*currfd
;
455 char buf
[2 * NFS_V3MAXFHSIZE
+ 3];
459 #ifndef NFS_NOSYMLINK
460 struct nfs_iodesc
*newfd
;
461 struct nfsv3_fattrs
*fa
;
464 char namebuf
[NFS_MAXPATHLEN
+ 1];
465 char linkbuf
[NFS_MAXPATHLEN
+ 1];
471 if (netproto
!= NET_NFS
)
476 printf("nfs_open: %s (rootpath=%s)\n", upath
, rootpath
);
479 printf("no rootpath, no nfs\n");
484 * This is silly - we should look at dv_type but that value is
485 * arch dependant and we can't use it here.
488 if (strcmp(f
->f_dev
->dv_name
, "net") != 0)
491 if (strcmp(f
->f_dev
->dv_name
, "pxe") != 0)
495 if (!(desc
= socktodesc(*(int *)(f
->f_devdata
))))
498 /* Bind to a reserved port. */
499 desc
->myport
= htons(--rpc_port
);
500 desc
->destip
= rootip
;
501 if ((error
= nfs_getrootfh(desc
, rootpath
, &nfs_root_node
.fhsize
,
504 nfs_root_node
.fa
.fa_type
= htonl(NFDIR
);
505 nfs_root_node
.fa
.fa_mode
= htonl(0755);
506 nfs_root_node
.fa
.fa_nlink
= htonl(2);
507 nfs_root_node
.iodesc
= desc
;
509 fh
= &nfs_root_node
.fh
[0];
512 for (i
= 0; i
< nfs_root_node
.fhsize
; i
++, cp
+= 2)
513 sprintf(cp
, "%02x", fh
[i
]);
515 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
516 setenv("boot.nfsroot.path", rootpath
, 1);
517 setenv("boot.nfsroot.nfshandle", buf
, 1);
518 sprintf(buf
, "%d", nfs_root_node
.fhsize
);
519 setenv("boot.nfsroot.nfshandlelen", buf
, 1);
521 /* Allocate file system specific data structure */
522 currfd
= malloc(sizeof(*newfd
));
523 if (currfd
== NULL
) {
527 #ifndef NFS_NOSYMLINK
528 bcopy(&nfs_root_node
, currfd
, sizeof(*currfd
));
531 cp
= path
= strdup(upath
);
538 * Remove extra separators
546 * Check that current node is a directory.
548 if (currfd
->fa
.fa_type
!= htonl(NFDIR
)) {
553 /* allocate file system specific data structure */
554 newfd
= malloc(sizeof(*newfd
));
559 newfd
->iodesc
= currfd
->iodesc
;
562 * Get next component of path name.
568 while ((c
= *cp
) != '\0' && c
!= '/') {
569 if (++len
> NFS_MAXNAMLEN
) {
578 /* lookup a file handle */
579 error
= nfs_lookupfh(currfd
, ncp
, newfd
);
585 * Check for symbolic link
587 if (newfd
->fa
.fa_type
== htonl(NFLNK
)) {
590 error
= nfs_readlink(newfd
, linkbuf
);
594 link_len
= strlen(linkbuf
);
597 if (link_len
+ len
> MAXPATHLEN
598 || ++nlinks
> MAXSYMLINKS
) {
603 bcopy(cp
, &namebuf
[link_len
], len
+ 1);
604 bcopy(linkbuf
, namebuf
, link_len
);
607 * If absolute pathname, restart at root.
608 * If relative pathname, restart at parent directory.
612 bcopy(&nfs_root_node
, currfd
, sizeof(*currfd
));
631 currfd
->iodesc
= desc
;
633 error
= nfs_lookupfh(&nfs_root_node
, upath
, currfd
);
638 f
->f_fsdata
= (void *)currfd
;
644 printf("nfs_open: %s lookupfh failed: %s\n",
645 path
, strerror(error
));
653 nfs_close(struct open_file
*f
)
655 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
659 printf("nfs_close: fp=0x%lx\n", (u_long
)fp
);
664 f
->f_fsdata
= (void *)0;
670 * read a portion of a file
673 nfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
675 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
681 printf("nfs_read: size=%lu off=%d\n", (u_long
)size
,
684 while ((int)size
> 0) {
686 cc
= nfs_readdata(fp
, fp
->off
, (void *)addr
, size
);
687 /* XXX maybe should retry on certain errors */
691 printf("nfs_read: read: %s", strerror(errno
));
693 return (errno
); /* XXX - from nfs_readdata */
698 printf("nfs_read: hit EOF unexpectantly");
717 nfs_write(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
723 nfs_seek(struct open_file
*f
, off_t offset
, int where
)
725 struct nfs_iodesc
*d
= (struct nfs_iodesc
*)f
->f_fsdata
;
726 uint32_t size
= ntohl(d
->fa
.fa_size
.val
[1]);
736 d
->off
= size
- offset
;
746 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
747 int nfs_stat_types
[9] = {
748 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
, S_IFSOCK
, S_IFIFO
, 0 };
751 nfs_stat(struct open_file
*f
, struct stat
*sb
)
753 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
754 uint32_t ftype
, mode
;
756 ftype
= ntohl(fp
->fa
.fa_type
);
757 mode
= ntohl(fp
->fa
.fa_mode
);
758 mode
|= nfs_stat_types
[ftype
& 7];
761 sb
->st_nlink
= ntohl(fp
->fa
.fa_nlink
);
762 sb
->st_uid
= ntohl(fp
->fa
.fa_uid
);
763 sb
->st_gid
= ntohl(fp
->fa
.fa_gid
);
764 sb
->st_size
= ntohl(fp
->fa
.fa_size
.val
[1]);
770 nfs_readdir(struct open_file
*f
, struct dirent
*d
)
772 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
773 struct nfsv3_readdir_repl
*repl
;
774 struct nfsv3_readdir_entry
*rent
;
776 static struct nfs_iodesc
*pfp
= NULL
;
777 static uint64_t cookie
= 0;
783 uint32_t fhpluscookie
[5 + NFS_V3MAXFHSIZE
];
786 uint32_t h
[RPC_HEADER_WORDS
];
790 uint32_t h
[RPC_HEADER_WORDS
];
791 u_char d
[NFS_READDIRSIZE
];
794 if (fp
!= pfp
|| fp
->off
!= cookie
) {
798 bzero(args
, sizeof(*args
));
800 args
->fhsize
= htonl(fp
->fhsize
);
801 bcopy(fp
->fh
, args
->fhpluscookie
, fp
->fhsize
);
802 pos
= roundup(fp
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
803 args
->fhpluscookie
[pos
++] = htonl(fp
->off
>> 32);
804 args
->fhpluscookie
[pos
++] = htonl(fp
->off
);
805 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
>> 32);
806 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
);
807 args
->fhpluscookie
[pos
] = htonl(NFS_READDIRSIZE
);
809 cc
= rpc_call(fp
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READDIR
,
810 args
, 6 * sizeof(uint32_t) +
811 roundup(fp
->fhsize
, sizeof(uint32_t)),
812 rdata
.d
, sizeof(rdata
.d
));
814 repl
= (struct nfsv3_readdir_repl
*)buf
;
815 if (repl
->errno
!= 0)
816 return (ntohl(repl
->errno
));
819 fp
->cookie
= ((uint64_t)ntohl(repl
->cookiev0
) << 32) |
820 ntohl(repl
->cookiev1
);
821 buf
+= sizeof (struct nfsv3_readdir_repl
);
823 rent
= (struct nfsv3_readdir_entry
*)buf
;
825 if (rent
->follows
== 0) {
826 /* fid0 is actually eof */
827 if (rent
->fid0
!= 0) {
834 d
->d_namlen
= ntohl(rent
->len
);
835 bcopy(rent
->nameplus
, d
->d_name
, d
->d_namlen
);
836 d
->d_name
[d
->d_namlen
] = '\0';
838 pos
= roundup(d
->d_namlen
, sizeof(uint32_t)) / sizeof(uint32_t);
839 fp
->off
= cookie
= ((uint64_t)ntohl(rent
->nameplus
[pos
]) << 32) |
840 ntohl(rent
->nameplus
[pos
+ 1]);
842 buf
= (u_char
*)&rent
->nameplus
[pos
];