Print this page
*** NO COMMENTS ***
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libzpool/common/kernel.c
+++ new/usr/src/lib/libzpool/common/kernel.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <assert.h>
27 27 #include <fcntl.h>
28 28 #include <poll.h>
29 29 #include <stdio.h>
30 30 #include <stdlib.h>
31 31 #include <string.h>
32 32 #include <zlib.h>
33 33 #include <sys/spa.h>
34 34 #include <sys/stat.h>
35 35 #include <sys/processor.h>
36 36 #include <sys/zfs_context.h>
37 37 #include <sys/zmod.h>
38 38 #include <sys/utsname.h>
39 39 #include <sys/systeminfo.h>
40 40
41 41 /*
42 42 * Emulation of kernel services in userland.
43 43 */
44 44
45 45 int aok;
46 46 uint64_t physmem;
47 47 vnode_t *rootdir = (vnode_t *)0xabcd1234;
48 48 char hw_serial[HW_HOSTID_LEN];
49 49 vmem_t *zio_arena = NULL;
50 50
51 51 struct utsname utsname = {
52 52 "userland", "libzpool", "1", "1", "na"
53 53 };
54 54
55 55 /* this only exists to have its address taken */
56 56 struct proc p0;
57 57
58 58 /*
59 59 * =========================================================================
60 60 * threads
61 61 * =========================================================================
62 62 */
63 63 /*ARGSUSED*/
64 64 kthread_t *
65 65 zk_thread_create(void (*func)(), void *arg)
66 66 {
67 67 thread_t tid;
68 68
69 69 VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
70 70 &tid) == 0);
71 71
72 72 return ((void *)(uintptr_t)tid);
73 73 }
74 74
75 75 /*
76 76 * =========================================================================
77 77 * kstats
78 78 * =========================================================================
79 79 */
80 80 /*ARGSUSED*/
81 81 kstat_t *
82 82 kstat_create(char *module, int instance, char *name, char *class,
83 83 uchar_t type, ulong_t ndata, uchar_t ks_flag)
84 84 {
85 85 return (NULL);
86 86 }
87 87
88 88 /*ARGSUSED*/
89 89 void
90 90 kstat_install(kstat_t *ksp)
91 91 {}
92 92
93 93 /*ARGSUSED*/
94 94 void
95 95 kstat_delete(kstat_t *ksp)
96 96 {}
97 97
98 98 /*
99 99 * =========================================================================
100 100 * mutexes
101 101 * =========================================================================
102 102 */
103 103 void
104 104 zmutex_init(kmutex_t *mp)
105 105 {
106 106 mp->m_owner = NULL;
107 107 mp->initialized = B_TRUE;
108 108 (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
109 109 }
110 110
111 111 void
112 112 zmutex_destroy(kmutex_t *mp)
113 113 {
114 114 ASSERT(mp->initialized == B_TRUE);
115 115 ASSERT(mp->m_owner == NULL);
116 116 (void) _mutex_destroy(&(mp)->m_lock);
117 117 mp->m_owner = (void *)-1UL;
118 118 mp->initialized = B_FALSE;
119 119 }
120 120
121 121 void
122 122 mutex_enter(kmutex_t *mp)
123 123 {
124 124 ASSERT(mp->initialized == B_TRUE);
125 125 ASSERT(mp->m_owner != (void *)-1UL);
126 126 ASSERT(mp->m_owner != curthread);
127 127 VERIFY(mutex_lock(&mp->m_lock) == 0);
128 128 ASSERT(mp->m_owner == NULL);
129 129 mp->m_owner = curthread;
130 130 }
131 131
132 132 int
133 133 mutex_tryenter(kmutex_t *mp)
134 134 {
135 135 ASSERT(mp->initialized == B_TRUE);
136 136 ASSERT(mp->m_owner != (void *)-1UL);
137 137 if (0 == mutex_trylock(&mp->m_lock)) {
138 138 ASSERT(mp->m_owner == NULL);
139 139 mp->m_owner = curthread;
140 140 return (1);
141 141 } else {
142 142 return (0);
143 143 }
144 144 }
145 145
146 146 void
147 147 mutex_exit(kmutex_t *mp)
148 148 {
149 149 ASSERT(mp->initialized == B_TRUE);
150 150 ASSERT(mutex_owner(mp) == curthread);
151 151 mp->m_owner = NULL;
152 152 VERIFY(mutex_unlock(&mp->m_lock) == 0);
153 153 }
154 154
155 155 void *
156 156 mutex_owner(kmutex_t *mp)
157 157 {
158 158 ASSERT(mp->initialized == B_TRUE);
159 159 return (mp->m_owner);
160 160 }
161 161
162 162 /*
163 163 * =========================================================================
164 164 * rwlocks
165 165 * =========================================================================
166 166 */
167 167 /*ARGSUSED*/
168 168 void
169 169 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
170 170 {
171 171 rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
172 172 rwlp->rw_owner = NULL;
173 173 rwlp->initialized = B_TRUE;
174 174 }
175 175
176 176 void
177 177 rw_destroy(krwlock_t *rwlp)
178 178 {
179 179 rwlock_destroy(&rwlp->rw_lock);
180 180 rwlp->rw_owner = (void *)-1UL;
181 181 rwlp->initialized = B_FALSE;
182 182 }
183 183
184 184 void
185 185 rw_enter(krwlock_t *rwlp, krw_t rw)
186 186 {
187 187 ASSERT(!RW_LOCK_HELD(rwlp));
188 188 ASSERT(rwlp->initialized == B_TRUE);
189 189 ASSERT(rwlp->rw_owner != (void *)-1UL);
190 190 ASSERT(rwlp->rw_owner != curthread);
191 191
192 192 if (rw == RW_READER)
193 193 VERIFY(rw_rdlock(&rwlp->rw_lock) == 0);
194 194 else
195 195 VERIFY(rw_wrlock(&rwlp->rw_lock) == 0);
196 196
197 197 rwlp->rw_owner = curthread;
198 198 }
199 199
200 200 void
201 201 rw_exit(krwlock_t *rwlp)
202 202 {
203 203 ASSERT(rwlp->initialized == B_TRUE);
204 204 ASSERT(rwlp->rw_owner != (void *)-1UL);
205 205
206 206 rwlp->rw_owner = NULL;
207 207 VERIFY(rw_unlock(&rwlp->rw_lock) == 0);
208 208 }
209 209
210 210 int
211 211 rw_tryenter(krwlock_t *rwlp, krw_t rw)
212 212 {
213 213 int rv;
214 214
215 215 ASSERT(rwlp->initialized == B_TRUE);
216 216 ASSERT(rwlp->rw_owner != (void *)-1UL);
217 217
218 218 if (rw == RW_READER)
219 219 rv = rw_tryrdlock(&rwlp->rw_lock);
220 220 else
221 221 rv = rw_trywrlock(&rwlp->rw_lock);
222 222
223 223 if (rv == 0) {
224 224 rwlp->rw_owner = curthread;
225 225 return (1);
226 226 }
227 227
228 228 return (0);
229 229 }
230 230
231 231 /*ARGSUSED*/
232 232 int
233 233 rw_tryupgrade(krwlock_t *rwlp)
234 234 {
235 235 ASSERT(rwlp->initialized == B_TRUE);
236 236 ASSERT(rwlp->rw_owner != (void *)-1UL);
237 237
238 238 return (0);
239 239 }
240 240
241 241 /*
242 242 * =========================================================================
243 243 * condition variables
244 244 * =========================================================================
245 245 */
246 246 /*ARGSUSED*/
247 247 void
248 248 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
↓ open down ↓ |
248 lines elided |
↑ open up ↑ |
249 249 {
250 250 VERIFY(cond_init(cv, type, NULL) == 0);
251 251 }
252 252
253 253 void
254 254 cv_destroy(kcondvar_t *cv)
255 255 {
256 256 VERIFY(cond_destroy(cv) == 0);
257 257 }
258 258
259 -void
260 -cv_wait(kcondvar_t *cv, kmutex_t *mp)
259 +int
260 +cv_wait_sig(kcondvar_t *cv, kmutex_t *mp)
261 261 {
262 262 ASSERT(mutex_owner(mp) == curthread);
263 263 mp->m_owner = NULL;
264 264 int ret = cond_wait(cv, &mp->m_lock);
265 265 VERIFY(ret == 0 || ret == EINTR);
266 266 mp->m_owner = curthread;
267 + return (ret == EINTR ? 0 : 1);
268 +}
269 +
270 +void
271 +cv_wait(kcondvar_t *cv, kmutex_t *mp)
272 +{
273 + (void) cv_wait_sig(cv, mp);
267 274 }
268 275
269 276 clock_t
270 277 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
271 278 {
272 279 int error;
273 280 timestruc_t ts;
274 281 clock_t delta;
275 282
276 283 top:
277 284 delta = abstime - ddi_get_lbolt();
278 285 if (delta <= 0)
279 286 return (-1);
280 287
281 288 ts.tv_sec = delta / hz;
282 289 ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
283 290
284 291 ASSERT(mutex_owner(mp) == curthread);
285 292 mp->m_owner = NULL;
286 293 error = cond_reltimedwait(cv, &mp->m_lock, &ts);
287 294 mp->m_owner = curthread;
288 295
289 296 if (error == ETIME)
290 297 return (-1);
291 298
292 299 if (error == EINTR)
293 300 goto top;
294 301
295 302 ASSERT(error == 0);
296 303
297 304 return (1);
298 305 }
299 306
300 307 void
301 308 cv_signal(kcondvar_t *cv)
302 309 {
303 310 VERIFY(cond_signal(cv) == 0);
304 311 }
305 312
306 313 void
307 314 cv_broadcast(kcondvar_t *cv)
308 315 {
309 316 VERIFY(cond_broadcast(cv) == 0);
310 317 }
311 318
312 319 /*
313 320 * =========================================================================
314 321 * vnode operations
315 322 * =========================================================================
316 323 */
317 324 /*
318 325 * Note: for the xxxat() versions of these functions, we assume that the
319 326 * starting vp is always rootdir (which is true for spa_directory.c, the only
320 327 * ZFS consumer of these interfaces). We assert this is true, and then emulate
321 328 * them by adding '/' in front of the path.
322 329 */
323 330
324 331 /*ARGSUSED*/
325 332 int
326 333 vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
327 334 {
328 335 int fd;
329 336 vnode_t *vp;
330 337 int old_umask;
331 338 char realpath[MAXPATHLEN];
332 339 struct stat64 st;
333 340
334 341 /*
335 342 * If we're accessing a real disk from userland, we need to use
336 343 * the character interface to avoid caching. This is particularly
337 344 * important if we're trying to look at a real in-kernel storage
338 345 * pool from userland, e.g. via zdb, because otherwise we won't
339 346 * see the changes occurring under the segmap cache.
340 347 * On the other hand, the stupid character device returns zero
341 348 * for its size. So -- gag -- we open the block device to get
342 349 * its size, and remember it for subsequent VOP_GETATTR().
343 350 */
344 351 if (strncmp(path, "/dev/", 5) == 0) {
345 352 char *dsk;
346 353 fd = open64(path, O_RDONLY);
347 354 if (fd == -1)
348 355 return (errno);
349 356 if (fstat64(fd, &st) == -1) {
350 357 close(fd);
351 358 return (errno);
352 359 }
353 360 close(fd);
354 361 (void) sprintf(realpath, "%s", path);
355 362 dsk = strstr(path, "/dsk/");
356 363 if (dsk != NULL)
357 364 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
358 365 dsk + 1);
359 366 } else {
360 367 (void) sprintf(realpath, "%s", path);
361 368 if (!(flags & FCREAT) && stat64(realpath, &st) == -1)
362 369 return (errno);
363 370 }
364 371
365 372 if (flags & FCREAT)
366 373 old_umask = umask(0);
367 374
368 375 /*
369 376 * The construct 'flags - FREAD' conveniently maps combinations of
370 377 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
371 378 */
372 379 fd = open64(realpath, flags - FREAD, mode);
373 380
374 381 if (flags & FCREAT)
375 382 (void) umask(old_umask);
376 383
377 384 if (fd == -1)
378 385 return (errno);
379 386
380 387 if (fstat64(fd, &st) == -1) {
381 388 close(fd);
382 389 return (errno);
383 390 }
384 391
385 392 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
386 393
387 394 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
388 395
389 396 vp->v_fd = fd;
390 397 vp->v_size = st.st_size;
391 398 vp->v_path = spa_strdup(path);
392 399
393 400 return (0);
394 401 }
395 402
396 403 /*ARGSUSED*/
397 404 int
398 405 vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
399 406 int x3, vnode_t *startvp, int fd)
400 407 {
401 408 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
402 409 int ret;
403 410
404 411 ASSERT(startvp == rootdir);
405 412 (void) sprintf(realpath, "/%s", path);
406 413
407 414 /* fd ignored for now, need if want to simulate nbmand support */
408 415 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
409 416
410 417 umem_free(realpath, strlen(path) + 2);
411 418
412 419 return (ret);
413 420 }
414 421
415 422 /*ARGSUSED*/
416 423 int
417 424 vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
418 425 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
419 426 {
420 427 ssize_t iolen, split;
421 428
422 429 if (uio == UIO_READ) {
423 430 iolen = pread64(vp->v_fd, addr, len, offset);
424 431 } else {
425 432 /*
426 433 * To simulate partial disk writes, we split writes into two
427 434 * system calls so that the process can be killed in between.
428 435 */
429 436 int sectors = len >> SPA_MINBLOCKSHIFT;
430 437 split = (sectors > 0 ? rand() % sectors : 0) <<
431 438 SPA_MINBLOCKSHIFT;
432 439 iolen = pwrite64(vp->v_fd, addr, split, offset);
433 440 iolen += pwrite64(vp->v_fd, (char *)addr + split,
434 441 len - split, offset + split);
435 442 }
436 443
437 444 if (iolen == -1)
438 445 return (errno);
439 446 if (residp)
440 447 *residp = len - iolen;
441 448 else if (iolen != len)
442 449 return (EIO);
443 450 return (0);
444 451 }
445 452
446 453 void
447 454 vn_close(vnode_t *vp)
448 455 {
449 456 close(vp->v_fd);
450 457 spa_strfree(vp->v_path);
451 458 umem_free(vp, sizeof (vnode_t));
452 459 }
453 460
454 461 /*
455 462 * At a minimum we need to update the size since vdev_reopen()
456 463 * will no longer call vn_openat().
457 464 */
458 465 int
459 466 fop_getattr(vnode_t *vp, vattr_t *vap)
460 467 {
461 468 struct stat64 st;
462 469
463 470 if (fstat64(vp->v_fd, &st) == -1) {
464 471 close(vp->v_fd);
465 472 return (errno);
466 473 }
467 474
468 475 vap->va_size = st.st_size;
469 476 return (0);
470 477 }
471 478
472 479 #ifdef ZFS_DEBUG
473 480
474 481 /*
475 482 * =========================================================================
476 483 * Figure out which debugging statements to print
477 484 * =========================================================================
478 485 */
479 486
480 487 static char *dprintf_string;
481 488 static int dprintf_print_all;
482 489
483 490 int
484 491 dprintf_find_string(const char *string)
485 492 {
486 493 char *tmp_str = dprintf_string;
487 494 int len = strlen(string);
488 495
489 496 /*
490 497 * Find out if this is a string we want to print.
491 498 * String format: file1.c,function_name1,file2.c,file3.c
492 499 */
493 500
494 501 while (tmp_str != NULL) {
495 502 if (strncmp(tmp_str, string, len) == 0 &&
496 503 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
497 504 return (1);
498 505 tmp_str = strchr(tmp_str, ',');
499 506 if (tmp_str != NULL)
500 507 tmp_str++; /* Get rid of , */
501 508 }
502 509 return (0);
503 510 }
504 511
505 512 void
506 513 dprintf_setup(int *argc, char **argv)
507 514 {
508 515 int i, j;
509 516
510 517 /*
511 518 * Debugging can be specified two ways: by setting the
512 519 * environment variable ZFS_DEBUG, or by including a
513 520 * "debug=..." argument on the command line. The command
514 521 * line setting overrides the environment variable.
515 522 */
516 523
517 524 for (i = 1; i < *argc; i++) {
518 525 int len = strlen("debug=");
519 526 /* First look for a command line argument */
520 527 if (strncmp("debug=", argv[i], len) == 0) {
521 528 dprintf_string = argv[i] + len;
522 529 /* Remove from args */
523 530 for (j = i; j < *argc; j++)
524 531 argv[j] = argv[j+1];
525 532 argv[j] = NULL;
526 533 (*argc)--;
527 534 }
528 535 }
529 536
530 537 if (dprintf_string == NULL) {
531 538 /* Look for ZFS_DEBUG environment variable */
532 539 dprintf_string = getenv("ZFS_DEBUG");
533 540 }
534 541
535 542 /*
536 543 * Are we just turning on all debugging?
537 544 */
538 545 if (dprintf_find_string("on"))
539 546 dprintf_print_all = 1;
540 547 }
541 548
542 549 /*
543 550 * =========================================================================
544 551 * debug printfs
545 552 * =========================================================================
546 553 */
547 554 void
548 555 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
549 556 {
550 557 const char *newfile;
551 558 va_list adx;
552 559
553 560 /*
554 561 * Get rid of annoying "../common/" prefix to filename.
555 562 */
556 563 newfile = strrchr(file, '/');
557 564 if (newfile != NULL) {
558 565 newfile = newfile + 1; /* Get rid of leading / */
559 566 } else {
560 567 newfile = file;
561 568 }
562 569
563 570 if (dprintf_print_all ||
564 571 dprintf_find_string(newfile) ||
565 572 dprintf_find_string(func)) {
566 573 /* Print out just the function name if requested */
567 574 flockfile(stdout);
568 575 if (dprintf_find_string("pid"))
569 576 (void) printf("%d ", getpid());
570 577 if (dprintf_find_string("tid"))
571 578 (void) printf("%u ", thr_self());
572 579 if (dprintf_find_string("cpu"))
573 580 (void) printf("%u ", getcpuid());
574 581 if (dprintf_find_string("time"))
575 582 (void) printf("%llu ", gethrtime());
576 583 if (dprintf_find_string("long"))
577 584 (void) printf("%s, line %d: ", newfile, line);
578 585 (void) printf("%s: ", func);
579 586 va_start(adx, fmt);
580 587 (void) vprintf(fmt, adx);
581 588 va_end(adx);
582 589 funlockfile(stdout);
583 590 }
584 591 }
585 592
586 593 #endif /* ZFS_DEBUG */
587 594
588 595 /*
589 596 * =========================================================================
590 597 * cmn_err() and panic()
591 598 * =========================================================================
592 599 */
593 600 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
594 601 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
595 602
596 603 void
597 604 vpanic(const char *fmt, va_list adx)
598 605 {
599 606 (void) fprintf(stderr, "error: ");
600 607 (void) vfprintf(stderr, fmt, adx);
601 608 (void) fprintf(stderr, "\n");
602 609
603 610 abort(); /* think of it as a "user-level crash dump" */
604 611 }
605 612
606 613 void
607 614 panic(const char *fmt, ...)
608 615 {
609 616 va_list adx;
610 617
611 618 va_start(adx, fmt);
612 619 vpanic(fmt, adx);
613 620 va_end(adx);
614 621 }
615 622
616 623 void
617 624 vcmn_err(int ce, const char *fmt, va_list adx)
618 625 {
619 626 if (ce == CE_PANIC)
620 627 vpanic(fmt, adx);
621 628 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
622 629 (void) fprintf(stderr, "%s", ce_prefix[ce]);
623 630 (void) vfprintf(stderr, fmt, adx);
624 631 (void) fprintf(stderr, "%s", ce_suffix[ce]);
625 632 }
626 633 }
627 634
628 635 /*PRINTFLIKE2*/
629 636 void
630 637 cmn_err(int ce, const char *fmt, ...)
631 638 {
632 639 va_list adx;
633 640
634 641 va_start(adx, fmt);
635 642 vcmn_err(ce, fmt, adx);
636 643 va_end(adx);
637 644 }
638 645
639 646 /*
640 647 * =========================================================================
641 648 * kobj interfaces
642 649 * =========================================================================
643 650 */
644 651 struct _buf *
645 652 kobj_open_file(char *name)
646 653 {
647 654 struct _buf *file;
648 655 vnode_t *vp;
649 656
650 657 /* set vp as the _fd field of the file */
651 658 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
652 659 -1) != 0)
653 660 return ((void *)-1UL);
654 661
655 662 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
656 663 file->_fd = (intptr_t)vp;
657 664 return (file);
658 665 }
659 666
660 667 int
661 668 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
662 669 {
663 670 ssize_t resid;
664 671
665 672 vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
666 673 UIO_SYSSPACE, 0, 0, 0, &resid);
667 674
668 675 return (size - resid);
669 676 }
670 677
671 678 void
672 679 kobj_close_file(struct _buf *file)
673 680 {
674 681 vn_close((vnode_t *)file->_fd);
675 682 umem_free(file, sizeof (struct _buf));
676 683 }
677 684
678 685 int
679 686 kobj_get_filesize(struct _buf *file, uint64_t *size)
680 687 {
681 688 struct stat64 st;
682 689 vnode_t *vp = (vnode_t *)file->_fd;
683 690
684 691 if (fstat64(vp->v_fd, &st) == -1) {
685 692 vn_close(vp);
686 693 return (errno);
687 694 }
688 695 *size = st.st_size;
689 696 return (0);
690 697 }
691 698
692 699 /*
693 700 * =========================================================================
694 701 * misc routines
695 702 * =========================================================================
696 703 */
697 704
698 705 void
699 706 delay(clock_t ticks)
700 707 {
701 708 poll(0, 0, ticks * (1000 / hz));
702 709 }
703 710
704 711 /*
705 712 * Find highest one bit set.
706 713 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
707 714 * High order bit is 31 (or 63 in _LP64 kernel).
708 715 */
709 716 int
710 717 highbit(ulong_t i)
711 718 {
712 719 register int h = 1;
713 720
714 721 if (i == 0)
715 722 return (0);
716 723 #ifdef _LP64
717 724 if (i & 0xffffffff00000000ul) {
718 725 h += 32; i >>= 32;
719 726 }
720 727 #endif
721 728 if (i & 0xffff0000) {
722 729 h += 16; i >>= 16;
723 730 }
724 731 if (i & 0xff00) {
725 732 h += 8; i >>= 8;
726 733 }
727 734 if (i & 0xf0) {
728 735 h += 4; i >>= 4;
729 736 }
730 737 if (i & 0xc) {
731 738 h += 2; i >>= 2;
732 739 }
733 740 if (i & 0x2) {
734 741 h += 1;
735 742 }
736 743 return (h);
737 744 }
738 745
739 746 static int random_fd = -1, urandom_fd = -1;
740 747
741 748 static int
742 749 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
743 750 {
744 751 size_t resid = len;
745 752 ssize_t bytes;
746 753
747 754 ASSERT(fd != -1);
748 755
749 756 while (resid != 0) {
750 757 bytes = read(fd, ptr, resid);
751 758 ASSERT3S(bytes, >=, 0);
752 759 ptr += bytes;
753 760 resid -= bytes;
754 761 }
755 762
756 763 return (0);
757 764 }
758 765
759 766 int
760 767 random_get_bytes(uint8_t *ptr, size_t len)
761 768 {
762 769 return (random_get_bytes_common(ptr, len, random_fd));
763 770 }
764 771
765 772 int
766 773 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
767 774 {
768 775 return (random_get_bytes_common(ptr, len, urandom_fd));
769 776 }
770 777
771 778 int
772 779 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
773 780 {
774 781 char *end;
775 782
776 783 *result = strtoul(hw_serial, &end, base);
777 784 if (*result == 0)
778 785 return (errno);
779 786 return (0);
780 787 }
781 788
782 789 int
783 790 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
784 791 {
785 792 char *end;
786 793
787 794 *result = strtoull(str, &end, base);
788 795 if (*result == 0)
789 796 return (errno);
790 797 return (0);
791 798 }
792 799
793 800 /*
794 801 * =========================================================================
795 802 * kernel emulation setup & teardown
796 803 * =========================================================================
797 804 */
798 805 static int
799 806 umem_out_of_memory(void)
800 807 {
801 808 char errmsg[] = "out of memory -- generating core dump\n";
802 809
803 810 write(fileno(stderr), errmsg, sizeof (errmsg));
804 811 abort();
805 812 return (0);
806 813 }
807 814
808 815 void
809 816 kernel_init(int mode)
810 817 {
811 818 umem_nofail_callback(umem_out_of_memory);
812 819
813 820 physmem = sysconf(_SC_PHYS_PAGES);
814 821
815 822 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
816 823 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
817 824
818 825 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
819 826 (mode & FWRITE) ? gethostid() : 0);
820 827
821 828 VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
822 829 VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
823 830
824 831 system_taskq_init();
825 832
826 833 spa_init(mode);
827 834 }
828 835
829 836 void
830 837 kernel_fini(void)
831 838 {
832 839 spa_fini();
833 840
834 841 system_taskq_fini();
835 842
836 843 close(random_fd);
837 844 close(urandom_fd);
838 845
839 846 random_fd = -1;
840 847 urandom_fd = -1;
841 848 }
842 849
843 850 int
844 851 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
845 852 {
846 853 int ret;
847 854 uLongf len = *dstlen;
848 855
849 856 if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK)
850 857 *dstlen = (size_t)len;
851 858
852 859 return (ret);
853 860 }
854 861
855 862 int
856 863 z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
857 864 int level)
858 865 {
859 866 int ret;
860 867 uLongf len = *dstlen;
861 868
862 869 if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK)
863 870 *dstlen = (size_t)len;
864 871
865 872 return (ret);
866 873 }
867 874
868 875 uid_t
869 876 crgetuid(cred_t *cr)
870 877 {
871 878 return (0);
872 879 }
873 880
874 881 gid_t
875 882 crgetgid(cred_t *cr)
876 883 {
877 884 return (0);
878 885 }
879 886
880 887 int
881 888 crgetngroups(cred_t *cr)
882 889 {
883 890 return (0);
884 891 }
885 892
886 893 gid_t *
887 894 crgetgroups(cred_t *cr)
888 895 {
889 896 return (NULL);
890 897 }
891 898
892 899 int
893 900 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
894 901 {
895 902 return (0);
896 903 }
897 904
898 905 int
899 906 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
900 907 {
901 908 return (0);
902 909 }
903 910
904 911 int
905 912 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
906 913 {
907 914 return (0);
908 915 }
909 916
910 917 ksiddomain_t *
911 918 ksid_lookupdomain(const char *dom)
912 919 {
913 920 ksiddomain_t *kd;
914 921
915 922 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
916 923 kd->kd_name = spa_strdup(dom);
917 924 return (kd);
918 925 }
919 926
920 927 void
921 928 ksiddomain_rele(ksiddomain_t *ksid)
922 929 {
923 930 spa_strfree(ksid->kd_name);
924 931 umem_free(ksid, sizeof (ksiddomain_t));
925 932 }
926 933
927 934 /*
928 935 * Do not change the length of the returned string; it must be freed
929 936 * with strfree().
930 937 */
931 938 char *
932 939 kmem_asprintf(const char *fmt, ...)
933 940 {
934 941 int size;
935 942 va_list adx;
936 943 char *buf;
937 944
938 945 va_start(adx, fmt);
939 946 size = vsnprintf(NULL, 0, fmt, adx) + 1;
940 947 va_end(adx);
941 948
942 949 buf = kmem_alloc(size, KM_SLEEP);
943 950
944 951 va_start(adx, fmt);
945 952 size = vsnprintf(buf, size, fmt, adx);
946 953 va_end(adx);
947 954
948 955 return (buf);
949 956 }
950 957
951 958 /* ARGSUSED */
952 959 int
953 960 zfs_onexit_fd_hold(int fd, minor_t *minorp)
954 961 {
955 962 *minorp = 0;
956 963 return (0);
957 964 }
958 965
959 966 /* ARGSUSED */
960 967 void
961 968 zfs_onexit_fd_rele(int fd)
962 969 {
963 970 }
964 971
965 972 /* ARGSUSED */
966 973 int
967 974 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
968 975 uint64_t *action_handle)
969 976 {
970 977 return (0);
971 978 }
972 979
973 980 /* ARGSUSED */
974 981 int
975 982 zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
976 983 {
977 984 return (0);
978 985 }
979 986
980 987 /* ARGSUSED */
981 988 int
982 989 zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
983 990 {
984 991 return (0);
985 992 }
↓ open down ↓ |
709 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX