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