Update GIT documentation to describe the new DAC setup
[zfs.git] / patches / pthreads.patch
1 Use POSIX threads in userspace.
2
3 Index: zfs+chaos4/cmd/lztest/ztest.c
4 ===================================================================
5 --- zfs+chaos4.orig/cmd/lztest/ztest.c
6 +++ zfs+chaos4/cmd/lztest/ztest.c
7 @@ -141,7 +141,7 @@ typedef struct ztest_args {
8         spa_t           *za_spa;
9         objset_t        *za_os;
10         zilog_t         *za_zilog;
11 -       thread_t        za_thread;
12 +       pthread_t       za_thread;
13         uint64_t        za_instance;
14         uint64_t        za_random;
15         uint64_t        za_diroff;
16 @@ -224,17 +224,17 @@ ztest_info_t ztest_info[] = {
17   * Stuff we need to share writably between parent and child.
18   */
19  typedef struct ztest_shared {
20 -       mutex_t         zs_vdev_lock;
21 -       rwlock_t        zs_name_lock;
22 -       uint64_t        zs_vdev_primaries;
23 -       uint64_t        zs_enospc_count;
24 -       hrtime_t        zs_start_time;
25 -       hrtime_t        zs_stop_time;
26 -       uint64_t        zs_alloc;
27 -       uint64_t        zs_space;
28 -       ztest_info_t    zs_info[ZTEST_FUNCS];
29 -       mutex_t         zs_sync_lock[ZTEST_SYNC_LOCKS];
30 -       uint64_t        zs_seq[ZTEST_SYNC_LOCKS];
31 +       pthread_mutex_t         zs_vdev_lock;
32 +       pthread_rwlock_t        zs_name_lock;
33 +       uint64_t                zs_vdev_primaries;
34 +       uint64_t                zs_enospc_count;
35 +       hrtime_t                zs_start_time;
36 +       hrtime_t                zs_stop_time;
37 +       uint64_t                zs_alloc;
38 +       uint64_t                zs_space;
39 +       ztest_info_t            zs_info[ZTEST_FUNCS];
40 +       pthread_mutex_t         zs_sync_lock[ZTEST_SYNC_LOCKS];
41 +       uint64_t                zs_seq[ZTEST_SYNC_LOCKS];
42  } ztest_shared_t;
43  
44  static char ztest_dev_template[] = "%s/%s.%llua";
45 @@ -818,7 +818,7 @@ ztest_spa_create_destroy(ztest_args_t *z
46          * Attempt to create an existing pool.  It shouldn't matter
47          * what's in the nvroot; we should fail with EEXIST.
48          */
49 -       (void) rw_rdlock(&ztest_shared->zs_name_lock);
50 +       (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
51         nvroot = make_vdev_root(0, 0, 0, 0, 1);
52         error = spa_create(za->za_pool, nvroot, NULL, NULL);
53         nvlist_free(nvroot);
54 @@ -834,7 +834,7 @@ ztest_spa_create_destroy(ztest_args_t *z
55                 fatal(0, "spa_destroy() = %d", error);
56  
57         spa_close(spa, FTAG);
58 -       (void) rw_unlock(&ztest_shared->zs_name_lock);
59 +       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
60  }
61  
62  /*
63 @@ -851,7 +851,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
64         if (zopt_verbose >= 6)
65                 (void) printf("adding vdev\n");
66  
67 -       (void) mutex_lock(&ztest_shared->zs_vdev_lock);
68 +       (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
69  
70         spa_config_enter(spa, RW_READER, FTAG);
71  
72 @@ -869,7 +869,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
73         error = spa_vdev_add(spa, nvroot);
74         nvlist_free(nvroot);
75  
76 -       (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
77 +       (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
78  
79         if (error == ENOSPC)
80                 ztest_record_enospc("spa_vdev_add");
81 @@ -927,7 +927,7 @@ ztest_vdev_attach_detach(ztest_args_t *z
82         int error, expected_error;
83         int fd;
84  
85 -       (void) mutex_lock(&ztest_shared->zs_vdev_lock);
86 +       (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
87  
88         spa_config_enter(spa, RW_READER, FTAG);
89  
90 @@ -1054,7 +1054,7 @@ ztest_vdev_attach_detach(ztest_args_t *z
91                     oldpath, newpath, replacing, error, expected_error);
92         }
93  
94 -       (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
95 +       (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
96  }
97  
98  /*
99 @@ -1071,7 +1071,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
100         size_t fsize;
101         int fd;
102  
103 -       (void) mutex_lock(&ztest_shared->zs_vdev_lock);
104 +       (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
105  
106         /*
107          * Pick a random leaf vdev.
108 @@ -1102,7 +1102,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
109                 (void) close(fd);
110         }
111  
112 -       (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
113 +       (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
114  }
115  
116  /* ARGSUSED */
117 @@ -1198,7 +1198,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
118         uint64_t objects;
119         ztest_replay_t zr;
120  
121 -       (void) rw_rdlock(&ztest_shared->zs_name_lock);
122 +       (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
123         (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
124             (u_longlong_t)za->za_instance);
125  
126 @@ -1242,7 +1242,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
127         if (error) {
128                 if (error == ENOSPC) {
129                         ztest_record_enospc("dmu_objset_create");
130 -                       (void) rw_unlock(&ztest_shared->zs_name_lock);
131 +                       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
132                         return;
133                 }
134                 fatal(0, "dmu_objset_create(%s) = %d", name, error);
135 @@ -1321,7 +1321,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
136         if (error)
137                 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
138  
139 -       (void) rw_unlock(&ztest_shared->zs_name_lock);
140 +       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
141  }
142  
143  /*
144 @@ -1335,7 +1335,7 @@ ztest_dmu_snapshot_create_destroy(ztest_
145         char snapname[100];
146         char osname[MAXNAMELEN];
147  
148 -       (void) rw_rdlock(&ztest_shared->zs_name_lock);
149 +       (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
150         dmu_objset_name(os, osname);
151         (void) snprintf(snapname, 100, "%s@%llu", osname,
152             (u_longlong_t)za->za_instance);
153 @@ -1348,7 +1348,7 @@ ztest_dmu_snapshot_create_destroy(ztest_
154                 ztest_record_enospc("dmu_take_snapshot");
155         else if (error != 0 && error != EEXIST)
156                 fatal(0, "dmu_take_snapshot() = %d", error);
157 -       (void) rw_unlock(&ztest_shared->zs_name_lock);
158 +       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
159  }
160  
161  #define        ZTEST_TRAVERSE_BLOCKS   1000
162 @@ -1992,7 +1992,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
163         int bs = ZTEST_DIROBJ_BLOCKSIZE;
164         int do_free = 0;
165         uint64_t off, txg_how;
166 -       mutex_t *lp;
167 +       pthread_mutex_t *lp;
168         char osname[MAXNAMELEN];
169         char iobuf[SPA_MAXBLOCKSIZE];
170         blkptr_t blk = { 0 };
171 @@ -2041,7 +2041,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
172         }
173  
174         lp = &ztest_shared->zs_sync_lock[b];
175 -       (void) mutex_lock(lp);
176 +       (void) pthread_mutex_lock(lp);
177  
178         wbt->bt_objset = dmu_objset_id(os);
179         wbt->bt_object = ZTEST_DIROBJ;
180 @@ -2087,7 +2087,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
181                 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
182         }
183  
184 -       (void) mutex_unlock(lp);
185 +       (void) pthread_mutex_unlock(lp);
186  
187         if (ztest_random(1000) == 0)
188                 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
189 @@ -2106,7 +2106,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
190         /*
191          * dmu_sync() the block we just wrote.
192          */
193 -       (void) mutex_lock(lp);
194 +       (void) pthread_mutex_lock(lp);
195  
196         blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
197         error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
198 @@ -2114,7 +2114,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
199         if (error) {
200                 dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n",
201                     osname, ZTEST_DIROBJ, blkoff, error);
202 -               (void) mutex_unlock(lp);
203 +               (void) pthread_mutex_unlock(lp);
204                 return;
205         }
206         blkoff = off - blkoff;
207 @@ -2122,7 +2122,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
208         dmu_buf_rele(db, FTAG);
209         za->za_dbuf = NULL;
210  
211 -       (void) mutex_unlock(lp);
212 +       (void) pthread_mutex_unlock(lp);
213  
214         if (error) {
215                 dprintf("dmu_sync(%s, %d, %llx) = %d\n",
216 @@ -2502,7 +2502,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
217         char osname[MAXNAMELEN];
218         int error;
219  
220 -       (void) rw_rdlock(&ztest_shared->zs_name_lock);
221 +       (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
222  
223         dmu_objset_name(os, osname);
224  
225 @@ -2541,7 +2541,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
226                 }
227         }
228  
229 -       (void) rw_unlock(&ztest_shared->zs_name_lock);
230 +       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
231  }
232  
233  static void
234 @@ -2693,7 +2693,7 @@ ztest_spa_rename(ztest_args_t *za)
235         int error;
236         spa_t *spa;
237  
238 -       (void) rw_wrlock(&ztest_shared->zs_name_lock);
239 +       (void) pthread_rwlock_wrlock(&ztest_shared->zs_name_lock);
240  
241         oldname = za->za_pool;
242         newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
243 @@ -2745,7 +2745,7 @@ ztest_spa_rename(ztest_args_t *za)
244  
245         umem_free(newname, strlen(newname) + 1);
246  
247 -       (void) rw_unlock(&ztest_shared->zs_name_lock);
248 +       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
249  }
250  
251  
252 @@ -3090,13 +3090,13 @@ ztest_run(char *pool)
253         ztest_args_t *za;
254         spa_t *spa;
255         char name[100];
256 -       thread_t tid;
257 +       pthread_t tid;
258  
259 -       (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
260 -       (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
261 +       (void) pthread_mutex_init(&zs->zs_vdev_lock, NULL);
262 +       (void) pthread_rwlock_init(&zs->zs_name_lock, NULL);
263  
264         for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
265 -               (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
266 +               (void) pthread_mutex_init(&zs->zs_sync_lock[t], NULL);
267  
268         /*
269          * Destroy one disk before we even start.
270 @@ -3153,7 +3153,7 @@ ztest_run(char *pool)
271          * start the thread before setting the zio_io_fail_shift, which
272          * will indicate our failure rate.
273          */
274 -       error = thr_create(0, 0, ztest_suspend_monitor, NULL, THR_BOUND, &tid);
275 +       error = pthread_create(&tid, NULL, ztest_suspend_monitor, NULL);
276         if (error) {
277                 fatal(0, "can't create suspend monitor thread: error %d",
278                     t, error);
279 @@ -3217,7 +3217,7 @@ ztest_run(char *pool)
280                 if (t < zopt_datasets) {
281                         ztest_replay_t zr;
282                         int test_future = FALSE;
283 -                       (void) rw_rdlock(&ztest_shared->zs_name_lock);
284 +                       (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
285                         (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
286                         error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
287                             ztest_create_cb, NULL);
288 @@ -3225,7 +3225,7 @@ ztest_run(char *pool)
289                                 test_future = TRUE;
290                         } else if (error == ENOSPC) {
291                                 zs->zs_enospc_count++;
292 -                               (void) rw_unlock(&ztest_shared->zs_name_lock);
293 +                               (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
294                                 break;
295                         } else if (error != 0) {
296                                 fatal(0, "dmu_objset_create(%s) = %d",
297 @@ -3236,7 +3236,7 @@ ztest_run(char *pool)
298                         if (error)
299                                 fatal(0, "dmu_objset_open('%s') = %d",
300                                     name, error);
301 -                       (void) rw_unlock(&ztest_shared->zs_name_lock);
302 +                       (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
303                         if (test_future)
304                                 ztest_dmu_check_future_leak(&za[t]);
305                         zr.zr_os = za[d].za_os;
306 @@ -3245,15 +3245,15 @@ ztest_run(char *pool)
307                         za[d].za_zilog = zil_open(za[d].za_os, NULL);
308                 }
309  
310 -               error = thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
311 -                   &za[t].za_thread);
312 +               error = pthread_create(&za[t].za_thread, NULL, ztest_thread,
313 +                   &za[t]);
314                 if (error)
315                         fatal(0, "can't create thread %d: error %d",
316                             t, error);
317         }
318  
319         while (--t >= 0) {
320 -               error = thr_join(za[t].za_thread, NULL, NULL);
321 +               error = pthread_join(za[t].za_thread, NULL);
322                 if (error)
323                         fatal(0, "thr_join(%d) = %d", t, error);
324                 if (za[t].za_th)
325 @@ -3276,14 +3276,14 @@ ztest_run(char *pool)
326          * If we had out-of-space errors, destroy a random objset.
327          */
328         if (zs->zs_enospc_count != 0) {
329 -               (void) rw_rdlock(&ztest_shared->zs_name_lock);
330 +               (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
331                 d = (int)ztest_random(zopt_datasets);
332                 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
333                 if (zopt_verbose >= 3)
334                         (void) printf("Destroying %s to free up space\n", name);
335                 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
336                     DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
337 -               (void) rw_unlock(&ztest_shared->zs_name_lock);
338 +               (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
339         }
340  
341         txg_wait_synced(spa_get_dsl(spa), 0);
342 @@ -3301,7 +3301,7 @@ ztest_run(char *pool)
343         mutex_enter(&spa->spa_zio_lock);
344         cv_broadcast(&spa->spa_zio_cv);
345         mutex_exit(&spa->spa_zio_lock);
346 -       error = thr_join(tid, NULL, NULL);
347 +       error = pthread_join(tid, NULL);
348         if (error)
349                 fatal(0, "thr_join(%d) = %d", tid, error);
350  
351 Index: zfs+chaos4/lib/libuutil/uu_misc.c
352 ===================================================================
353 --- zfs+chaos4.orig/lib/libuutil/uu_misc.c
354 +++ zfs+chaos4/lib/libuutil/uu_misc.c
355 @@ -37,7 +37,6 @@
356  #include <stdlib.h>
357  #include <string.h>
358  #include <sys/debug.h>
359 -#include <thread.h>
360  #include <unistd.h>
361  
362  #if !defined(TEXT_DOMAIN)
363 @@ -70,11 +69,12 @@ static va_list              uu_panic_args;
364  static pthread_t       uu_panic_thread;
365  
366  static uint32_t                _uu_main_error;
367 +static __thread int    _uu_main_thread = 0;
368  
369  void
370  uu_set_error(uint_t code)
371  {
372 -       if (thr_main() != 0) {
373 +       if (_uu_main_thread) {
374                 _uu_main_error = code;
375                 return;
376         }
377 @@ -103,7 +103,7 @@ uu_set_error(uint_t code)
378  uint32_t
379  uu_error(void)
380  {
381 -       if (thr_main() != 0)
382 +       if (_uu_main_thread)
383                 return (_uu_main_error);
384  
385         if (uu_error_key_setup < 0)     /* can't happen? */
386 @@ -255,5 +255,6 @@ uu_release_child(void)
387  static void
388  uu_init(void)
389  {
390 +       _uu_main_thread = 1;
391         (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
392  }
393 Index: zfs+chaos4/lib/libzfscommon/include/sys/zfs_context_user.h
394 ===================================================================
395 --- zfs+chaos4.orig/lib/libzfscommon/include/sys/zfs_context_user.h
396 +++ zfs+chaos4/lib/libzfscommon/include/sys/zfs_context_user.h
397 @@ -52,8 +52,7 @@ extern "C" {
398  #include <errno.h>
399  #include <string.h>
400  #include <strings.h>
401 -#include <synch.h>
402 -#include <thread.h>
403 +#include <pthread.h>
404  #include <assert.h>
405  #include <alloca.h>
406  #include <umem.h>
407 @@ -191,13 +190,15 @@ _NOTE(CONSTCOND) } while (0)
408  /*
409   * Threads
410   */
411 -#define        curthread       ((void *)(uintptr_t)thr_self())
412 +
413 +/* XXX: not portable */
414 +#define        curthread       ((void *)(uintptr_t)pthread_self())
415  
416  typedef struct kthread kthread_t;
417  
418  #define        thread_create(stk, stksize, func, arg, len, pp, state, pri)     \
419         zk_thread_create(func, arg)
420 -#define        thread_exit() thr_exit(NULL)
421 +#define        thread_exit() pthread_exit(NULL)
422  
423  extern kthread_t *zk_thread_create(void (*func)(), void *arg);
424  
425 @@ -207,28 +208,18 @@ extern kthread_t *zk_thread_create(void 
426  /*
427   * Mutexes
428   */
429 +#define MTX_MAGIC 0x9522f51362a6e326ull
430  typedef struct kmutex {
431         void            *m_owner;
432 -       boolean_t       initialized;
433 -       mutex_t         m_lock;
434 +       uint64_t        m_magic;
435 +       pthread_mutex_t m_lock;
436  } kmutex_t;
437  
438 -#define        MUTEX_DEFAULT   USYNC_THREAD
439 -#undef MUTEX_HELD
440 -#define        MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
441 -
442 -/*
443 - * Argh -- we have to get cheesy here because the kernel and userland
444 - * have different signatures for the same routine.
445 - */
446 -extern int _mutex_init(mutex_t *mp, int type, void *arg);
447 -extern int _mutex_destroy(mutex_t *mp);
448 -
449 -#define        mutex_init(mp, b, c, d)         zmutex_init((kmutex_t *)(mp))
450 -#define        mutex_destroy(mp)               zmutex_destroy((kmutex_t *)(mp))
451 +#define        MUTEX_DEFAULT   0
452 +#define        MUTEX_HELD(m)   ((m)->m_owner == curthread)
453  
454 -extern void zmutex_init(kmutex_t *mp);
455 -extern void zmutex_destroy(kmutex_t *mp);
456 +extern void mutex_init(kmutex_t *mp, char *name, int type, void *cookie);
457 +extern void mutex_destroy(kmutex_t *mp);
458  extern void mutex_enter(kmutex_t *mp);
459  extern void mutex_exit(kmutex_t *mp);
460  extern int mutex_tryenter(kmutex_t *mp);
461 @@ -237,23 +228,24 @@ extern void *mutex_owner(kmutex_t *mp);
462  /*
463   * RW locks
464   */
465 +#define RW_MAGIC 0x4d31fb123648e78aull
466  typedef struct krwlock {
467 -       void            *rw_owner;
468 -       boolean_t       initialized;
469 -       rwlock_t        rw_lock;
470 +       void                    *rw_owner;
471 +       void                    *rw_wr_owner;
472 +       uint64_t                rw_magic;
473 +       pthread_rwlock_t        rw_lock;
474 +       uint_t                  rw_readers;
475  } krwlock_t;
476  
477  typedef int krw_t;
478  
479  #define        RW_READER       0
480  #define        RW_WRITER       1
481 -#define        RW_DEFAULT      USYNC_THREAD
482 -
483 -#undef RW_READ_HELD
484 -#define        RW_READ_HELD(x)         _rw_read_held(&(x)->rw_lock)
485 +#define        RW_DEFAULT      0
486  
487 -#undef RW_WRITE_HELD
488 -#define        RW_WRITE_HELD(x)        _rw_write_held(&(x)->rw_lock)
489 +#define        RW_READ_HELD(x)         ((x)->rw_readers > 0)
490 +#define        RW_WRITE_HELD(x)        ((x)->rw_wr_owner == curthread)
491 +#define        RW_LOCK_HELD(x)         (RW_READ_HELD(x) || RW_WRITE_HELD(x))
492  
493  extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
494  extern void rw_destroy(krwlock_t *rwlp);
495 @@ -271,9 +263,13 @@ extern gid_t *crgetgroups(cred_t *cr);
496  /*
497   * Condition variables
498   */
499 -typedef cond_t kcondvar_t;
500 +#define CV_MAGIC 0xd31ea9a83b1b30c4ull
501 +typedef struct kcondvar {
502 +       uint64_t cv_magic;
503 +       pthread_cond_t cv;
504 +} kcondvar_t;
505  
506 -#define        CV_DEFAULT      USYNC_THREAD
507 +#define        CV_DEFAULT      0
508  
509  extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
510  extern void cv_destroy(kcondvar_t *cv);
511 @@ -444,7 +440,8 @@ extern void delay(clock_t ticks);
512  #define        minclsyspri     60
513  #define        maxclsyspri     99
514  
515 -#define        CPU_SEQID       (thr_self() & (max_ncpus - 1))
516 +/* XXX: not portable */
517 +#define        CPU_SEQID       (pthread_self() & (max_ncpus - 1))
518  
519  #define        kcred           NULL
520  #define        CRED()          NULL
521 Index: zfs+chaos4/lib/libzpool/kernel.c
522 ===================================================================
523 --- zfs+chaos4.orig/lib/libzpool/kernel.c
524 +++ zfs+chaos4/lib/libzpool/kernel.c
525 @@ -38,6 +38,7 @@
526  #include <sys/zfs_context.h>
527  #include <sys/zmod.h>
528  #include <sys/utsname.h>
529 +#include <sys/time.h>
530  
531  /*
532   * Emulation of kernel services in userland.
533 @@ -60,11 +61,15 @@ struct utsname utsname = {
534  kthread_t *
535  zk_thread_create(void (*func)(), void *arg)
536  {
537 -       thread_t tid;
538 +       pthread_t tid;
539  
540 -       VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
541 -           &tid) == 0);
542 +       pthread_attr_t attr;
543 +       VERIFY(pthread_attr_init(&attr) == 0);
544 +       VERIFY(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
545  
546 +       VERIFY(pthread_create(&tid, &attr, (void *(*)(void *))func, arg) == 0);
547 +
548 +       /* XXX: not portable */
549         return ((void *)(uintptr_t)tid);
550  }
551  
552 @@ -97,30 +102,37 @@ kstat_delete(kstat_t *ksp)
553   * =========================================================================
554   */
555  void
556 -zmutex_init(kmutex_t *mp)
557 +mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
558  {
559 +       ASSERT(type == MUTEX_DEFAULT);
560 +       ASSERT(cookie == NULL);
561 +
562 +#ifdef IM_FEELING_LUCKY
563 +       ASSERT(mp->m_magic != MTX_MAGIC);
564 +#endif
565 +
566         mp->m_owner = NULL;
567 -       mp->initialized = B_TRUE;
568 -       (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
569 +       mp->m_magic = MTX_MAGIC;
570 +       VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0);
571  }
572  
573  void
574 -zmutex_destroy(kmutex_t *mp)
575 +mutex_destroy(kmutex_t *mp)
576  {
577 -       ASSERT(mp->initialized == B_TRUE);
578 +       ASSERT(mp->m_magic == MTX_MAGIC);
579         ASSERT(mp->m_owner == NULL);
580 -       (void) _mutex_destroy(&(mp)->m_lock);
581 +       VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
582         mp->m_owner = (void *)-1UL;
583 -       mp->initialized = B_FALSE;
584 +       mp->m_magic = 0;
585  }
586  
587  void
588  mutex_enter(kmutex_t *mp)
589  {
590 -       ASSERT(mp->initialized == B_TRUE);
591 +       ASSERT(mp->m_magic == MTX_MAGIC);
592         ASSERT(mp->m_owner != (void *)-1UL);
593         ASSERT(mp->m_owner != curthread);
594 -       VERIFY(mutex_lock(&mp->m_lock) == 0);
595 +       VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0);
596         ASSERT(mp->m_owner == NULL);
597         mp->m_owner = curthread;
598  }
599 @@ -128,9 +140,9 @@ mutex_enter(kmutex_t *mp)
600  int
601  mutex_tryenter(kmutex_t *mp)
602  {
603 -       ASSERT(mp->initialized == B_TRUE);
604 +       ASSERT(mp->m_magic == MTX_MAGIC);
605         ASSERT(mp->m_owner != (void *)-1UL);
606 -       if (0 == mutex_trylock(&mp->m_lock)) {
607 +       if (0 == pthread_mutex_trylock(&mp->m_lock)) {
608                 ASSERT(mp->m_owner == NULL);
609                 mp->m_owner = curthread;
610                 return (1);
611 @@ -142,16 +154,16 @@ mutex_tryenter(kmutex_t *mp)
612  void
613  mutex_exit(kmutex_t *mp)
614  {
615 -       ASSERT(mp->initialized == B_TRUE);
616 +       ASSERT(mp->m_magic == MTX_MAGIC);
617         ASSERT(mutex_owner(mp) == curthread);
618         mp->m_owner = NULL;
619 -       VERIFY(mutex_unlock(&mp->m_lock) == 0);
620 +       VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0);
621  }
622  
623  void *
624  mutex_owner(kmutex_t *mp)
625  {
626 -       ASSERT(mp->initialized == B_TRUE);
627 +       ASSERT(mp->m_magic == MTX_MAGIC);
628         return (mp->m_owner);
629  }
630  
631 @@ -164,31 +176,48 @@ mutex_owner(kmutex_t *mp)
632  void
633  rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
634  {
635 -       rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
636 +       ASSERT(type == RW_DEFAULT);
637 +       ASSERT(arg == NULL);
638 +
639 +#ifdef IM_FEELING_LUCKY
640 +       ASSERT(rwlp->rw_magic != RW_MAGIC);
641 +#endif
642 +
643 +       VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0);
644         rwlp->rw_owner = NULL;
645 -       rwlp->initialized = B_TRUE;
646 +       rwlp->rw_wr_owner = NULL;
647 +       rwlp->rw_readers = 0;
648 +       rwlp->rw_magic = RW_MAGIC;
649  }
650  
651  void
652  rw_destroy(krwlock_t *rwlp)
653  {
654 -       rwlock_destroy(&rwlp->rw_lock);
655 -       rwlp->rw_owner = (void *)-1UL;
656 -       rwlp->initialized = B_FALSE;
657 +       ASSERT(rwlp->rw_magic == RW_MAGIC);
658 +
659 +       VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
660 +       rwlp->rw_magic = 0;
661  }
662  
663  void
664  rw_enter(krwlock_t *rwlp, krw_t rw)
665  {
666 -       ASSERT(!RW_LOCK_HELD(rwlp));
667 -       ASSERT(rwlp->initialized == B_TRUE);
668 -       ASSERT(rwlp->rw_owner != (void *)-1UL);
669 +       ASSERT(rwlp->rw_magic == RW_MAGIC);
670         ASSERT(rwlp->rw_owner != curthread);
671 +       ASSERT(rwlp->rw_wr_owner != curthread);
672  
673 -       if (rw == RW_READER)
674 -               (void) rw_rdlock(&rwlp->rw_lock);
675 -       else
676 -               (void) rw_wrlock(&rwlp->rw_lock);
677 +       if (rw == RW_READER) {
678 +               VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0);
679 +               ASSERT(rwlp->rw_wr_owner == NULL);
680 +
681 +               atomic_inc_uint(&rwlp->rw_readers);
682 +       } else {
683 +               VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0);
684 +               ASSERT(rwlp->rw_wr_owner == NULL);
685 +               ASSERT3U(rwlp->rw_readers, ==, 0);
686 +
687 +               rwlp->rw_wr_owner = curthread;
688 +       }
689  
690         rwlp->rw_owner = curthread;
691  }
692 @@ -196,11 +225,16 @@ rw_enter(krwlock_t *rwlp, krw_t rw)
693  void
694  rw_exit(krwlock_t *rwlp)
695  {
696 -       ASSERT(rwlp->initialized == B_TRUE);
697 -       ASSERT(rwlp->rw_owner != (void *)-1UL);
698 +       ASSERT(rwlp->rw_magic == RW_MAGIC);
699 +       ASSERT(RW_LOCK_HELD(rwlp));
700 +
701 +       if (RW_READ_HELD(rwlp))
702 +               atomic_dec_uint(&rwlp->rw_readers);
703 +       else
704 +               rwlp->rw_wr_owner = NULL;
705  
706         rwlp->rw_owner = NULL;
707 -       (void) rw_unlock(&rwlp->rw_lock);
708 +       VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0);
709  }
710  
711  int
712 @@ -208,19 +242,29 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
713  {
714         int rv;
715  
716 -       ASSERT(rwlp->initialized == B_TRUE);
717 -       ASSERT(rwlp->rw_owner != (void *)-1UL);
718 +       ASSERT(rwlp->rw_magic == RW_MAGIC);
719  
720         if (rw == RW_READER)
721 -               rv = rw_tryrdlock(&rwlp->rw_lock);
722 +               rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
723         else
724 -               rv = rw_trywrlock(&rwlp->rw_lock);
725 +               rv = pthread_rwlock_trywrlock(&rwlp->rw_lock);
726  
727         if (rv == 0) {
728 +               ASSERT(rwlp->rw_wr_owner == NULL);
729 +
730 +               if (rw == RW_READER)
731 +                       atomic_inc_uint(&rwlp->rw_readers);
732 +               else {
733 +                       ASSERT3U(rwlp->rw_readers, ==, 0);
734 +                       rwlp->rw_wr_owner = curthread;
735 +               }
736 +
737                 rwlp->rw_owner = curthread;
738                 return (1);
739         }
740  
741 +       VERIFY3S(rv, ==, EBUSY);
742 +
743         return (0);
744  }
745  
746 @@ -228,8 +272,7 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
747  int
748  rw_tryupgrade(krwlock_t *rwlp)
749  {
750 -       ASSERT(rwlp->initialized == B_TRUE);
751 -       ASSERT(rwlp->rw_owner != (void *)-1UL);
752 +       ASSERT(rwlp->rw_magic == RW_MAGIC);
753  
754         return (0);
755  }
756 @@ -243,22 +286,34 @@ rw_tryupgrade(krwlock_t *rwlp)
757  void
758  cv_init(kcondvar_t *cv, char *name, int type, void *arg)
759  {
760 -       VERIFY(cond_init(cv, type, NULL) == 0);
761 +       ASSERT(type == CV_DEFAULT);
762 +
763 +#ifdef IM_FEELING_LUCKY
764 +       ASSERT(cv->cv_magic != CV_MAGIC);
765 +#endif
766 +
767 +       cv->cv_magic = CV_MAGIC;
768 +
769 +       VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
770  }
771  
772  void
773  cv_destroy(kcondvar_t *cv)
774  {
775 -       VERIFY(cond_destroy(cv) == 0);
776 +       ASSERT(cv->cv_magic == CV_MAGIC);
777 +       VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
778 +       cv->cv_magic = 0;
779  }
780  
781  void
782  cv_wait(kcondvar_t *cv, kmutex_t *mp)
783  {
784 +       ASSERT(cv->cv_magic == CV_MAGIC);
785         ASSERT(mutex_owner(mp) == curthread);
786         mp->m_owner = NULL;
787 -       int ret = cond_wait(cv, &mp->m_lock);
788 -       VERIFY(ret == 0 || ret == EINTR);
789 +       int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
790 +       if (ret != 0)
791 +               VERIFY3S(ret, ==, EINTR);
792         mp->m_owner = curthread;
793  }
794  
795 @@ -266,29 +321,38 @@ clock_t
796  cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
797  {
798         int error;
799 +       struct timeval tv;
800         timestruc_t ts;
801         clock_t delta;
802  
803 +       ASSERT(cv->cv_magic == CV_MAGIC);
804 +
805  top:
806         delta = abstime - lbolt;
807         if (delta <= 0)
808                 return (-1);
809  
810 -       ts.tv_sec = delta / hz;
811 -       ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
812 +       VERIFY(gettimeofday(&tv, NULL) == 0);
813 +
814 +       ts.tv_sec = tv.tv_sec + delta / hz;
815 +       ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
816 +       if (ts.tv_nsec >= NANOSEC) {
817 +               ts.tv_sec++;
818 +               ts.tv_nsec -= NANOSEC;
819 +       }
820  
821         ASSERT(mutex_owner(mp) == curthread);
822         mp->m_owner = NULL;
823 -       error = cond_reltimedwait(cv, &mp->m_lock, &ts);
824 +       error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
825         mp->m_owner = curthread;
826  
827 -       if (error == ETIME)
828 +       if (error == ETIMEDOUT)
829                 return (-1);
830  
831         if (error == EINTR)
832                 goto top;
833  
834 -       ASSERT(error == 0);
835 +       VERIFY3S(error, ==, 0);
836  
837         return (1);
838  }
839 @@ -296,13 +360,15 @@ top:
840  void
841  cv_signal(kcondvar_t *cv)
842  {
843 -       VERIFY(cond_signal(cv) == 0);
844 +       ASSERT(cv->cv_magic == CV_MAGIC);
845 +       VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
846  }
847  
848  void
849  cv_broadcast(kcondvar_t *cv)
850  {
851 -       VERIFY(cond_broadcast(cv) == 0);
852 +       ASSERT(cv->cv_magic == CV_MAGIC);
853 +       VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
854  }
855  
856  /*
857 @@ -549,11 +615,11 @@ __dprintf(const char *file, const char *
858             dprintf_find_string(func)) {
859                 /* Print out just the function name if requested */
860                 flockfile(stdout);
861 -               /* XXX: the following printf may not be portable */
862 +               /* XXX: the following 2 printfs may not be portable */
863                 if (dprintf_find_string("pid"))
864                         (void) printf("%llu ", (u_longlong_t) getpid());
865                 if (dprintf_find_string("tid"))
866 -                       (void) printf("%u ", (uint_t) thr_self());
867 +                       (void) printf("%u ", (uint_t) pthread_self());
868                 if (dprintf_find_string("cpu"))
869                         (void) printf("%u ", getcpuid());
870                 if (dprintf_find_string("time"))
871 Index: zfs+chaos4/lib/libzpool/taskq.c
872 ===================================================================
873 --- zfs+chaos4.orig/lib/libzpool/taskq.c
874 +++ zfs+chaos4/lib/libzpool/taskq.c
875 @@ -43,7 +43,7 @@ struct taskq {
876         krwlock_t       tq_threadlock;
877         kcondvar_t      tq_dispatch_cv;
878         kcondvar_t      tq_wait_cv;
879 -       thread_t        *tq_threadlist;
880 +       pthread_t       *tq_threadlist;
881         int             tq_flags;
882         int             tq_active;
883         int             tq_nthreads;
884 @@ -186,7 +186,7 @@ taskq_create(const char *name, int nthre
885         tq->tq_maxalloc = maxalloc;
886         tq->tq_task.task_next = &tq->tq_task;
887         tq->tq_task.task_prev = &tq->tq_task;
888 -       tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
889 +       tq->tq_threadlist = kmem_alloc(nthreads * sizeof (pthread_t), KM_SLEEP);
890  
891         if (flags & TASKQ_PREPOPULATE) {
892                 mutex_enter(&tq->tq_lock);
893 @@ -196,8 +196,8 @@ taskq_create(const char *name, int nthre
894         }
895  
896         for (t = 0; t < nthreads; t++)
897 -               VERIFY(thr_create(0, 0, taskq_thread,
898 -                   tq, THR_BOUND, &tq->tq_threadlist[t]) == 0);
899 +               VERIFY(pthread_create(&tq->tq_threadlist[t],
900 +                   NULL, taskq_thread, tq) == 0);
901  
902         return (tq);
903  }
904 @@ -227,9 +227,9 @@ taskq_destroy(taskq_t *tq)
905         mutex_exit(&tq->tq_lock);
906  
907         for (t = 0; t < nthreads; t++)
908 -               VERIFY(thr_join(tq->tq_threadlist[t], NULL, NULL) == 0);
909 +               VERIFY(pthread_join(tq->tq_threadlist[t], NULL) == 0);
910  
911 -       kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
912 +       kmem_free(tq->tq_threadlist, nthreads * sizeof (pthread_t));
913  
914         rw_destroy(&tq->tq_threadlock);
915         mutex_destroy(&tq->tq_lock);
916 @@ -248,7 +248,7 @@ taskq_member(taskq_t *tq, void *t)
917                 return (1);
918  
919         for (i = 0; i < tq->tq_nthreads; i++)
920 -               if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
921 +               if (tq->tq_threadlist[i] == (pthread_t)(uintptr_t)t)
922                         return (1);
923  
924         return (0);