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
)
191 char path
[FNAME_SIZE
];
196 u_char fh
[NFS_V3MAXFHSIZE
];
201 uint32_t h
[RPC_HEADER_WORDS
];
208 printf("nfs_getrootfh: %s\n", path
);
213 bzero(args
, sizeof(*args
));
215 if (len
> sizeof(args
->path
))
216 len
= sizeof(args
->path
);
217 args
->len
= htonl(len
);
218 bcopy(path
, args
->path
, len
);
219 len
= sizeof(uint32_t) + roundup(len
, sizeof(uint32_t));
221 cc
= rpc_call(d
, RPCPROG_MNT
, RPCMNT_VER3
, RPCMNT_MOUNT
,
222 args
, len
, (void **)&repl
, &pkt
);
225 /* errno was set by rpc_call */
228 if (cc
< 2 * sizeof (uint32_t)) {
232 if (repl
->errno
!= 0) {
234 return (ntohl(repl
->errno
));
236 *fhlenp
= ntohl(repl
->fhsize
);
237 bcopy(repl
->fh
, fhp
, *fhlenp
);
245 * Lookup a file. Store handle and attributes.
246 * Return zero or error number.
249 nfs_lookupfh(struct nfs_iodesc
*d
, const char *name
, struct nfs_iodesc
*newfd
)
255 uint32_t fhplusname
[1 +
256 (NFS_V3MAXFHSIZE
+ FNAME_SIZE
) / sizeof(uint32_t)];
261 uint32_t fhplusattr
[(NFS_V3MAXFHSIZE
+
262 2 * (sizeof(uint32_t) +
263 sizeof(struct nfsv3_fattrs
))) / sizeof(uint32_t)];
266 uint32_t h
[RPC_HEADER_WORDS
];
273 printf("lookupfh: called\n");
278 bzero(args
, sizeof(*args
));
279 args
->fhsize
= htonl(d
->fhsize
);
280 bcopy(d
->fh
, args
->fhplusname
, d
->fhsize
);
282 if (len
> FNAME_SIZE
)
284 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
285 args
->fhplusname
[pos
++] = htonl(len
);
286 bcopy(name
, &args
->fhplusname
[pos
], len
);
287 len
= sizeof(uint32_t) + pos
* sizeof(uint32_t) +
288 roundup(len
, sizeof(uint32_t));
290 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_LOOKUP
,
291 args
, len
, (void **)&repl
, &pkt
);
294 return (errno
); /* XXX - from rpc_call */
296 if (cc
< 2 * sizeof(uint32_t)) {
300 if (repl
->errno
!= 0) {
302 /* saerrno.h now matches NFS error numbers. */
303 return (ntohl(repl
->errno
));
305 newfd
->fhsize
= ntohl(repl
->fhsize
);
306 bcopy(repl
->fhplusattr
, &newfd
->fh
, newfd
->fhsize
);
307 pos
= roundup(newfd
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
308 if (repl
->fhplusattr
[pos
++] == 0) {
312 bcopy(&repl
->fhplusattr
[pos
], &newfd
->fa
, sizeof(newfd
->fa
));
318 * Get the destination of a symbolic link.
321 nfs_readlink(struct nfs_iodesc
*d
, char *buf
)
326 u_char fh
[NFS_V3MAXFHSIZE
];
331 struct nfsv3_fattrs fa
;
333 u_char path
[NFS_MAXPATHLEN
];
336 uint32_t h
[RPC_HEADER_WORDS
];
344 printf("readlink: called\n");
349 bzero(args
, sizeof(*args
));
350 args
->fhsize
= htonl(d
->fhsize
);
351 bcopy(d
->fh
, args
->fh
, d
->fhsize
);
352 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READLINK
,
353 args
, sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
354 (void **)&repl
, &pkt
);
358 if (cc
< 2 * sizeof(uint32_t)) {
363 if (repl
->errno
!= 0) {
364 rc
= ntohl(repl
->errno
);
373 repl
->len
= ntohl(repl
->len
);
374 if (repl
->len
> NFS_MAXPATHLEN
) {
379 bcopy(repl
->path
, buf
, repl
->len
);
387 * Read data from a file.
388 * Return transfer count or -1 (and set errno)
391 nfs_readdata(struct nfs_iodesc
*d
, off_t off
, void *addr
, size_t len
)
396 uint32_t fhoffcnt
[NFS_V3MAXFHSIZE
/ sizeof(uint32_t) + 3];
401 struct nfsv3_fattrs fa
;
405 u_char data
[NFSREAD_MAX_SIZE
];
408 uint32_t h
[RPC_HEADER_WORDS
];
417 bzero(args
, sizeof(*args
));
418 args
->fhsize
= htonl(d
->fhsize
);
419 bcopy(d
->fh
, args
->fhoffcnt
, d
->fhsize
);
420 pos
= roundup(d
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
421 args
->fhoffcnt
[pos
++] = 0;
422 args
->fhoffcnt
[pos
++] = htonl((uint32_t)off
);
423 if (len
> nfs_read_size
)
425 args
->fhoffcnt
[pos
] = htonl((uint32_t)len
);
426 hlen
= offsetof(struct repl
, data
[0]);
428 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READ
,
429 args
, 4 * sizeof(uint32_t) + roundup(d
->fhsize
, sizeof(uint32_t)),
430 (void **)&repl
, &pkt
);
432 /* errno was already set by rpc_call */
440 if (repl
->errno
!= 0) {
441 errno
= ntohl(repl
->errno
);
446 x
= ntohl(repl
->count
);
448 printf("nfsread: short packet, %d < %ld\n", rlen
, x
);
453 bcopy(repl
->data
, addr
, x
);
460 * return zero or error number
463 nfs_open(const char *upath
, struct open_file
*f
)
466 struct nfs_iodesc
*currfd
;
467 char buf
[2 * NFS_V3MAXFHSIZE
+ 3];
471 struct nfs_iodesc
*newfd
;
472 struct nfsv3_fattrs
*fa
;
475 char namebuf
[NFS_MAXPATHLEN
+ 1];
476 char linkbuf
[NFS_MAXPATHLEN
+ 1];
481 if (netproto
!= NET_NFS
)
486 printf("nfs_open: %s (rootpath=%s)\n", upath
, rootpath
);
489 printf("no rootpath, no nfs\n");
493 if (f
->f_dev
->dv_type
!= DEVT_NET
)
496 if (!(desc
= socktodesc(*(int *)(f
->f_devdata
))))
499 /* Bind to a reserved port. */
500 desc
->myport
= htons(--rpc_port
);
501 desc
->destip
= rootip
;
502 if ((error
= nfs_getrootfh(desc
, rootpath
, &nfs_root_node
.fhsize
,
505 nfs_root_node
.fa
.fa_type
= htonl(NFDIR
);
506 nfs_root_node
.fa
.fa_mode
= htonl(0755);
507 nfs_root_node
.fa
.fa_nlink
= htonl(2);
508 nfs_root_node
.iodesc
= desc
;
510 fh
= &nfs_root_node
.fh
[0];
513 for (i
= 0; i
< nfs_root_node
.fhsize
; i
++, cp
+= 2)
514 sprintf(cp
, "%02x", fh
[i
]);
516 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
517 setenv("boot.nfsroot.path", rootpath
, 1);
518 setenv("boot.nfsroot.nfshandle", buf
, 1);
519 sprintf(buf
, "%d", nfs_root_node
.fhsize
);
520 setenv("boot.nfsroot.nfshandlelen", buf
, 1);
522 /* Allocate file system specific data structure */
523 currfd
= malloc(sizeof(*newfd
));
524 if (currfd
== NULL
) {
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
));
633 f
->f_fsdata
= (void *)currfd
;
639 printf("nfs_open: %s lookupfh failed: %s\n",
640 path
, strerror(error
));
648 nfs_close(struct open_file
*f
)
650 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
654 printf("nfs_close: fp=0x%lx\n", (u_long
)fp
);
664 * read a portion of a file
667 nfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
669 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
675 printf("nfs_read: size=%lu off=%d\n", (u_long
)size
,
678 while ((int)size
> 0) {
680 cc
= nfs_readdata(fp
, fp
->off
, (void *)addr
, size
);
681 /* XXX maybe should retry on certain errors */
685 printf("nfs_read: read: %s", strerror(errno
));
687 return (errno
); /* XXX - from nfs_readdata */
692 printf("nfs_read: hit EOF unexpectantly");
711 nfs_write(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
717 nfs_seek(struct open_file
*f
, off_t offset
, int where
)
719 struct nfs_iodesc
*d
= (struct nfs_iodesc
*)f
->f_fsdata
;
720 uint32_t size
= ntohl(d
->fa
.fa_size
.val
[1]);
730 d
->off
= size
- offset
;
740 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5, NFSOCK=6, NFFIFO=7 */
741 int nfs_stat_types
[9] = {
742 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
, S_IFSOCK
, S_IFIFO
, 0 };
745 nfs_stat(struct open_file
*f
, struct stat
*sb
)
747 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
748 uint32_t ftype
, mode
;
750 ftype
= ntohl(fp
->fa
.fa_type
);
751 mode
= ntohl(fp
->fa
.fa_mode
);
752 mode
|= nfs_stat_types
[ftype
& 7];
755 sb
->st_nlink
= ntohl(fp
->fa
.fa_nlink
);
756 sb
->st_uid
= ntohl(fp
->fa
.fa_uid
);
757 sb
->st_gid
= ntohl(fp
->fa
.fa_gid
);
758 sb
->st_size
= ntohl(fp
->fa
.fa_size
.val
[1]);
764 nfs_readdir(struct open_file
*f
, struct dirent
*d
)
766 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
767 struct nfsv3_readdir_repl
*repl
;
768 struct nfsv3_readdir_entry
*rent
;
769 static void *pkt
= NULL
;
771 static struct nfs_iodesc
*pfp
= NULL
;
772 static uint64_t cookie
= 0;
778 uint32_t fhpluscookie
[5 + NFS_V3MAXFHSIZE
];
781 uint32_t h
[RPC_HEADER_WORDS
];
785 if (fp
!= pfp
|| fp
->off
!= cookie
) {
791 bzero(args
, sizeof(*args
));
793 args
->fhsize
= htonl(fp
->fhsize
);
794 bcopy(fp
->fh
, args
->fhpluscookie
, fp
->fhsize
);
795 pos
= roundup(fp
->fhsize
, sizeof(uint32_t)) / sizeof(uint32_t);
796 args
->fhpluscookie
[pos
++] = htonl(fp
->off
>> 32);
797 args
->fhpluscookie
[pos
++] = htonl(fp
->off
);
798 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
>> 32);
799 args
->fhpluscookie
[pos
++] = htonl(fp
->cookie
);
800 args
->fhpluscookie
[pos
] = htonl(NFS_READDIRSIZE
);
802 cc
= rpc_call(fp
->iodesc
, NFS_PROG
, NFS_VER3
, NFSPROCV3_READDIR
,
803 args
, 6 * sizeof(uint32_t) +
804 roundup(fp
->fhsize
, sizeof(uint32_t)),
805 (void **)&buf
, &pkt
);
810 repl
= (struct nfsv3_readdir_repl
*)buf
;
811 if (repl
->errno
!= 0) {
812 rc
= ntohl(repl
->errno
);
817 fp
->cookie
= ((uint64_t)ntohl(repl
->cookiev0
) << 32) |
818 ntohl(repl
->cookiev1
);
819 buf
+= sizeof (struct nfsv3_readdir_repl
);
821 rent
= (struct nfsv3_readdir_entry
*)buf
;
823 if (rent
->follows
== 0) {
824 /* fid0 is actually eof */
825 if (rent
->fid0
!= 0) {
832 d
->d_namlen
= ntohl(rent
->len
);
833 bcopy(rent
->nameplus
, d
->d_name
, d
->d_namlen
);
834 d
->d_name
[d
->d_namlen
] = '\0';
836 pos
= roundup(d
->d_namlen
, sizeof(uint32_t)) / sizeof(uint32_t);
837 fp
->off
= cookie
= ((uint64_t)ntohl(rent
->nameplus
[pos
]) << 32) |
838 ntohl(rent
->nameplus
[pos
+ 1]);
840 buf
= (u_char
*)&rent
->nameplus
[pos
];