1 /* $FreeBSD: src/lib/libstand/nfs.c,v 1.2.6.3 2000/09/10 01:33:25 ps Exp $ */
2 /* $DragonFly: src/lib/libstand/nfs.c,v 1.4 2005/12/11 03:27:26 swildner Exp $ */
3 /* $NetBSD: nfs.c,v 1.2 1998/01/24 12:43:09 drochner Exp $ */
6 * Copyright (c) 1993 John Brezak
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/param.h>
35 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
52 /* Define our own NFS attributes without NQNFS stuff. */
65 struct nfsv2_time fa_atime
;
66 struct nfsv2_time fa_mtime
;
67 struct nfsv2_time fa_ctime
;
71 struct nfs_read_args
{
72 u_char fh
[NFS_FHSIZE
];
75 n_long xxx
; /* XXX what's this for? */
78 /* Data part of nfs rpc reply (also the largest thing we receive) */
79 #define NFSREAD_SIZE 1024
80 struct nfs_read_repl
{
82 struct nfsv2_fattrs fa
;
84 u_char data
[NFSREAD_SIZE
];
88 struct nfs_readlnk_repl
{
91 char path
[NFS_MAXPATHLEN
];
95 struct nfs_readdir_args
{
96 u_char fh
[NFS_FHSIZE
];
101 struct nfs_readdir_data
{
107 struct nfs_readdir_off
{
113 struct iodesc
*iodesc
;
115 u_char fh
[NFS_FHSIZE
];
116 struct nfsv2_fattrs fa
; /* all in network order */
120 * XXX interactions with tftp? See nfswrapper.c for a confusing
123 int nfs_open(const char *path
, struct open_file
*f
);
124 static int nfs_close(struct open_file
*f
);
125 static int nfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
126 static int nfs_write(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
127 static off_t
nfs_seek(struct open_file
*f
, off_t offset
, int where
);
128 static int nfs_stat(struct open_file
*f
, struct stat
*sb
);
129 static int nfs_readdir(struct open_file
*f
, struct dirent
*d
);
131 struct nfs_iodesc nfs_root_node
;
133 struct fs_ops nfs_fsops
= {
145 * Fetch the root file handle (call mount daemon)
146 * Return zero or error number.
149 nfs_getrootfh(struct iodesc
*d
, char *path
, u_char
*fhp
)
154 char path
[FNAME_SIZE
];
158 u_char fh
[NFS_FHSIZE
];
161 n_long h
[RPC_HEADER_WORDS
];
165 n_long h
[RPC_HEADER_WORDS
];
172 printf("nfs_getrootfh: %s\n", path
);
178 bzero(args
, sizeof(*args
));
180 if (len
> sizeof(args
->path
))
181 len
= sizeof(args
->path
);
182 args
->len
= htonl(len
);
183 bcopy(path
, args
->path
, len
);
184 len
= 4 + roundup(len
, 4);
186 cc
= rpc_call(d
, RPCPROG_MNT
, RPCMNT_VER1
, RPCMNT_MOUNT
,
187 args
, len
, repl
, sizeof(*repl
));
189 /* errno was set by rpc_call */
195 return (ntohl(repl
->errno
));
196 bcopy(repl
->fh
, fhp
, sizeof(repl
->fh
));
201 * Lookup a file. Store handle and attributes.
202 * Return zero or error number.
205 nfs_lookupfh(struct nfs_iodesc
*d
, const char *name
, struct nfs_iodesc
*newfd
)
209 u_char fh
[NFS_FHSIZE
];
211 char name
[FNAME_SIZE
];
215 u_char fh
[NFS_FHSIZE
];
216 struct nfsv2_fattrs fa
;
219 n_long h
[RPC_HEADER_WORDS
];
223 n_long h
[RPC_HEADER_WORDS
];
230 printf("lookupfh: called\n");
236 bzero(args
, sizeof(*args
));
237 bcopy(d
->fh
, args
->fh
, sizeof(args
->fh
));
239 if (len
> sizeof(args
->name
))
240 len
= sizeof(args
->name
);
241 bcopy(name
, args
->name
, len
);
242 args
->len
= htonl(len
);
243 len
= 4 + roundup(len
, 4);
246 rlen
= sizeof(*repl
);
248 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER2
, NFSPROC_LOOKUP
,
249 args
, len
, repl
, rlen
);
251 return (errno
); /* XXX - from rpc_call */
255 /* saerrno.h now matches NFS error numbers. */
256 return (ntohl(repl
->errno
));
258 bcopy( repl
->fh
, &newfd
->fh
, sizeof(newfd
->fh
));
259 bcopy(&repl
->fa
, &newfd
->fa
, sizeof(newfd
->fa
));
263 #ifndef NFS_NOSYMLINK
265 * Get the destination of a symbolic link.
268 nfs_readlink(struct nfs_iodesc
*d
, char *buf
)
271 n_long h
[RPC_HEADER_WORDS
];
272 u_char fh
[NFS_FHSIZE
];
275 n_long h
[RPC_HEADER_WORDS
];
276 struct nfs_readlnk_repl d
;
282 printf("readlink: called\n");
285 bcopy(d
->fh
, sdata
.fh
, NFS_FHSIZE
);
286 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER2
, NFSPROC_READLINK
,
287 sdata
.fh
, NFS_FHSIZE
,
288 &rdata
.d
, sizeof(rdata
.d
));
296 return (ntohl(rdata
.d
.errno
));
298 rdata
.d
.len
= ntohl(rdata
.d
.len
);
299 if (rdata
.d
.len
> NFS_MAXPATHLEN
)
300 return (ENAMETOOLONG
);
302 bcopy(rdata
.d
.path
, buf
, rdata
.d
.len
);
303 buf
[rdata
.d
.len
] = 0;
309 * Read data from a file.
310 * Return transfer count or -1 (and set errno)
313 nfs_readdata(struct nfs_iodesc
*d
, off_t off
, void *addr
, size_t len
)
315 struct nfs_read_args
*args
;
316 struct nfs_read_repl
*repl
;
318 n_long h
[RPC_HEADER_WORDS
];
319 struct nfs_read_args d
;
322 n_long h
[RPC_HEADER_WORDS
];
323 struct nfs_read_repl d
;
332 bcopy(d
->fh
, args
->fh
, NFS_FHSIZE
);
333 args
->off
= htonl((n_long
)off
);
334 if (len
> NFSREAD_SIZE
)
336 args
->len
= htonl((n_long
)len
);
337 args
->xxx
= htonl((n_long
)0);
338 hlen
= sizeof(*repl
) - NFSREAD_SIZE
;
340 cc
= rpc_call(d
->iodesc
, NFS_PROG
, NFS_VER2
, NFSPROC_READ
,
342 repl
, sizeof(*repl
));
344 /* errno was already set by rpc_call */
352 errno
= ntohl(repl
->errno
);
356 x
= ntohl(repl
->count
);
358 printf("nfsread: short packet, %d < %ld\n", rlen
, x
);
362 bcopy(repl
->data
, addr
, x
);
368 * return zero or error number
371 nfs_open(const char *upath
, struct open_file
*f
)
374 struct nfs_iodesc
*currfd
;
375 #ifndef NFS_NOSYMLINK
376 struct nfs_iodesc
*newfd
;
377 struct nfsv2_fattrs
*fa
;
380 char namebuf
[NFS_MAXPATHLEN
+ 1];
381 char linkbuf
[NFS_MAXPATHLEN
+ 1];
389 printf("nfs_open: %s (rootpath=%s)\n", path
, rootpath
);
392 printf("no rootpath, no nfs\n");
396 if (!(desc
= socktodesc(*(int *)(f
->f_devdata
))))
399 /* Bind to a reserved port. */
400 desc
->myport
= htons(--rpc_port
);
401 desc
->destip
= rootip
;
402 if ((error
= nfs_getrootfh(desc
, rootpath
, nfs_root_node
.fh
)))
404 nfs_root_node
.iodesc
= desc
;
406 #ifndef NFS_NOSYMLINK
407 /* Fake up attributes for the root dir. */
408 fa
= &nfs_root_node
.fa
;
409 fa
->fa_type
= htonl(NFDIR
);
410 fa
->fa_mode
= htonl(0755);
411 fa
->fa_nlink
= htonl(2);
413 currfd
= &nfs_root_node
;
416 cp
= path
= strdup(upath
);
423 * Remove extra separators
431 * Check that current node is a directory.
433 if (currfd
->fa
.fa_type
!= htonl(NFDIR
)) {
438 /* allocate file system specific data structure */
439 newfd
= malloc(sizeof(*newfd
));
440 newfd
->iodesc
= currfd
->iodesc
;
444 * Get next component of path name.
450 while ((c
= *cp
) != '\0' && c
!= '/') {
451 if (++len
> NFS_MAXNAMLEN
) {
460 /* lookup a file handle */
461 error
= nfs_lookupfh(currfd
, ncp
, newfd
);
467 * Check for symbolic link
469 if (newfd
->fa
.fa_type
== htonl(NFLNK
)) {
472 error
= nfs_readlink(newfd
, linkbuf
);
476 link_len
= strlen(linkbuf
);
479 if (link_len
+ len
> MAXPATHLEN
480 || ++nlinks
> MAXSYMLINKS
) {
485 bcopy(cp
, &namebuf
[link_len
], len
+ 1);
486 bcopy(linkbuf
, namebuf
, link_len
);
489 * If absolute pathname, restart at root.
490 * If relative pathname, restart at parent directory.
494 if (currfd
!= &nfs_root_node
)
496 currfd
= &nfs_root_node
;
505 if (currfd
!= &nfs_root_node
)
519 /* allocate file system specific data structure */
520 currfd
= malloc(sizeof(*currfd
));
521 currfd
->iodesc
= desc
;
524 error
= nfs_lookupfh(&nfs_root_node
, upath
, currfd
);
527 f
->f_fsdata
= (void *)currfd
;
533 printf("nfs_open: %s lookupfh failed: %s\n",
534 path
, strerror(error
));
536 #ifndef NFS_NOSYMLINK
537 if (currfd
!= &nfs_root_node
)
545 nfs_close(struct open_file
*f
)
547 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
551 printf("nfs_close: fp=0x%lx\n", (u_long
)fp
);
554 if (fp
!= &nfs_root_node
&& fp
)
556 f
->f_fsdata
= (void *)0;
562 * read a portion of a file
568 nfs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
570 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
576 printf("nfs_read: size=%lu off=%d\n", (u_long
)size
,
579 while ((int)size
> 0) {
581 cc
= nfs_readdata(fp
, fp
->off
, (void *)addr
, size
);
582 /* XXX maybe should retry on certain errors */
586 printf("nfs_read: read: %s", strerror(errno
));
588 return (errno
); /* XXX - from nfs_readdata */
593 printf("nfs_read: hit EOF unexpectantly");
615 nfs_write(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
)
621 nfs_seek(struct open_file
*f
, off_t offset
, int where
)
623 struct nfs_iodesc
*d
= (struct nfs_iodesc
*)f
->f_fsdata
;
624 n_long size
= ntohl(d
->fa
.fa_size
);
634 d
->off
= size
- offset
;
643 /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
644 int nfs_stat_types
[8] = {
645 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
, 0 };
648 nfs_stat(struct open_file
*f
, struct stat
*sb
)
650 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
653 ftype
= ntohl(fp
->fa
.fa_type
);
654 mode
= ntohl(fp
->fa
.fa_mode
);
655 mode
|= nfs_stat_types
[ftype
& 7];
658 sb
->st_nlink
= ntohl(fp
->fa
.fa_nlink
);
659 sb
->st_uid
= ntohl(fp
->fa
.fa_uid
);
660 sb
->st_gid
= ntohl(fp
->fa
.fa_gid
);
661 sb
->st_size
= ntohl(fp
->fa
.fa_size
);
667 nfs_readdir(struct open_file
*f
, struct dirent
*d
)
669 struct nfs_iodesc
*fp
= (struct nfs_iodesc
*)f
->f_fsdata
;
670 struct nfs_readdir_args
*args
;
671 struct nfs_readdir_data
*rd
;
672 struct nfs_readdir_off
*roff
= NULL
;
674 static n_long cookie
= 0;
679 n_long h
[RPC_HEADER_WORDS
];
680 struct nfs_readdir_args d
;
683 n_long h
[RPC_HEADER_WORDS
];
684 u_char d
[NFS_READDIRSIZE
];
690 bzero(args
, sizeof(*args
));
692 bcopy(fp
->fh
, args
->fh
, NFS_FHSIZE
);
693 args
->cookie
= htonl(cookie
);
694 args
->count
= htonl(NFS_READDIRSIZE
);
696 cc
= rpc_call(fp
->iodesc
, NFS_PROG
, NFS_VER2
, NFSPROC_READDIR
,
698 rdata
.d
, sizeof(rdata
.d
));
700 roff
= (struct nfs_readdir_off
*)buf
;
701 if (ntohl(roff
->cookie
) != 0)
704 roff
= (struct nfs_readdir_off
*)buf
;
706 if (ntohl(roff
->follows
) == 0) {
707 eof
= ntohl((roff
+1)->cookie
);
715 buf
+= sizeof(struct nfs_readdir_off
);
716 rd
= (struct nfs_readdir_data
*)buf
;
717 d
->d_namlen
= ntohl(rd
->len
);
718 bcopy(rd
->name
, d
->d_name
, d
->d_namlen
);
719 d
->d_name
[d
->d_namlen
] = '\0';
721 buf
+= (sizeof(struct nfs_readdir_data
) + roundup(htonl(rd
->len
),4));
722 roff
= (struct nfs_readdir_off
*)buf
;
723 cookie
= ntohl(roff
->cookie
);