6198 Let's EOL cachefs
[illumos-gate.git] / usr / src / lib / libnsl / rpc / fdsync.c
blob98e6752544e9f13eddff4766bdaeb334a0f71b60
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * This file contains functions that enables the rpc library to synchronize
32 * between various threads while they compete for a particular file descriptor.
35 #include "mt.h"
36 #include "rpc_mt.h"
37 #include <rpc/rpc.h>
38 #include <errno.h>
39 #include <sys/poll.h>
40 #include <syslog.h>
41 #include <sys/types.h>
42 #include <stdlib.h>
43 #include <unistd.h>
46 * A block holds an array of maxBlockSize cell and associated recursive locks
49 #define CELLTBLSZ 1024
51 typedef struct rpcfd_block {
52 struct rpcfd_block *next; /* Next Block */
53 struct rpcfd_block *prev; /* prev Block */
54 int end; /* fd of last lock in the list */
55 mutex_t lock[CELLTBLSZ]; /* recursive locks */
56 } rpcfd_block_t;
58 mutex_t rpc_fd_list_lock = DEFAULTMUTEX; /* protects list manipulation */
60 /* Following functions create and manipulates the dgfd lock object */
62 static mutex_t *search(const void *handle, int fd);
63 static rpcfd_block_t *create_block(const void *handle, int fd);
65 void *
66 rpc_fd_init(void)
69 * Create first chunk of CELLTBLSZ
70 * (No lock is required for initial creation.)
72 return (create_block(NULL, 0));
76 * If rpc_fd_lock() fails to acquire a lock, it returns non-zero (ENOMEM).
77 * (The operation can only fail due to a malloc() failure.)
78 * The caller of rpc_fd_lock() must call rpc_fd_unlock() even if
79 * rpc_fd_lock() failed. This keeps _sigoff() and _sigon() balanced.
81 * If search() and create_block() fail for rpc_fd_lock(), then search()
82 * will fail for rpc_fd_unlock(), so mutex_lock() and mutex_unlock()
83 * calls will be balanced. In any case, since the mutex is marked
84 * LOCK_ERRORCHECK, an additional mutex_unlock() does nothing.
86 int
87 rpc_fd_lock(const void *handle, int fd)
89 mutex_t *mp;
90 rpcfd_block_t *p;
92 _sigoff();
93 (void) mutex_lock(&rpc_fd_list_lock);
94 mp = search(handle, fd);
95 if (mp == NULL) {
96 p = create_block(handle, fd);
97 if (p != NULL)
98 mp = &p->lock[fd % CELLTBLSZ];
100 (void) mutex_unlock(&rpc_fd_list_lock);
101 if (mp == NULL)
102 return (ENOMEM);
103 (void) mutex_lock(mp);
104 return (0);
107 void
108 rpc_fd_unlock(const void *handle, int fd)
110 mutex_t *mp;
112 (void) mutex_lock(&rpc_fd_list_lock);
113 mp = search(handle, fd);
114 (void) mutex_unlock(&rpc_fd_list_lock);
115 if (mp != NULL)
116 (void) mutex_unlock(mp);
117 _sigon();
120 static rpcfd_block_t *
121 create_block(const void *handle, int fd)
123 rpcfd_block_t *l, *lprev;
124 rpcfd_block_t *p;
125 int i;
127 p = malloc(sizeof (rpcfd_block_t));
128 if (p == NULL)
129 return (NULL);
131 for (i = 0; i < CELLTBLSZ; i++) {
132 (void) mutex_init(&p->lock[i],
133 USYNC_THREAD | LOCK_RECURSIVE | LOCK_ERRORCHECK, NULL);
135 p->end = (((fd + CELLTBLSZ) / CELLTBLSZ) * CELLTBLSZ) - 1;
136 lprev = NULL;
137 for (l = (rpcfd_block_t *)handle; l; l = l->next) {
138 lprev = l;
139 if (fd < l->end)
140 break;
143 p->next = l;
144 p->prev = lprev;
145 if (lprev)
146 lprev->next = p;
147 if (l)
148 l->prev = p;
150 return (p);
154 * Called with rpc_fd_list_lock held.
156 static mutex_t *
157 search(const void *handle, int fd)
159 rpcfd_block_t *p;
161 for (p = (rpcfd_block_t *)handle; p; p = p->next) {
162 if (fd <= p->end)
163 return (&p->lock[fd % CELLTBLSZ]);
166 return (NULL);