1 /*
2 * CDDL HEADER START
3 *
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
7 * with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Stuff relating to NFSv3 directory reading ...
27 */
28
29 #include <rpc/types.h>
30 #include <rpc/auth.h>
31 #include <rpc/xdr.h>
32 #include "clnt.h"
33 #include <rpc/rpc_msg.h>
34 #include <sys/t_lock.h>
35 #include "nfs_inet.h"
36 #include <rpc/rpc.h>
37 #include "brpc.h"
38 #include <rpcsvc/nfs_prot.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/bootvfs.h>
42 #include <sys/sysmacros.h>
43 #include "socket_inet.h"
44 #include <sys/salib.h>
45 #include <sys/bootdebug.h>
46
47 #define MAXDENTS 16
48 #define MINSIZ 20
49
50 /*
51 * Boot needs to be cleaned up to use either dirent32 or dirent64,
52 * in the meantime use dirent_t and always round to 8 bytes
53 */
54 #define BDIRENT_RECLEN(namelen) \
55 ((offsetof(dirent_t, d_name[0]) + 1 + (namelen) + 7) & ~ 7)
56
57 #define dprintf if (boothowto & RB_DEBUG) printf
58
59 /*
60 * Get directory entries:
61 *
62 * Uses the nfs "READDIR" operation to read directory entries
63 * into a local buffer. These are then translated into file
64 * system independent "dirent" structs and returned in the
65 * caller's buffer. Returns the number of entries converted
66 * (-1 if there's an error).
67 *
68 * Although the xdr functions can allocate memory, we have
69 * a limited heap so we allocate our own space,
70 * assuming the worst case of 256 byte names.
71 * This is a space hog in our local buffer, so we want
72 * the number of buffers to be small. To make sure we don't
73 * get more names than we can handle, we tell the rpc
74 * routine that we only have space for MAXDENT names if
75 * they are all the minimum size. This keeps the return
76 * packet unfragmented, but may result in lots of reads
77 * to process a large directory. Since this is standalone
78 * we don't worry about speed. With MAXDENTs at 16, the
79 * local buffer is 4k.
80 */
81
82 int
nfs3getdents(struct nfs_file * nfp,struct dirent * dep,unsigned size)83 nfs3getdents(struct nfs_file *nfp, struct dirent *dep, unsigned size)
84 {
85 int cnt = 0;
86 entry3 *ep;
87 READDIR3args rda;
88 READDIR3res res;
89 enum clnt_stat status;
90 struct {
91 entry3 etlist[MAXDENTS];
92 char names[MAXDENTS][NFS_MAXNAMLEN+1];
93 } rdbuf;
94 int j;
95 struct timeval zero_timeout = {0, 0}; /* default */
96
97 bzero((caddr_t)&res, sizeof (res));
98 bzero((caddr_t)&rda, sizeof (rda));
99 bzero((caddr_t)rdbuf.etlist, sizeof (rdbuf.etlist));
100
101 rda.dir.data.data_len = nfp->fh.fh3.len;
102 rda.dir.data.data_val = nfp->fh.fh3.data;
103 rda.cookie = nfp->cookie.cookie3;
104
105 while (!res.READDIR3res_u.resok.reply.eof) {
106 /*
107 * Keep issuing nfs calls until EOF is reached on
108 * the directory or the user buffer is filled.
109 */
110
111 for (j = 0; j < MAXDENTS; j++) {
112 /*
113 * Link our buffers together for the benefit of
114 * XDR. We do this each time we issue the rpc call
115 * JIC the xdr decode
116 * routines screw up the linkage!
117 */
118
119 rdbuf.etlist[j].name = rdbuf.names[(MAXDENTS-1) - j];
120 rdbuf.etlist[j].nextentry =
121 (j < (MAXDENTS-1)) ? &rdbuf.etlist[j+1] : 0;
122 }
123
124 res.READDIR3res_u.resok.reply.entries = rdbuf.etlist;
125 /*
126 * Cannot give the whole buffer unless every name is
127 * 256 bytes! Assume the worst case of all 1 byte names.
128 * This results in MINSIZ bytes/name in the xdr stream.
129 */
130 rda.count = sizeof (res) + MAXDENTS*MINSIZ;
131 bzero((caddr_t)rdbuf.names, sizeof (rdbuf.names));
132
133 status = CLNT_CALL(root_CLIENT, NFSPROC3_READDIR,
134 xdr_READDIR3args, (caddr_t)&rda,
135 xdr_READDIR3res, (caddr_t)&res, zero_timeout);
136
137 if (status != RPC_SUCCESS) {
138 dprintf("nfs3_getdents: RPC error\n");
139 return (-1);
140 }
141 if (res.status != NFS3_OK) {
142 /*
143 * The most common failure here would be trying to
144 * issue a getdents call on a non-directory!
145 */
146
147 nfs3_error(res.status);
148 return (-1);
149 }
150
151 for (ep = rdbuf.etlist; ep; ep = ep->nextentry) {
152 /*
153 * Step thru all entries returned by NFS, converting
154 * to the cannonical form and copying out to the
155 * user's buffer.
156 */
157
158 int n;
159
160 /*
161 * catch the case user called at EOF
162 */
163 if ((n = strlen(ep->name)) == 0)
164 return (cnt);
165
166 n = BDIRENT_RECLEN(n);
167
168 if (n > size)
169 return (cnt);
170 size -= n;
171
172 (void) strlcpy(dep->d_name, ep->name,
173 strlen(ep->name) + 1);
174 dep->d_ino = ep->fileid;
175 dep->d_off = (off_t)ep->cookie;
176 dep->d_reclen = (ushort_t)n;
177
178 dep = (struct dirent *)((char *)dep + n);
179 rda.cookie = ep->cookie;
180 nfp->cookie.cookie3 = ep->cookie;
181 cnt++;
182 }
183 }
184
185 return (cnt);
186 }
187