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