xref: /illumos-gate/usr/src/lib/smbclnt/libfknsmb/common/fake_fio.c (revision 08855964b9970604433f7b19dcd71cf5af5e5f14)
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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent Inc.
25  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright 2024 RackTop Systems, Inc.
27  */
28 
29 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30 /*	All Rights Reserved */
31 
32 /*
33  * The kTLI "shim"  in fake_ktli.c uses file_getf(), file_releasef() to
34  * get a file_t pointer, and uses the (private) file_getfd() to access
35  * the socket file descriptor behind that file_t (which is actually a
36  * fake_file_t created here).  These correspond to getf/releasef that
37  * are normally found in os/fio.c but renamed to avoid accidental use.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/sysmacros.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/cred.h>
46 #include <sys/user.h>
47 #include <sys/vfs.h>
48 #include <sys/vnode.h>
49 #include <sys/file.h>
50 #include <sys/debug.h>
51 #include <sys/kmem.h>
52 
53 #include "fake_fio.h"
54 
55 typedef struct fake_file {
56 	struct file ff_file;
57 	int	ff_fd;
58 } fake_file_t;
59 
60 #define	FAKEFDS	256
61 
62 kmutex_t ftlock;
63 fake_file_t *ftab[FAKEFDS];
64 
65 file_t *
66 file_getf(int fd)
67 {
68 	fake_file_t *fp;
69 
70 	if (fd < 0 || fd >= FAKEFDS)
71 		return (NULL);
72 
73 	mutex_enter(&ftlock);
74 	if ((fp = ftab[fd]) != NULL) {
75 		fp->ff_file.f_count++;
76 		mutex_exit(&ftlock);
77 		return (&fp->ff_file);
78 	}
79 
80 	fp = kmem_zalloc(sizeof (*fp), KM_SLEEP);
81 	fp->ff_fd = fd;
82 	fp->ff_file.f_count = 1;
83 
84 	ftab[fd] = fp;
85 	mutex_exit(&ftlock);
86 
87 	return (&fp->ff_file);
88 }
89 
90 void
91 file_releasef(int fd)
92 {
93 	fake_file_t *fp;
94 
95 	mutex_enter(&ftlock);
96 	if ((fp = ftab[fd]) == NULL) {
97 		mutex_exit(&ftlock);
98 		return;
99 	}
100 	fp->ff_file.f_count--;
101 	if (fp->ff_file.f_count > 0) {
102 		mutex_exit(&ftlock);
103 		return;
104 	}
105 	ftab[fd] = NULL;
106 	mutex_exit(&ftlock);
107 
108 	kmem_free(fp, sizeof (*fp));
109 }
110 
111 int
112 file_getfd(file_t *fp)
113 {
114 	fake_file_t *ffp = (fake_file_t *)fp;
115 	int fd = ffp->ff_fd;
116 
117 	VERIFY(fd >= 0 && fd < FAKEFDS);
118 	ASSERT(ffp == ftab[fd]);
119 
120 	return (fd);
121 }
122