4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Stuff relating to NFSv3 directory reading ...
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <rpc/types.h>
35 #include <rpc/rpc_msg.h>
36 #include <sys/t_lock.h>
40 #include <rpcsvc/nfs_prot.h>
41 #include <sys/types.h>
43 #include <sys/bootvfs.h>
44 #include <sys/sysmacros.h>
45 #include "socket_inet.h"
46 #include <sys/salib.h>
47 #include <sys/bootdebug.h>
53 * Boot needs to be cleaned up to use either dirent32 or dirent64,
54 * in the meantime use dirent_t and always round to 8 bytes
56 #define BDIRENT_RECLEN(namelen) \
57 ((offsetof(dirent_t, d_name[0]) + 1 + (namelen) + 7) & ~ 7)
59 #define dprintf if (boothowto & RB_DEBUG) printf
62 * Get directory entries:
64 * Uses the nfs "READDIR" operation to read directory entries
65 * into a local buffer. These are then translated into file
66 * system independent "dirent" structs and returned in the
67 * caller's buffer. Returns the number of entries converted
68 * (-1 if there's an error).
70 * Although the xdr functions can allocate memory, we have
71 * a limited heap so we allocate our own space,
72 * assuming the worst case of 256 byte names.
73 * This is a space hog in our local buffer, so we want
74 * the number of buffers to be small. To make sure we don't
75 * get more names than we can handle, we tell the rpc
76 * routine that we only have space for MAXDENT names if
77 * they are all the minimum size. This keeps the return
78 * packet unfragmented, but may result in lots of reads
79 * to process a large directory. Since this is standalone
80 * we don't worry about speed. With MAXDENTs at 16, the
85 nfs3getdents(struct nfs_file
*nfp
, struct dirent
*dep
, unsigned size
)
91 enum clnt_stat status
;
93 entry3 etlist
[MAXDENTS
];
94 char names
[MAXDENTS
][NFS_MAXNAMLEN
+1];
97 struct timeval zero_timeout
= {0, 0}; /* default */
99 bzero((caddr_t
)&res
, sizeof (res
));
100 bzero((caddr_t
)&rda
, sizeof (rda
));
101 bzero((caddr_t
)rdbuf
.etlist
, sizeof (rdbuf
.etlist
));
103 rda
.dir
.data
.data_len
= nfp
->fh
.fh3
.len
;
104 rda
.dir
.data
.data_val
= nfp
->fh
.fh3
.data
;
105 rda
.cookie
= nfp
->cookie
.cookie3
;
107 while (!res
.READDIR3res_u
.resok
.reply
.eof
) {
109 * Keep issuing nfs calls until EOF is reached on
110 * the directory or the user buffer is filled.
113 for (j
= 0; j
< MAXDENTS
; j
++) {
115 * Link our buffers together for the benefit of
116 * XDR. We do this each time we issue the rpc call
118 * routines screw up the linkage!
121 rdbuf
.etlist
[j
].name
= rdbuf
.names
[(MAXDENTS
-1) - j
];
122 rdbuf
.etlist
[j
].nextentry
=
123 (j
< (MAXDENTS
-1)) ? &rdbuf
.etlist
[j
+1] : 0;
126 res
.READDIR3res_u
.resok
.reply
.entries
= rdbuf
.etlist
;
128 * Cannot give the whole buffer unless every name is
129 * 256 bytes! Assume the worst case of all 1 byte names.
130 * This results in MINSIZ bytes/name in the xdr stream.
132 rda
.count
= sizeof (res
) + MAXDENTS
*MINSIZ
;
133 bzero((caddr_t
)rdbuf
.names
, sizeof (rdbuf
.names
));
135 status
= CLNT_CALL(root_CLIENT
, NFSPROC3_READDIR
,
136 xdr_READDIR3args
, (caddr_t
)&rda
,
137 xdr_READDIR3res
, (caddr_t
)&res
, zero_timeout
);
139 if (status
!= RPC_SUCCESS
) {
140 dprintf("nfs3_getdents: RPC error\n");
143 if (res
.status
!= NFS3_OK
) {
145 * The most common failure here would be trying to
146 * issue a getdents call on a non-directory!
149 nfs3_error(res
.status
);
153 for (ep
= rdbuf
.etlist
; ep
; ep
= ep
->nextentry
) {
155 * Step thru all entries returned by NFS, converting
156 * to the cannonical form and copying out to the
163 * catch the case user called at EOF
165 if ((n
= strlen(ep
->name
)) == 0)
168 n
= BDIRENT_RECLEN(n
);
174 (void) strcpy(dep
->d_name
, ep
->name
);
175 dep
->d_ino
= ep
->fileid
;
176 dep
->d_off
= (off_t
)ep
->cookie
;
177 dep
->d_reclen
= (ushort_t
)n
;
179 dep
= (struct dirent
*)((char *)dep
+ n
);
180 rda
.cookie
= ep
->cookie
;
181 nfp
->cookie
.cookie3
= ep
->cookie
;