17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate *
267c478bd9Sstevel@tonic-gate * Stuff relating to directory reading ...
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <rpc/types.h>
307c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
317c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
327c478bd9Sstevel@tonic-gate #include "clnt.h"
337c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h>
347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
357c478bd9Sstevel@tonic-gate #include "nfs_inet.h"
367c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
377c478bd9Sstevel@tonic-gate #include "brpc.h"
387c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/bootvfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include "socket_inet.h"
447c478bd9Sstevel@tonic-gate #include <sys/salib.h>
457c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define MAXDENTS 16
487c478bd9Sstevel@tonic-gate #define MINSIZ 20
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Boot needs to be cleaned up to use either dirent32 or dirent64,
527c478bd9Sstevel@tonic-gate * in the meantime use dirent_t and always round to 8 bytes
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate #define BDIRENT_RECLEN(namelen) \
557c478bd9Sstevel@tonic-gate ((offsetof(dirent_t, d_name[0]) + 1 + (namelen) + 7) & ~ 7)
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #define dprintf if (boothowto & RB_DEBUG) printf
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Get directory entries:
617c478bd9Sstevel@tonic-gate *
627c478bd9Sstevel@tonic-gate * Uses the nfs "READDIR" operation to read directory entries
637c478bd9Sstevel@tonic-gate * into a local buffer. These are then translated into file
647c478bd9Sstevel@tonic-gate * system independent "dirent" structs and returned in the
657c478bd9Sstevel@tonic-gate * caller's buffer. Returns the number of entries converted
667c478bd9Sstevel@tonic-gate * (-1 if there's an error).
677c478bd9Sstevel@tonic-gate *
687c478bd9Sstevel@tonic-gate * Although the xdr functions can allocate memory, we have
697c478bd9Sstevel@tonic-gate * a limited heap so we allocate our own space,
707c478bd9Sstevel@tonic-gate * assuming the worst case of 256 byte names.
717c478bd9Sstevel@tonic-gate * This is a space hog in our local buffer, so we want
727c478bd9Sstevel@tonic-gate * the number of buffers to be small. To make sure we don't
737c478bd9Sstevel@tonic-gate * get more names than we can handle, we tell the rpc
747c478bd9Sstevel@tonic-gate * routine that we only have space for MAXDENT names if
757c478bd9Sstevel@tonic-gate * they are all the minimum size. This keeps the return
767c478bd9Sstevel@tonic-gate * packet unfragmented, but may result in lots of reads
777c478bd9Sstevel@tonic-gate * to process a large directory. Since this is standalone
787c478bd9Sstevel@tonic-gate * we don't worry about speed. With MAXDENTs at 16, the
797c478bd9Sstevel@tonic-gate * local buffer is 4k.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate int
nfsgetdents(struct nfs_file * nfp,struct dirent * dep,unsigned size)837c478bd9Sstevel@tonic-gate nfsgetdents(struct nfs_file *nfp, struct dirent *dep, unsigned size)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate entry *ep;
867c478bd9Sstevel@tonic-gate readdirargs rda;
877c478bd9Sstevel@tonic-gate readdirres res;
887c478bd9Sstevel@tonic-gate enum clnt_stat status;
897c478bd9Sstevel@tonic-gate struct {
907c478bd9Sstevel@tonic-gate entry etlist[MAXDENTS];
917c478bd9Sstevel@tonic-gate char names[MAXDENTS][NFS_MAXNAMLEN+1];
927c478bd9Sstevel@tonic-gate } rdbuf;
937c478bd9Sstevel@tonic-gate uint32_t offset;
947c478bd9Sstevel@tonic-gate int j, cnt = 0;
957c478bd9Sstevel@tonic-gate struct timeval zero_timeout = {0, 0}; /* default */
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate bzero((caddr_t)&res, sizeof (res));
987c478bd9Sstevel@tonic-gate bzero((caddr_t)&rda, sizeof (rda));
997c478bd9Sstevel@tonic-gate bzero((caddr_t)rdbuf.etlist, sizeof (rdbuf.etlist));
1007c478bd9Sstevel@tonic-gate bcopy((caddr_t)&nfp->fh.fh2, (caddr_t)&rda.dir, NFS_FHSIZE);
1017c478bd9Sstevel@tonic-gate bcopy((caddr_t)nfp->cookie.cookie2, (caddr_t)rda.cookie,
1027c478bd9Sstevel@tonic-gate sizeof (nfscookie));
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate while (!res.readdirres_u.reply.eof) {
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * Keep issuing nfs calls until EOF is reached on
1077c478bd9Sstevel@tonic-gate * the directory or the user buffer is filled.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate for (j = 0; j < MAXDENTS; j++) {
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Link our buffers together for the benefit of
1137c478bd9Sstevel@tonic-gate * XDR. We do this each time we issue the rpc call
1147c478bd9Sstevel@tonic-gate * JIC the xdr decode
1157c478bd9Sstevel@tonic-gate * routines screw up the linkage!
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate rdbuf.etlist[j].name = rdbuf.names[(MAXDENTS-1) - j];
1197c478bd9Sstevel@tonic-gate rdbuf.etlist[j].nextentry =
1207c478bd9Sstevel@tonic-gate (j < (MAXDENTS-1)) ? &rdbuf.etlist[j+1] : 0;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate res.readdirres_u.reply.entries = rdbuf.etlist;
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * Cannot give the whole buffer unless every name is
1267c478bd9Sstevel@tonic-gate * 256 bytes! Assume the worst case of all 1 byte names.
1277c478bd9Sstevel@tonic-gate * This results in MINSIZ bytes/name in the xdr stream.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate rda.count = sizeof (res) + MAXDENTS*MINSIZ;
1307c478bd9Sstevel@tonic-gate bzero((caddr_t)rdbuf.names, sizeof (rdbuf.names));
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate status = CLNT_CALL(root_CLIENT, NFSPROC_READDIR,
1337c478bd9Sstevel@tonic-gate xdr_readdirargs, (caddr_t)&rda,
1347c478bd9Sstevel@tonic-gate xdr_readdirres, (caddr_t)&res, zero_timeout);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate if (status != RPC_SUCCESS) {
1377c478bd9Sstevel@tonic-gate dprintf("nfs_getdents: RPC error\n");
1387c478bd9Sstevel@tonic-gate return (-1);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate if (res.status != NFS_OK) {
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * The most common failure here would be trying to
1437c478bd9Sstevel@tonic-gate * issue a getdents call on a non-directory!
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate nfs_error(res.status);
1477c478bd9Sstevel@tonic-gate return (-1);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate for (ep = rdbuf.etlist; ep; ep = ep->nextentry) {
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Step thru all entries returned by NFS, converting
1537c478bd9Sstevel@tonic-gate * to the cannonical form and copying out to the
1547c478bd9Sstevel@tonic-gate * user's buffer.
1557c478bd9Sstevel@tonic-gate */
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate int n;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * catch the case user called at EOF
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate if ((n = strlen(ep->name)) == 0)
1637c478bd9Sstevel@tonic-gate return (cnt);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate n = BDIRENT_RECLEN(n);
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (n > size)
1687c478bd9Sstevel@tonic-gate return (cnt);
1697c478bd9Sstevel@tonic-gate size -= n;
1707c478bd9Sstevel@tonic-gate
171*b531f6d1SToomas Soome (void) strlcpy(dep->d_name, ep->name,
172*b531f6d1SToomas Soome strlen(ep->name) + 1);
1737c478bd9Sstevel@tonic-gate dep->d_ino = ep->fileid;
1747c478bd9Sstevel@tonic-gate bcopy(ep->cookie, &offset, sizeof (nfscookie));
1757c478bd9Sstevel@tonic-gate dep->d_off = offset;
1767c478bd9Sstevel@tonic-gate dep->d_reclen = (ushort_t)n;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate dep = (struct dirent *)((char *)dep + n);
1797c478bd9Sstevel@tonic-gate bcopy(ep->cookie, rda.cookie, sizeof (nfscookie));
1807c478bd9Sstevel@tonic-gate bcopy(ep->cookie, nfp->cookie.cookie2,
1817c478bd9Sstevel@tonic-gate sizeof (nfscookie));
1827c478bd9Sstevel@tonic-gate cnt++;
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate return (cnt);
1877c478bd9Sstevel@tonic-gate }
188