xref: /illumos-gate/usr/src/stand/lib/fs/nfs/getdents4.c (revision 7c478bd9)
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 NFSv4 directory reading ...
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <rpc/types.h>
32 #include <rpc/auth.h>
33 #include <rpc/xdr.h>
34 #include "clnt.h"
35 #include <rpc/rpc_msg.h>
36 #include <sys/t_lock.h>
37 #include "nfs_inet.h"
38 #include <rpc/rpc.h>
39 #include "brpc.h"
40 #include <rpcsvc/nfs_prot.h>
41 #include <sys/types.h>
42 #include <sys/stat.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>
48 
49 #define	MAXDENTS 16
50 #define	MINSIZ 20
51 
52 /*
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
55  */
56 #define	BDIRENT_RECLEN(namelen) \
57 	((offsetof(dirent_t, d_name[0]) + 1 + (namelen) + 7) & ~ 7)
58 
59 #define	dprintf	if (boothowto & RB_DEBUG) printf
60 
61 /*
62  *  Get directory entries:
63  *
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).
69  *
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
81  *	local buffer is 4k.
82  */
83 
84 int
nfs4getdents(struct nfs_file * nfp,struct dirent * dep,unsigned size)85 nfs4getdents(struct nfs_file *nfp, struct dirent *dep, unsigned size)
86 {
87 	int		cnt = 0;
88 	b_entry4_t	*ep;
89 	readdir4arg_t	readdir_args;
90 	readdir4res_t	readdir_res;
91 	attr4_bitmap1_t	bitmap1;
92 	enum clnt_stat	status;
93 	struct {
94 		b_entry4_t etlist[MAXDENTS];
95 		char names[MAXDENTS][NFS_MAXNAMLEN+1];
96 	} rdbuf;
97 	int		j;
98 	struct timeval	zero_timeout = {0, 0};   /* default */
99 	utf8string	str;
100 	char		tagname[] = "inetboot readdir";
101 
102 	bzero((caddr_t)&readdir_res, sizeof (readdir4res_t));
103 	bzero((caddr_t)&readdir_args, sizeof (readdir4arg_t));
104 	bzero((caddr_t)rdbuf.etlist, sizeof (rdbuf.etlist));
105 
106 	str.utf8string_len = sizeof (tagname) - 1;
107 	str.utf8string_val = tagname;
108 
109 	if (nfp->fh.fh4.len > 0)
110 		compound_init(&readdir_args.rd_arg, &str, 0, 2, &nfp->fh.fh4);
111 	else
112 		compound_init(&readdir_args.rd_arg, &str, 0, 2, NULL);
113 
114 	readdir_args.rd_opreaddir = OP_READDIR;
115 	readdir_args.rd_cookie = nfp->cookie.cookie4;
116 
117 	while (!readdir_res.rd_eof) {
118 		/*
119 		 *  Keep issuing nfs calls until EOF is reached on
120 		 *  the directory or the user buffer is filled.
121 		 */
122 		for (j = 0; j < MAXDENTS; j++) {
123 			/*
124 			 *  Link our buffers together for the benefit of
125 			 *  XDR.  We do this each time we issue the rpc call
126 			 *  JIC the xdr decode
127 			 *  routines screw up the linkage!
128 			 */
129 			rdbuf.etlist[j].b_name.utf8string_len = NFS_MAXNAMLEN;
130 			rdbuf.etlist[j].b_name.utf8string_val =
131 				rdbuf.names[(MAXDENTS-1) - j];
132 			rdbuf.etlist[j].b_nextentry =
133 				(j < (MAXDENTS-1)) ? &rdbuf.etlist[j+1] : 0;
134 		}
135 
136 		readdir_res.rd_entries = rdbuf.etlist;
137 		/*
138 		 * Cannot give the whole buffer unless every name is
139 		 * 256 bytes! Assume the worst case of all 1 byte names.
140 		 * This results in MINSIZ bytes/name in the xdr stream.
141 		 */
142 		readdir_args.rd_dircount = MAXDENTS * MINSIZ;
143 		readdir_args.rd_maxcount = sizeof (readdir4res_t) +
144 							(MAXDENTS * MINSIZ);
145 		bzero((caddr_t)rdbuf.names, sizeof (rdbuf.names));
146 
147 		/*
148 		 * Set the attr bitmap, so we get the fileid back.
149 		 */
150 		bitmap1.word = 0;
151 		bitmap1.bm_fattr4_fileid = 1;
152 		readdir_args.rd_attr_req.b_bitmap_len = 1;
153 		readdir_args.rd_attr_req.b_bitmap_val[0] = bitmap1.word;
154 
155 		status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND,
156 			xdr_readdir4_args, (caddr_t)&readdir_args,
157 			xdr_readdir4_res, (caddr_t)&readdir_res, zero_timeout);
158 
159 		if (status != RPC_SUCCESS) {
160 			dprintf("nfs4_getdents: RPC error\n");
161 			return (-1);
162 		}
163 		if (readdir_res.rd_status != NFS4_OK) {
164 			/*
165 			 *  The most common failure here would be trying to
166 			 *  issue a getdents call on a non-directory!
167 			 */
168 
169 			nfs4_error(readdir_res.rd_status);
170 			return (-1);
171 		}
172 
173 		/*
174 		 * If we are reading from the beginning of the
175 		 * directory we will need to create the "." and ".."
176 		 * since we won't be getting them from the server.  To obtain
177 		 * the fileid's just issue a couple otw lookups to get the
178 		 * info we need.
179 		 */
180 		if (readdir_args.rd_cookie == 0 &&
181 		    rdbuf.etlist[0].b_cookie > 2) {
182 			int n;
183 			int error;
184 			uint64_t fileid;
185 			struct vattr va;
186 
187 			/*
188 			 * Do a getattr for the '.'
189 			 */
190 			error = nfs4getattr(nfp, &va);
191 			if (error)
192 				return (-1);
193 
194 			dep->d_name[0] = '.';
195 			dep->d_name[1] = '\0';
196 			dep->d_ino = va.va_nodeid;
197 			dep->d_off = 1;
198 			n = BDIRENT_RECLEN(1);
199 			dep->d_reclen = n;
200 			dep = (struct dirent *)((char *)dep + n);
201 
202 			/*
203 			 * Do a lookupp for the '..'
204 			 */
205 			(void) nfs4lookupp(nfp, &error, &fileid);
206 			if (error)
207 				return (-1);
208 
209 			dep->d_name[0] = '.';
210 			dep->d_name[1] = '.';
211 			dep->d_name[2] = '\0';
212 			dep->d_ino = fileid;
213 			dep->d_off = 2;
214 			n = BDIRENT_RECLEN(2);
215 			dep->d_reclen = n;
216 			dep = (struct dirent *)((char *)dep + n);
217 		}
218 
219 		for (ep = rdbuf.etlist; ep; ep = ep->b_nextentry) {
220 			/*
221 			 *  Step thru all entries returned by NFS, converting
222 			 *  to the cannonical form and copying out to the
223 			 *  user's buffer.
224 			 */
225 			int n;
226 			int namlen;
227 
228 			/*
229 			 * catch the case user called at EOF
230 			 */
231 			if ((namlen = ep->b_name.utf8string_len) == 0)
232 				return (cnt);
233 
234 			n = BDIRENT_RECLEN(namlen);
235 
236 			if (n > size)
237 				return (cnt);
238 			size -= n;
239 
240 			bcopy(ep->b_name.utf8string_val, dep->d_name, namlen);
241 			dep->d_name[namlen] = '\0';
242 			dep->d_ino = ep->b_fileid;
243 			dep->d_off = (off_t)ep->b_cookie;
244 			dep->d_reclen = (ushort_t)n;
245 
246 			dep = (struct dirent *)((char *)dep + n);
247 			readdir_args.rd_cookie = ep->b_cookie;
248 			nfp->cookie.cookie4 = ep->b_cookie;
249 			cnt++;
250 		}
251 	}
252 
253 	return (cnt);
254 }
255