hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <memory.h>
#include <boilerplate/ancillaries.h>
#include <boilerplate/lock.h>
#include <copperplate/cluster.h>
#include <psos/psos.h>
#include "internal.h"
#include "pt.h"
 
#define pt_magic    0x8181fefe
 
#define pt_align_mask   (sizeof(void *)-1)
 
#define pt_bitmap_pos(pt,n) \
pt->bitmap[((n) / (sizeof(u_long) * 8))]
 
#define pt_block_pos(n) \
(1L << ((n) % (sizeof(u_long) * 8)))
 
#define pt_bitmap_setbit(pt,n) \
(pt_bitmap_pos(pt,n) |= pt_block_pos(n))
 
#define pt_bitmap_clrbit(pt,n) \
(pt_bitmap_pos(pt,n) &= ~pt_block_pos(n))
 
#define pt_bitmap_tstbit(pt,n) \
(pt_bitmap_pos(pt,n) & pt_block_pos(n))
 
struct pvcluster psos_pt_table;
 
static unsigned long anon_ptids;
 
/*
 * XXX: Status wrt caller cancellation: all these routines are not
 * supposed to traverse any cancellation point (*), so we don't bother
 * adding any cleanup handler to release the partition lock, given
 * that copperplate-protected sections disables asynchronous thread
 * cancellation temporarily and therefore will allow callees to grab
 * mutexes safely.
 *
 * (*) all private cluster management routines are expected to be free
 * from cancellation points. You have been warned.
 */
 
static struct psos_pt *get_pt_from_id(u_long ptid, int *err_r)
{
   struct psos_pt *pt = (struct psos_pt *)ptid;
 
   /*
    * Unlike most other pSOS objects (except timers), the
    * partition control block is NOT laid into the main heap, so
    * don't have to apply mainheap_deref() to convert partition
    * handles to pointers, but we do a plain cast instead.  (This
    * said, mainheap_deref() is smart enough to deal with private
    * pointers, but we just avoid useless overhead).
    */
   if (pt == NULL || ((uintptr_t)pt & (sizeof(uintptr_t)-1)) != 0)
       goto objid_error;
 
   if (pt->magic == pt_magic) {
       if (__RT(pthread_mutex_lock(&pt->lock)) == 0) {
           if (pt->magic == pt_magic)
               return pt;
           __RT(pthread_mutex_unlock(&pt->lock));
           /* Will likely fall down to ERR_OBJDEL. */
       }
   }
 
   if (pt->magic == ~pt_magic) {
       *err_r = ERR_OBJDEL;
       return NULL;
   }
 
   if ((pt->magic >> 16) == 0x8181) {
       *err_r = ERR_OBJTYPE;
       return NULL;
   }
 
objid_error:
   *err_r = ERR_OBJID;
 
   return NULL;
}
 
static inline void put_pt(struct psos_pt *pt)
{
   __RT(pthread_mutex_unlock(&pt->lock));
}
 
static inline size_t pt_overhead(size_t psize, size_t bsize)
{
   size_t m = (bsize * 8);
   size_t q = ((psize - sizeof(struct psos_pt)) * m) / (m + 1);
   return (psize - q + pt_align_mask) & ~pt_align_mask;
}
 
