Print this page
2619 asynchronous destruction of ZFS file systems
2747 SPA versioning with zfs feature flags
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <gwilson@delphix.com>
Reviewed by: Richard Lowe <richlowe@richlowe.net>
Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com>
Approved by: Dan McDonald <danmcd@nexenta.com>
| 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)
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 259 void
260 260 cv_wait(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 267 }
268 268
269 269 clock_t
270 270 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
271 271 {
272 272 int error;
273 273 timestruc_t ts;
274 274 clock_t delta;
275 275
276 276 top:
277 277 delta = abstime - ddi_get_lbolt();
278 278 if (delta <= 0)
279 279 return (-1);
280 280
281 281 ts.tv_sec = delta / hz;
282 282 ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
283 283
284 284 ASSERT(mutex_owner(mp) == curthread);
285 285 mp->m_owner = NULL;
286 286 error = cond_reltimedwait(cv, &mp->m_lock, &ts);
287 287 mp->m_owner = curthread;
288 288
289 289 if (error == ETIME)
290 290 return (-1);
291 291
292 292 if (error == EINTR)
293 293 goto top;
294 294
295 295 ASSERT(error == 0);
296 296
297 297 return (1);
298 298 }
299 299
300 300 void
301 301 cv_signal(kcondvar_t *cv)
302 302 {
303 303 VERIFY(cond_signal(cv) == 0);
304 304 }
305 305
306 306 void
307 307 cv_broadcast(kcondvar_t *cv)
308 308 {
309 309 VERIFY(cond_broadcast(cv) == 0);
310 310 }
311 311
312 312 /*
313 313 * =========================================================================
314 314 * vnode operations
315 315 * =========================================================================
316 316 */
317 317 /*
318 318 * Note: for the xxxat() versions of these functions, we assume that the
319 319 * starting vp is always rootdir (which is true for spa_directory.c, the only
320 320 * ZFS consumer of these interfaces). We assert this is true, and then emulate
321 321 * them by adding '/' in front of the path.
322 322 */
323 323
324 324 /*ARGSUSED*/
325 325 int
326 326 vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
327 327 {
328 328 int fd;
329 329 vnode_t *vp;
330 330 int old_umask;
331 331 char realpath[MAXPATHLEN];
332 332 struct stat64 st;
333 333
334 334 /*
335 335 * If we're accessing a real disk from userland, we need to use
336 336 * the character interface to avoid caching. This is particularly
337 337 * important if we're trying to look at a real in-kernel storage
338 338 * pool from userland, e.g. via zdb, because otherwise we won't
339 339 * see the changes occurring under the segmap cache.
340 340 * On the other hand, the stupid character device returns zero
341 341 * for its size. So -- gag -- we open the block device to get
342 342 * its size, and remember it for subsequent VOP_GETATTR().
343 343 */
344 344 if (strncmp(path, "/dev/", 5) == 0) {
345 345 char *dsk;
346 346 fd = open64(path, O_RDONLY);
347 347 if (fd == -1)
348 348 return (errno);
349 349 if (fstat64(fd, &st) == -1) {
350 350 close(fd);
351 351 return (errno);
352 352 }
353 353 close(fd);
354 354 (void) sprintf(realpath, "%s", path);
355 355 dsk = strstr(path, "/dsk/");
356 356 if (dsk != NULL)
357 357 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
358 358 dsk + 1);
359 359 } else {
360 360 (void) sprintf(realpath, "%s", path);
361 361 if (!(flags & FCREAT) && stat64(realpath, &st) == -1)
362 362 return (errno);
363 363 }
364 364
365 365 if (flags & FCREAT)
366 366 old_umask = umask(0);
367 367
368 368 /*
369 369 * The construct 'flags - FREAD' conveniently maps combinations of
370 370 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
371 371 */
372 372 fd = open64(realpath, flags - FREAD, mode);
373 373
374 374 if (flags & FCREAT)
375 375 (void) umask(old_umask);
376 376
377 377 if (fd == -1)
378 378 return (errno);
379 379
380 380 if (fstat64(fd, &st) == -1) {
381 381 close(fd);
382 382 return (errno);
383 383 }
384 384
385 385 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
386 386
387 387 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
388 388
389 389 vp->v_fd = fd;
390 390 vp->v_size = st.st_size;
391 391 vp->v_path = spa_strdup(path);
392 392
393 393 return (0);
394 394 }
395 395
396 396 /*ARGSUSED*/
397 397 int
398 398 vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
399 399 int x3, vnode_t *startvp, int fd)
400 400 {
401 401 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
402 402 int ret;
403 403
404 404 ASSERT(startvp == rootdir);
405 405 (void) sprintf(realpath, "/%s", path);
406 406
407 407 /* fd ignored for now, need if want to simulate nbmand support */
408 408 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
409 409
410 410 umem_free(realpath, strlen(path) + 2);
411 411
412 412 return (ret);
413 413 }
414 414
415 415 /*ARGSUSED*/
416 416 int
417 417 vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
418 418 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
|
↓ open down ↓ |
418 lines elided |
↑ open up ↑ |
419 419 {
420 420 ssize_t iolen, split;
421 421
422 422 if (uio == UIO_READ) {
423 423 iolen = pread64(vp->v_fd, addr, len, offset);
424 424 } else {
425 425 /*
426 426 * To simulate partial disk writes, we split writes into two
427 427 * system calls so that the process can be killed in between.
428 428 */
429 - split = (len > 0 ? rand() % len : 0);
429 + int sectors = len >> SPA_MINBLOCKSHIFT;
430 + split = (sectors > 0 ? rand() % sectors : 0) <<
431 + SPA_MINBLOCKSHIFT;
430 432 iolen = pwrite64(vp->v_fd, addr, split, offset);
431 433 iolen += pwrite64(vp->v_fd, (char *)addr + split,
432 434 len - split, offset + split);
433 435 }
434 436
435 437 if (iolen == -1)
436 438 return (errno);
437 439 if (residp)
438 440 *residp = len - iolen;
439 441 else if (iolen != len)
440 442 return (EIO);
441 443 return (0);
442 444 }
443 445
444 446 void
445 447 vn_close(vnode_t *vp)
446 448 {
447 449 close(vp->v_fd);
448 450 spa_strfree(vp->v_path);
449 451 umem_free(vp, sizeof (vnode_t));
450 452 }
451 453
452 454 /*
453 455 * At a minimum we need to update the size since vdev_reopen()
454 456 * will no longer call vn_openat().
455 457 */
456 458 int
457 459 fop_getattr(vnode_t *vp, vattr_t *vap)
458 460 {
459 461 struct stat64 st;
460 462
461 463 if (fstat64(vp->v_fd, &st) == -1) {
462 464 close(vp->v_fd);
463 465 return (errno);
464 466 }
465 467
466 468 vap->va_size = st.st_size;
467 469 return (0);
468 470 }
469 471
470 472 #ifdef ZFS_DEBUG
471 473
472 474 /*
473 475 * =========================================================================
474 476 * Figure out which debugging statements to print
475 477 * =========================================================================
476 478 */
477 479
478 480 static char *dprintf_string;
479 481 static int dprintf_print_all;
480 482
481 483 int
482 484 dprintf_find_string(const char *string)
483 485 {
484 486 char *tmp_str = dprintf_string;
485 487 int len = strlen(string);
486 488
487 489 /*
488 490 * Find out if this is a string we want to print.
489 491 * String format: file1.c,function_name1,file2.c,file3.c
490 492 */
491 493
492 494 while (tmp_str != NULL) {
493 495 if (strncmp(tmp_str, string, len) == 0 &&
494 496 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
495 497 return (1);
496 498 tmp_str = strchr(tmp_str, ',');
497 499 if (tmp_str != NULL)
498 500 tmp_str++; /* Get rid of , */
499 501 }
500 502 return (0);
501 503 }
502 504
503 505 void
504 506 dprintf_setup(int *argc, char **argv)
505 507 {
506 508 int i, j;
507 509
508 510 /*
509 511 * Debugging can be specified two ways: by setting the
510 512 * environment variable ZFS_DEBUG, or by including a
511 513 * "debug=..." argument on the command line. The command
512 514 * line setting overrides the environment variable.
513 515 */
514 516
515 517 for (i = 1; i < *argc; i++) {
516 518 int len = strlen("debug=");
517 519 /* First look for a command line argument */
518 520 if (strncmp("debug=", argv[i], len) == 0) {
519 521 dprintf_string = argv[i] + len;
520 522 /* Remove from args */
521 523 for (j = i; j < *argc; j++)
522 524 argv[j] = argv[j+1];
523 525 argv[j] = NULL;
524 526 (*argc)--;
525 527 }
526 528 }
527 529
528 530 if (dprintf_string == NULL) {
529 531 /* Look for ZFS_DEBUG environment variable */
530 532 dprintf_string = getenv("ZFS_DEBUG");
531 533 }
532 534
533 535 /*
534 536 * Are we just turning on all debugging?
535 537 */
536 538 if (dprintf_find_string("on"))
537 539 dprintf_print_all = 1;
538 540 }
539 541
540 542 /*
541 543 * =========================================================================
542 544 * debug printfs
543 545 * =========================================================================
544 546 */
545 547 void
546 548 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
547 549 {
548 550 const char *newfile;
549 551 va_list adx;
550 552
551 553 /*
552 554 * Get rid of annoying "../common/" prefix to filename.
553 555 */
554 556 newfile = strrchr(file, '/');
555 557 if (newfile != NULL) {
556 558 newfile = newfile + 1; /* Get rid of leading / */
557 559 } else {
558 560 newfile = file;
559 561 }
560 562
561 563 if (dprintf_print_all ||
562 564 dprintf_find_string(newfile) ||
563 565 dprintf_find_string(func)) {
564 566 /* Print out just the function name if requested */
565 567 flockfile(stdout);
566 568 if (dprintf_find_string("pid"))
567 569 (void) printf("%d ", getpid());
568 570 if (dprintf_find_string("tid"))
569 571 (void) printf("%u ", thr_self());
570 572 if (dprintf_find_string("cpu"))
571 573 (void) printf("%u ", getcpuid());
572 574 if (dprintf_find_string("time"))
573 575 (void) printf("%llu ", gethrtime());
574 576 if (dprintf_find_string("long"))
575 577 (void) printf("%s, line %d: ", newfile, line);
576 578 (void) printf("%s: ", func);
577 579 va_start(adx, fmt);
578 580 (void) vprintf(fmt, adx);
579 581 va_end(adx);
580 582 funlockfile(stdout);
581 583 }
582 584 }
583 585
584 586 #endif /* ZFS_DEBUG */
585 587
586 588 /*
587 589 * =========================================================================
588 590 * cmn_err() and panic()
589 591 * =========================================================================
590 592 */
591 593 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
592 594 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
593 595
594 596 void
595 597 vpanic(const char *fmt, va_list adx)
596 598 {
597 599 (void) fprintf(stderr, "error: ");
598 600 (void) vfprintf(stderr, fmt, adx);
599 601 (void) fprintf(stderr, "\n");
600 602
601 603 abort(); /* think of it as a "user-level crash dump" */
602 604 }
603 605
604 606 void
605 607 panic(const char *fmt, ...)
606 608 {
607 609 va_list adx;
608 610
609 611 va_start(adx, fmt);
610 612 vpanic(fmt, adx);
611 613 va_end(adx);
612 614 }
613 615
614 616 void
615 617 vcmn_err(int ce, const char *fmt, va_list adx)
616 618 {
617 619 if (ce == CE_PANIC)
618 620 vpanic(fmt, adx);
619 621 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
620 622 (void) fprintf(stderr, "%s", ce_prefix[ce]);
621 623 (void) vfprintf(stderr, fmt, adx);
622 624 (void) fprintf(stderr, "%s", ce_suffix[ce]);
623 625 }
624 626 }
625 627
626 628 /*PRINTFLIKE2*/
627 629 void
628 630 cmn_err(int ce, const char *fmt, ...)
629 631 {
630 632 va_list adx;
631 633
632 634 va_start(adx, fmt);
633 635 vcmn_err(ce, fmt, adx);
634 636 va_end(adx);
635 637 }
636 638
637 639 /*
638 640 * =========================================================================
639 641 * kobj interfaces
640 642 * =========================================================================
641 643 */
642 644 struct _buf *
643 645 kobj_open_file(char *name)
644 646 {
645 647 struct _buf *file;
646 648 vnode_t *vp;
647 649
648 650 /* set vp as the _fd field of the file */
649 651 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
650 652 -1) != 0)
651 653 return ((void *)-1UL);
652 654
653 655 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
654 656 file->_fd = (intptr_t)vp;
655 657 return (file);
656 658 }
657 659
658 660 int
659 661 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
660 662 {
661 663 ssize_t resid;
662 664
663 665 vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
664 666 UIO_SYSSPACE, 0, 0, 0, &resid);
665 667
666 668 return (size - resid);
667 669 }
668 670
669 671 void
670 672 kobj_close_file(struct _buf *file)
671 673 {
672 674 vn_close((vnode_t *)file->_fd);
673 675 umem_free(file, sizeof (struct _buf));
674 676 }
675 677
676 678 int
677 679 kobj_get_filesize(struct _buf *file, uint64_t *size)
678 680 {
679 681 struct stat64 st;
680 682 vnode_t *vp = (vnode_t *)file->_fd;
681 683
682 684 if (fstat64(vp->v_fd, &st) == -1) {
683 685 vn_close(vp);
684 686 return (errno);
685 687 }
686 688 *size = st.st_size;
687 689 return (0);
688 690 }
689 691
690 692 /*
691 693 * =========================================================================
692 694 * misc routines
693 695 * =========================================================================
694 696 */
695 697
696 698 void
697 699 delay(clock_t ticks)
698 700 {
699 701 poll(0, 0, ticks * (1000 / hz));
700 702 }
701 703
702 704 /*
703 705 * Find highest one bit set.
704 706 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
705 707 * High order bit is 31 (or 63 in _LP64 kernel).
706 708 */
707 709 int
708 710 highbit(ulong_t i)
709 711 {
710 712 register int h = 1;
711 713
712 714 if (i == 0)
713 715 return (0);
714 716 #ifdef _LP64
715 717 if (i & 0xffffffff00000000ul) {
716 718 h += 32; i >>= 32;
717 719 }
718 720 #endif
719 721 if (i & 0xffff0000) {
720 722 h += 16; i >>= 16;
721 723 }
722 724 if (i & 0xff00) {
723 725 h += 8; i >>= 8;
724 726 }
725 727 if (i & 0xf0) {
726 728 h += 4; i >>= 4;
727 729 }
728 730 if (i & 0xc) {
729 731 h += 2; i >>= 2;
730 732 }
731 733 if (i & 0x2) {
732 734 h += 1;
733 735 }
734 736 return (h);
735 737 }
736 738
737 739 static int random_fd = -1, urandom_fd = -1;
738 740
739 741 static int
740 742 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
741 743 {
742 744 size_t resid = len;
743 745 ssize_t bytes;
744 746
745 747 ASSERT(fd != -1);
746 748
747 749 while (resid != 0) {
748 750 bytes = read(fd, ptr, resid);
749 751 ASSERT3S(bytes, >=, 0);
750 752 ptr += bytes;
751 753 resid -= bytes;
752 754 }
753 755
754 756 return (0);
755 757 }
756 758
757 759 int
758 760 random_get_bytes(uint8_t *ptr, size_t len)
759 761 {
760 762 return (random_get_bytes_common(ptr, len, random_fd));
761 763 }
762 764
763 765 int
764 766 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
765 767 {
766 768 return (random_get_bytes_common(ptr, len, urandom_fd));
767 769 }
768 770
769 771 int
770 772 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
771 773 {
772 774 char *end;
773 775
774 776 *result = strtoul(hw_serial, &end, base);
775 777 if (*result == 0)
776 778 return (errno);
777 779 return (0);
778 780 }
779 781
780 782 int
781 783 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
782 784 {
783 785 char *end;
784 786
785 787 *result = strtoull(str, &end, base);
786 788 if (*result == 0)
787 789 return (errno);
788 790 return (0);
789 791 }
790 792
791 793 /*
792 794 * =========================================================================
793 795 * kernel emulation setup & teardown
794 796 * =========================================================================
795 797 */
796 798 static int
797 799 umem_out_of_memory(void)
798 800 {
799 801 char errmsg[] = "out of memory -- generating core dump\n";
800 802
801 803 write(fileno(stderr), errmsg, sizeof (errmsg));
802 804 abort();
803 805 return (0);
804 806 }
805 807
806 808 void
807 809 kernel_init(int mode)
808 810 {
809 811 umem_nofail_callback(umem_out_of_memory);
810 812
811 813 physmem = sysconf(_SC_PHYS_PAGES);
812 814
813 815 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
814 816 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
815 817
816 818 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
817 819 (mode & FWRITE) ? gethostid() : 0);
818 820
819 821 VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
820 822 VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
821 823
822 824 system_taskq_init();
823 825
824 826 spa_init(mode);
825 827 }
826 828
827 829 void
828 830 kernel_fini(void)
829 831 {
830 832 spa_fini();
831 833
832 834 system_taskq_fini();
833 835
834 836 close(random_fd);
835 837 close(urandom_fd);
836 838
837 839 random_fd = -1;
838 840 urandom_fd = -1;
839 841 }
840 842
841 843 int
842 844 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
843 845 {
844 846 int ret;
845 847 uLongf len = *dstlen;
846 848
847 849 if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK)
848 850 *dstlen = (size_t)len;
849 851
850 852 return (ret);
851 853 }
852 854
853 855 int
854 856 z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
855 857 int level)
856 858 {
857 859 int ret;
858 860 uLongf len = *dstlen;
859 861
860 862 if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK)
861 863 *dstlen = (size_t)len;
862 864
863 865 return (ret);
864 866 }
865 867
866 868 uid_t
867 869 crgetuid(cred_t *cr)
868 870 {
869 871 return (0);
870 872 }
871 873
872 874 gid_t
873 875 crgetgid(cred_t *cr)
874 876 {
875 877 return (0);
876 878 }
877 879
878 880 int
879 881 crgetngroups(cred_t *cr)
880 882 {
881 883 return (0);
882 884 }
883 885
884 886 gid_t *
885 887 crgetgroups(cred_t *cr)
886 888 {
887 889 return (NULL);
888 890 }
889 891
890 892 int
891 893 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
892 894 {
893 895 return (0);
894 896 }
895 897
896 898 int
897 899 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
898 900 {
899 901 return (0);
900 902 }
901 903
902 904 int
903 905 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
904 906 {
905 907 return (0);
906 908 }
907 909
908 910 ksiddomain_t *
909 911 ksid_lookupdomain(const char *dom)
910 912 {
911 913 ksiddomain_t *kd;
912 914
913 915 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
914 916 kd->kd_name = spa_strdup(dom);
915 917 return (kd);
916 918 }
917 919
918 920 void
919 921 ksiddomain_rele(ksiddomain_t *ksid)
920 922 {
921 923 spa_strfree(ksid->kd_name);
922 924 umem_free(ksid, sizeof (ksiddomain_t));
923 925 }
924 926
925 927 /*
926 928 * Do not change the length of the returned string; it must be freed
927 929 * with strfree().
928 930 */
929 931 char *
930 932 kmem_asprintf(const char *fmt, ...)
931 933 {
932 934 int size;
933 935 va_list adx;
934 936 char *buf;
935 937
936 938 va_start(adx, fmt);
937 939 size = vsnprintf(NULL, 0, fmt, adx) + 1;
938 940 va_end(adx);
939 941
940 942 buf = kmem_alloc(size, KM_SLEEP);
941 943
942 944 va_start(adx, fmt);
943 945 size = vsnprintf(buf, size, fmt, adx);
944 946 va_end(adx);
945 947
946 948 return (buf);
947 949 }
948 950
949 951 /* ARGSUSED */
950 952 int
951 953 zfs_onexit_fd_hold(int fd, minor_t *minorp)
952 954 {
953 955 *minorp = 0;
954 956 return (0);
955 957 }
956 958
957 959 /* ARGSUSED */
958 960 void
959 961 zfs_onexit_fd_rele(int fd)
960 962 {
961 963 }
962 964
963 965 /* ARGSUSED */
964 966 int
965 967 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
966 968 uint64_t *action_handle)
967 969 {
968 970 return (0);
969 971 }
970 972
971 973 /* ARGSUSED */
972 974 int
973 975 zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
974 976 {
975 977 return (0);
976 978 }
977 979
978 980 /* ARGSUSED */
979 981 int
980 982 zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
981 983 {
982 984 return (0);
983 985 }
|
↓ open down ↓ |
544 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX