1*8329232eSGordon Ross /*
2*8329232eSGordon Ross  * CDDL HEADER START
3*8329232eSGordon Ross  *
4*8329232eSGordon Ross  * The contents of this file are subject to the terms of the
5*8329232eSGordon Ross  * Common Development and Distribution License (the "License").
6*8329232eSGordon Ross  * You may not use this file except in compliance with the License.
7*8329232eSGordon Ross  *
8*8329232eSGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8329232eSGordon Ross  * or http://www.opensolaris.org/os/licensing.
10*8329232eSGordon Ross  * See the License for the specific language governing permissions
11*8329232eSGordon Ross  * and limitations under the License.
12*8329232eSGordon Ross  *
13*8329232eSGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14*8329232eSGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8329232eSGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16*8329232eSGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17*8329232eSGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8329232eSGordon Ross  *
19*8329232eSGordon Ross  * CDDL HEADER END
20*8329232eSGordon Ross  */
21*8329232eSGordon Ross 
22*8329232eSGordon Ross /*
23*8329232eSGordon Ross  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24*8329232eSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25*8329232eSGordon Ross  */
26*8329232eSGordon Ross 
27*8329232eSGordon Ross /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*8329232eSGordon Ross /*	  All Rights Reserved	*/
29*8329232eSGordon Ross 
30*8329232eSGordon Ross /*
31*8329232eSGordon Ross  * Portions of this source code were derived from Berkeley 4.3 BSD
32*8329232eSGordon Ross  * under license from the Regents of the University of California.
33*8329232eSGordon Ross  */
34*8329232eSGordon Ross 
35*8329232eSGordon Ross #include <sys/param.h>
36*8329232eSGordon Ross #include <sys/isa_defs.h>
37*8329232eSGordon Ross #include <sys/types.h>
38*8329232eSGordon Ross #include <sys/inttypes.h>
39*8329232eSGordon Ross #include <sys/sysmacros.h>
40*8329232eSGordon Ross #include <sys/cred.h>
41*8329232eSGordon Ross #include <sys/dirent.h>
42*8329232eSGordon Ross #include <sys/systm.h>
43*8329232eSGordon Ross #include <sys/errno.h>
44*8329232eSGordon Ross #include <sys/vnode.h>
45*8329232eSGordon Ross #include <sys/file.h>
46*8329232eSGordon Ross #include <sys/mode.h>
47*8329232eSGordon Ross #include <sys/uio.h>
48*8329232eSGordon Ross #include <sys/filio.h>
49*8329232eSGordon Ross #include <sys/debug.h>
50*8329232eSGordon Ross #include <sys/kmem.h>
51*8329232eSGordon Ross #include <sys/cmn_err.h>
52*8329232eSGordon Ross 
53*8329232eSGordon Ross /*
54*8329232eSGordon Ross  * Returns count of dir entries, or -errno
55*8329232eSGordon Ross  */
56*8329232eSGordon Ross int
fake_getdents(vnode_t * vp,offset_t * offp,void * buf,size_t count)57*8329232eSGordon Ross fake_getdents(vnode_t *vp, offset_t *offp, void *buf, size_t count)
58*8329232eSGordon Ross {
59*8329232eSGordon Ross 	struct uio auio;
60*8329232eSGordon Ross 	struct iovec aiov;
61*8329232eSGordon Ross 	register int error;
62*8329232eSGordon Ross 	int sink;
63*8329232eSGordon Ross 
64*8329232eSGordon Ross 	if (count < sizeof (struct dirent64))
65*8329232eSGordon Ross 		return (-EINVAL);
66*8329232eSGordon Ross 
67*8329232eSGordon Ross 	/*
68*8329232eSGordon Ross 	 * Don't let the user overcommit kernel resources.
69*8329232eSGordon Ross 	 */
70*8329232eSGordon Ross 	if (count > MAXGETDENTS_SIZE)
71*8329232eSGordon Ross 		count = MAXGETDENTS_SIZE;
72*8329232eSGordon Ross 
73*8329232eSGordon Ross 	if (vp->v_type != VDIR) {
74*8329232eSGordon Ross 		return (-ENOTDIR);
75*8329232eSGordon Ross 	}
76*8329232eSGordon Ross 
77*8329232eSGordon Ross 	aiov.iov_base = buf;
78*8329232eSGordon Ross 	aiov.iov_len = count;
79*8329232eSGordon Ross 	auio.uio_iov = &aiov;
80*8329232eSGordon Ross 	auio.uio_iovcnt = 1;
81*8329232eSGordon Ross 	auio.uio_loffset = *offp;
82*8329232eSGordon Ross 	auio.uio_segflg = UIO_USERSPACE;
83*8329232eSGordon Ross 	auio.uio_resid = count;
84*8329232eSGordon Ross 	auio.uio_fmode = 0;
85*8329232eSGordon Ross 	auio.uio_extflg = UIO_COPY_CACHED;
86*8329232eSGordon Ross 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
87*8329232eSGordon Ross 	error = VOP_READDIR(vp, &auio, CRED(), &sink, NULL, 0);
88*8329232eSGordon Ross 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
89*8329232eSGordon Ross 	if (error) {
90*8329232eSGordon Ross 		return (-error);
91*8329232eSGordon Ross 	}
92*8329232eSGordon Ross 	count = count - auio.uio_resid;
93*8329232eSGordon Ross 	*offp = auio.uio_loffset;
94*8329232eSGordon Ross 
95*8329232eSGordon Ross 	return (count);
96*8329232eSGordon Ross }