u_long pt_create(const char *name,
        void *paddr, void *laddr  __attribute__ ((unused)),
        u_long psize, u_long bsize, u_long flags,
        u_long *ptid_r, u_long *nbuf)
{
   pthread_mutexattr_t mattr;
   char short_name[5];
   struct service svc;
   struct psos_pt *pt;
   int ret = SUCCESS;
   u_long overhead;
   caddr_t mp;
   u_long n;
 
   if ((uintptr_t)paddr & (sizeof(uintptr_t) - 1))
       return ERR_PTADDR;
 
   if (bsize <= pt_align_mask)
       return ERR_BUFSIZE;
 
   if (bsize & (bsize - 1))
       return ERR_BUFSIZE;    /* Not a power of two. */
 
   if (psize < sizeof(*pt))
       return ERR_TINYPT;
 
   pt = paddr;
 
   if (name == NULL || *name == '\0')
       sprintf(pt->name, "pt%lu", ++anon_ptids);
   else {
       name = psos_trunc_name(short_name, name);
       namecpy(pt->name, name);
   }
 
   CANCEL_DEFER(svc);
 
   if (pvcluster_addobj_dup(&psos_pt_table, pt->name, &pt->cobj)) {
       warning("cannot register partition: %s", pt->name);
       ret = ERR_OBJID;
       goto out;
   }
 
   pt->flags = flags;
   pt->bsize = (bsize + pt_align_mask) & ~pt_align_mask;
   overhead = pt_overhead(psize, pt->bsize);
 
   pt->nblks = (psize - overhead) / pt->bsize;
   if (pt->nblks == 0) {
       ret = ERR_TINYPT;
       goto out;
   }
 
   pt->psize = pt->nblks * pt->bsize;
   pt->data = (caddr_t)pt + overhead;
   pt->freelist = mp = pt->data;
   pt->ublks = 0;
 
   for (n = pt->nblks; n > 1; n--) {
       caddr_t nmp = mp + pt->bsize;
       *((void **)mp) = nmp;
       mp = nmp;
   }
 
   *((void **)mp) = NULL;
   memset(pt->bitmap, 0, overhead - sizeof(*pt) + sizeof(pt->bitmap));
   *nbuf = pt->nblks;
 
   pthread_mutexattr_init(&mattr);
   pthread_mutexattr_settype(&mattr, mutex_type_attribute);
   pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
   pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_PRIVATE);
   __RT(pthread_mutex_init(&pt->lock, &mattr));
   pthread_mutexattr_destroy(&mattr);
 
   pt->magic = pt_magic;
   *ptid_r = (u_long)pt;
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long pt_delete(u_long ptid)
{
   struct psos_pt *pt;
   struct service svc;
   int ret;
 
   pt = get_pt_from_id(ptid, &ret);
   if (pt == NULL)
       return ret;
 
   if ((pt->flags & PT_DEL) == 0 && pt->ublks > 0) {
       put_pt(pt);
       return ERR_BUFINUSE;
   }
 
   CANCEL_DEFER(svc);
   pvcluster_delobj(&psos_pt_table, &pt->cobj);
   CANCEL_RESTORE(svc);
   pt->magic = ~pt_magic; /* Prevent further reference. */
   put_pt(pt);
   __RT(pthread_mutex_destroy(&pt->lock));
 
   return SUCCESS;
}
 
u_long pt_getbuf(u_long ptid, void **bufaddr)
{
   struct psos_pt *pt;
   u_long numblk;
   void *buf;
   int ret;
 
   pt = get_pt_from_id(ptid, &ret);
   if (pt == NULL)
       return ret;
 
   buf = pt->freelist;
   if (buf) {
       pt->freelist = *((void **)buf);
       pt->ublks++;
       numblk = ((caddr_t)buf - pt->data) / pt->bsize;
       pt_bitmap_setbit(pt, numblk);
   }
 
   put_pt(pt);
 
   *bufaddr = buf;
   if (buf == NULL)
       return ERR_NOBUF;
 
   return SUCCESS;
}
 
u_long pt_retbuf(u_long ptid, void *buf)
{
   struct psos_pt *pt;
   u_long numblk;
   int ret;
 
   pt = get_pt_from_id(ptid, &ret);
   if (pt == NULL)
       return ret;
 
   if ((caddr_t)buf < pt->data ||
       (caddr_t)buf >= pt->data + pt->psize ||
       (((caddr_t)buf - pt->data) % pt->bsize) != 0) {
       ret = ERR_BUFADDR;
       goto done;
   }
 
   numblk = ((caddr_t)buf - pt->data) / pt->bsize;
 
   if (!pt_bitmap_tstbit(pt, numblk)) {
       ret = ERR_BUFFREE;
       goto done;
   }
 
   pt_bitmap_clrbit(pt, numblk);
   *((void **)buf) = pt->freelist;
   pt->freelist = buf;
   pt->ublks--;
   ret = SUCCESS;
done:
   put_pt(pt);
 
   return ret;
}
 
u_long pt_ident(const char *name, u_long node, u_long *ptid_r)
{
   struct pvclusterobj *cobj;
   struct service svc;
   struct psos_pt *pt;
   char short_name[5];
 
   if (node)
       return ERR_NODENO;
 
   name = psos_trunc_name(short_name, name);
 
   CANCEL_DEFER(svc);
   cobj = pvcluster_findobj(&psos_pt_table, name);
   CANCEL_RESTORE(svc);
   if (cobj == NULL)
       return ERR_OBJNF;
 
   pt = container_of(cobj, struct psos_pt, cobj);
   *ptid_r = (u_long)pt;
 
   return SUCCESS;
}