summaryrefslogtreecommitdiffstats
path: root/src/clnt_fd_locks.h
blob: 6ba62cb14b97e0301fcb68059e93b2b8da25f1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * debug.h -- debugging routines for libtirpc
 *
 * Copyright (c) 2020 SUSE LINUX GmbH, Nuernberg, Germany.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of Sun Microsystems, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CLNT_FD_LOCKS_H
#define _CLNT_FD_LOCKS_H

#include <sys/queue.h>
#include <errno.h>
#include <reentrant.h>
#include <rpc/xdr.h>


/*
 * This utility manages a list of per-fd locks for the clients.
 *
 * If MAX_FDLOCKS_PREALLOC is defined, a number of pre-fd locks will be
 * pre-allocated. This number is the minimum of MAX_FDLOCKS_PREALLOC or
 * the process soft limit of allowed fds.
 */
#ifdef MAX_FDLOCKS_PREALLOC
static unsigned int fd_locks_prealloc = 0;
#endif

/* per-fd lock */
struct fd_lock_t {
	bool_t active;
	int pending;        /* Number of pending operations on fd */
	cond_t cv;
};
typedef struct fd_lock_t fd_lock_t;


/* internal type to store per-fd locks in a list */
struct fd_lock_item_t {
	/* fd_lock_t first so we can cast to fd_lock_item_t */
	fd_lock_t fd_lock;
	int fd;
	unsigned int refs;
	TAILQ_ENTRY(fd_lock_item_t) link;
};
typedef struct fd_lock_item_t fd_lock_item_t;
#define to_fd_lock_item(fdlock_t_ptr) ((fd_lock_item_t*) fdlock_t_ptr)


/* internal list of per-fd locks */
typedef TAILQ_HEAD(,fd_lock_item_t) fd_lock_list_t;


#ifdef MAX_FDLOCKS_PREALLOC

/* With pre-allocation, keep track of both an array and a list */
struct fd_locks_t {
	fd_lock_list_t fd_lock_list;
	fd_lock_t *fd_lock_array;
};
typedef struct fd_locks_t fd_locks_t;
#define to_fd_lock_list(fd_locks_t_ptr) (&fd_locks_t_ptr->fd_lock_list)

#else

/* With no pre-allocation, just keep track of a list */
typedef fd_lock_list_t fd_locks_t;
#define to_fd_lock_list(fd_locks_t_ptr) ((fd_lock_list_t *) fd_locks_t_ptr)

#endif


/* allocate fd locks */
static inline
fd_locks_t* fd_locks_init() {
	fd_locks_t *fd_locks;

	fd_locks = (fd_locks_t *) mem_alloc(sizeof(fd_locks_t));
	if (fd_locks == (fd_locks_t *) NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	TAILQ_INIT(to_fd_lock_list(fd_locks));

#ifdef MAX_FDLOCKS_PREALLOC
	size_t fd_lock_arraysz;

	if (fd_locks_prealloc == 0) {
		unsigned int dtbsize = __rpc_dtbsize();
		if (0 < dtbsize && dtbsize < MAX_FDLOCKS_PREALLOC)
			fd_locks_prealloc = dtbsize;
		else
			fd_locks_prealloc = MAX_FDLOCKS_PREALLOC;
	}

	if ( (size_t) fd_locks_prealloc > SIZE_MAX/sizeof(fd_lock_t)) {
		mem_free(fd_locks, sizeof (*fd_locks));
		errno = EOVERFLOW;
		return (NULL);
	}

	fd_lock_arraysz = fd_locks_prealloc * sizeof (fd_lock_t);
	fd_locks->fd_lock_array = (fd_lock_t *) mem_alloc(fd_lock_arraysz);
	if (fd_locks->fd_lock_array == (fd_lock_t *) NULL) {
		mem_free(fd_locks, sizeof (*fd_locks));
		errno = ENOMEM;
		return (NULL);
	}
	else {
		int i;

		for (i = 0; i < fd_locks_prealloc; i++) {
			fd_locks->fd_lock_array[i].active = FALSE;
			cond_init(&fd_locks->fd_lock_array[i].cv, 0, (void *) 0);
		}
	}
#endif

	return fd_locks;
}

/* de-allocate fd locks */
static inline
void fd_locks_destroy(fd_locks_t *fd_locks) {
#ifdef MAX_FDLOCKS_PREALLOC
	fd_lock_t *array = fd_locks->fd_lock_array;
	mem_free(array, fd_locks_prealloc * sizeof (fd_lock_t));
#endif
	fd_lock_item_t *item;
	fd_lock_list_t *list = to_fd_lock_list(fd_locks);

	TAILQ_FOREACH(item, list, link) {
		cond_destroy(&item->fd_lock.cv);
		mem_free(item, sizeof (*item));
	}
	mem_free(fd_locks, sizeof (*fd_locks));
}

/* allocate per-fd lock */
static inline
fd_lock_t* fd_lock_create(int fd, fd_locks_t *fd_locks) {
#ifdef MAX_FDLOCKS_PREALLOC
	if (fd < fd_locks_prealloc) {
		return &fd_locks->fd_lock_array[fd];
	}
#endif
	fd_lock_item_t *item;
	fd_lock_list_t *list = to_fd_lock_list(fd_locks);

	for (item = TAILQ_FIRST(list);
	     item != (fd_lock_item_t *) NULL && item->fd != fd;
	     item = TAILQ_NEXT(item, link));

	if (item == (fd_lock_item_t *) NULL) {
		item = (fd_lock_item_t *) mem_alloc(sizeof(fd_lock_item_t));
		if (item == (fd_lock_item_t *) NULL) {
			errno = ENOMEM;
			return (NULL);
		}
		item->fd = fd;
		item->refs = 1;
		item->fd_lock.active = FALSE;
		item->fd_lock.pending = 0;
		cond_init(&item->fd_lock.cv, 0, (void *) 0);
		TAILQ_INSERT_HEAD(list, item, link);
	} else {
		item->refs++;
	}
	return &item->fd_lock;
}

/* de-allocate per-fd lock */
static inline
void fd_lock_destroy(int fd, fd_lock_t *fd_lock, fd_locks_t *fd_locks) {
#ifdef MAX_FDLOCKS_PREALLOC
	if (fd < fd_locks_prealloc)
		return;
#endif
	fd_lock_item_t* item = to_fd_lock_item(fd_lock);
	item->refs--;
	if (item->refs <= 0) {
		TAILQ_REMOVE(to_fd_lock_list(fd_locks), item, link);
		cond_destroy(&item->fd_lock.cv);
		mem_free(item, sizeof (*item));
	}
}

#endif /* _CLNT_FD_LOCKS_H */