89 #include <sys/zap.h>
90 #include <sys/dmu_objset.h>
91 #include <sys/poll.h>
92 #include <sys/stat.h>
93 #include <sys/time.h>
94 #include <sys/wait.h>
95 #include <sys/mman.h>
96 #include <sys/resource.h>
97 #include <sys/zio.h>
98 #include <sys/zil.h>
99 #include <sys/zil_impl.h>
100 #include <sys/vdev_impl.h>
101 #include <sys/vdev_file.h>
102 #include <sys/spa_impl.h>
103 #include <sys/metaslab_impl.h>
104 #include <sys/dsl_prop.h>
105 #include <sys/dsl_dataset.h>
106 #include <sys/dsl_scan.h>
107 #include <sys/zio_checksum.h>
108 #include <sys/refcount.h>
109 #include <stdio.h>
110 #include <stdio_ext.h>
111 #include <stdlib.h>
112 #include <unistd.h>
113 #include <signal.h>
114 #include <umem.h>
115 #include <dlfcn.h>
116 #include <ctype.h>
117 #include <math.h>
118 #include <sys/fs/zfs.h>
119 #include <libnvpair.h>
120
121 #define ZTEST_FD_DATA 3
122 #define ZTEST_FD_RAND 4
123
124 typedef struct ztest_shared_hdr {
125 uint64_t zh_hdr_size;
126 uint64_t zh_opts_size;
127 uint64_t zh_size;
128 uint64_t zh_stats_size;
5555 h -= d * 24;
5556
5557 timebuf[0] = '\0';
5558
5559 if (d)
5560 (void) sprintf(timebuf,
5561 "%llud%02lluh%02llum%02llus", d, h, m, s);
5562 else if (h)
5563 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5564 else if (m)
5565 (void) sprintf(timebuf, "%llum%02llus", m, s);
5566 else
5567 (void) sprintf(timebuf, "%llus", s);
5568 }
5569
5570 static nvlist_t *
5571 make_random_props()
5572 {
5573 nvlist_t *props;
5574
5575 if (ztest_random(2) == 0)
5576 return (NULL);
5577
5578 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5579 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5580
5581 return (props);
5582 }
5583
5584 /*
5585 * Create a storage pool with the given name and initial vdev size.
5586 * Then test spa_freeze() functionality.
5587 */
5588 static void
5589 ztest_init(ztest_shared_t *zs)
5590 {
5591 spa_t *spa;
5592 nvlist_t *nvroot, *props;
5593
5594 VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
5595 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5596
5597 kernel_init(FREAD | FWRITE);
5598
5599 /*
5600 * Create the storage pool.
5601 */
5602 (void) spa_destroy(ztest_opts.zo_pool);
5603 ztest_shared->zs_vdev_next_leaf = 0;
5604 zs->zs_splits = 0;
5605 zs->zs_mirrors = ztest_opts.zo_mirrors;
5606 nvroot = make_vdev_root(NULL, NULL, ztest_opts.zo_vdev_size, 0,
5607 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
5608 props = make_random_props();
5609 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props,
5610 NULL, NULL));
5611 nvlist_free(nvroot);
5612
5613 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5614 zs->zs_metaslab_sz =
5615 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5616 spa_close(spa, FTAG);
5617
5618 kernel_fini();
5619
5620 ztest_run_zdb(ztest_opts.zo_pool);
5621
5622 ztest_freeze();
5623
5624 ztest_run_zdb(ztest_opts.zo_pool);
5625
5626 (void) rwlock_destroy(&ztest_name_lock);
5627 (void) _mutex_destroy(&ztest_vdev_lock);
5628 }
5629
5630 static void
5631 setup_fds(void)
5632 {
5633 int fd;
5634
5635 char *tmp = tempnam(NULL, NULL);
5636 fd = open(tmp, O_RDWR | O_CREAT, 0700);
5637 ASSERT3U(fd, ==, ZTEST_FD_DATA);
5638 (void) unlink(tmp);
5639 free(tmp);
5640
5641 fd = open("/dev/urandom", O_RDONLY);
5642 ASSERT3U(fd, ==, ZTEST_FD_RAND);
5643 }
5644
5645 static void
5646 setup_hdr(void)
5647 {
5648 ztest_shared_hdr_t *hdr;
5649
5650 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5651 PROT_READ | PROT_WRITE, MAP_SHARED, ZTEST_FD_DATA, 0);
5652 ASSERT(hdr != MAP_FAILED);
5653
5654 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
5655 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
5656 hdr->zh_size = sizeof (ztest_shared_t);
5657 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
5658 hdr->zh_stats_count = ZTEST_FUNCS;
5659 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
5660 hdr->zh_ds_count = ztest_opts.zo_datasets;
5661
5662 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5663 }
5664
5665 static void
5666 setup_data(void)
5667 {
5668 int size, offset;
5669 ztest_shared_hdr_t *hdr;
5670 uint8_t *buf;
5671
5672 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5673 PROT_READ, MAP_SHARED, ZTEST_FD_DATA, 0);
5674 ASSERT(hdr != MAP_FAILED);
5675
5676 size = hdr->zh_hdr_size;
5677 size += hdr->zh_opts_size;
5678 size += hdr->zh_size;
5679 size += hdr->zh_stats_size * hdr->zh_stats_count;
5680 size += hdr->zh_ds_size * hdr->zh_ds_count;
5681
5682 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5683 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
5684 PROT_READ | PROT_WRITE, MAP_SHARED, ZTEST_FD_DATA, 0);
5685 ASSERT(hdr != MAP_FAILED);
5686 buf = (uint8_t *)hdr;
5687
5688 offset = hdr->zh_hdr_size;
5689 ztest_shared_opts = (void *)&buf[offset];
5690 offset += hdr->zh_opts_size;
5691 ztest_shared = (void *)&buf[offset];
5692 offset += hdr->zh_size;
5693 ztest_shared_callstate = (void *)&buf[offset];
5694 offset += hdr->zh_stats_size * hdr->zh_stats_count;
5695 ztest_shared_ds = (void *)&buf[offset];
5696 }
5697
5698 static boolean_t
5699 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5700 {
|
89 #include <sys/zap.h>
90 #include <sys/dmu_objset.h>
91 #include <sys/poll.h>
92 #include <sys/stat.h>
93 #include <sys/time.h>
94 #include <sys/wait.h>
95 #include <sys/mman.h>
96 #include <sys/resource.h>
97 #include <sys/zio.h>
98 #include <sys/zil.h>
99 #include <sys/zil_impl.h>
100 #include <sys/vdev_impl.h>
101 #include <sys/vdev_file.h>
102 #include <sys/spa_impl.h>
103 #include <sys/metaslab_impl.h>
104 #include <sys/dsl_prop.h>
105 #include <sys/dsl_dataset.h>
106 #include <sys/dsl_scan.h>
107 #include <sys/zio_checksum.h>
108 #include <sys/refcount.h>
109 #include <sys/zfeature.h>
110 #include <stdio.h>
111 #include <stdio_ext.h>
112 #include <stdlib.h>
113 #include <unistd.h>
114 #include <signal.h>
115 #include <umem.h>
116 #include <dlfcn.h>
117 #include <ctype.h>
118 #include <math.h>
119 #include <sys/fs/zfs.h>
120 #include <libnvpair.h>
121
122 #define ZTEST_FD_DATA 3
123 #define ZTEST_FD_RAND 4
124
125 typedef struct ztest_shared_hdr {
126 uint64_t zh_hdr_size;
127 uint64_t zh_opts_size;
128 uint64_t zh_size;
129 uint64_t zh_stats_size;
5556 h -= d * 24;
5557
5558 timebuf[0] = '\0';
5559
5560 if (d)
5561 (void) sprintf(timebuf,
5562 "%llud%02lluh%02llum%02llus", d, h, m, s);
5563 else if (h)
5564 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5565 else if (m)
5566 (void) sprintf(timebuf, "%llum%02llus", m, s);
5567 else
5568 (void) sprintf(timebuf, "%llus", s);
5569 }
5570
5571 static nvlist_t *
5572 make_random_props()
5573 {
5574 nvlist_t *props;
5575
5576 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5577 if (ztest_random(2) == 0)
5578 return (props);
5579 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5580
5581 return (props);
5582 }
5583
5584 /*
5585 * Create a storage pool with the given name and initial vdev size.
5586 * Then test spa_freeze() functionality.
5587 */
5588 static void
5589 ztest_init(ztest_shared_t *zs)
5590 {
5591 spa_t *spa;
5592 nvlist_t *nvroot, *props;
5593
5594 VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
5595 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5596
5597 kernel_init(FREAD | FWRITE);
5598
5599 /*
5600 * Create the storage pool.
5601 */
5602 (void) spa_destroy(ztest_opts.zo_pool);
5603 ztest_shared->zs_vdev_next_leaf = 0;
5604 zs->zs_splits = 0;
5605 zs->zs_mirrors = ztest_opts.zo_mirrors;
5606 nvroot = make_vdev_root(NULL, NULL, ztest_opts.zo_vdev_size, 0,
5607 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
5608 props = make_random_props();
5609 for (int i = 0; i < SPA_FEATURES; i++) {
5610 char buf[1024];
5611 (void) snprintf(buf, sizeof (buf), "feature@%s",
5612 spa_feature_table[i].fi_uname);
5613 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
5614 }
5615 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props,
5616 NULL, NULL));
5617 nvlist_free(nvroot);
5618
5619 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5620 zs->zs_metaslab_sz =
5621 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5622
5623 spa_close(spa, FTAG);
5624
5625 kernel_fini();
5626
5627 ztest_run_zdb(ztest_opts.zo_pool);
5628
5629 ztest_freeze();
5630
5631 ztest_run_zdb(ztest_opts.zo_pool);
5632
5633 (void) rwlock_destroy(&ztest_name_lock);
5634 (void) _mutex_destroy(&ztest_vdev_lock);
5635 }
5636
5637 static void
5638 setup_fds(void)
5639 {
5640 int fd;
5641
5642 char *tmp = tempnam(NULL, NULL);
5643 fd = open(tmp, O_RDWR | O_CREAT, 0700);
5644 ASSERT3U(fd, ==, ZTEST_FD_DATA);
5645 (void) unlink(tmp);
5646 free(tmp);
5647
5648 fd = open("/dev/urandom", O_RDONLY);
5649 ASSERT3U(fd, ==, ZTEST_FD_RAND);
5650 }
5651
5652 static int
5653 shared_data_size(ztest_shared_hdr_t *hdr)
5654 {
5655 int size;
5656
5657 size = hdr->zh_hdr_size;
5658 size += hdr->zh_opts_size;
5659 size += hdr->zh_size;
5660 size += hdr->zh_stats_size * hdr->zh_stats_count;
5661 size += hdr->zh_ds_size * hdr->zh_ds_count;
5662
5663 return (size);
5664 }
5665
5666 static void
5667 setup_hdr(void)
5668 {
5669 int size;
5670 ztest_shared_hdr_t *hdr;
5671
5672 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5673 PROT_READ | PROT_WRITE, MAP_SHARED, ZTEST_FD_DATA, 0);
5674 ASSERT(hdr != MAP_FAILED);
5675
5676 VERIFY3U(0, ==, ftruncate(ZTEST_FD_DATA, sizeof (ztest_shared_hdr_t)));
5677
5678 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
5679 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
5680 hdr->zh_size = sizeof (ztest_shared_t);
5681 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
5682 hdr->zh_stats_count = ZTEST_FUNCS;
5683 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
5684 hdr->zh_ds_count = ztest_opts.zo_datasets;
5685
5686 size = shared_data_size(hdr);
5687 VERIFY3U(0, ==, ftruncate(ZTEST_FD_DATA, size));
5688
5689 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5690 }
5691
5692 static void
5693 setup_data(void)
5694 {
5695 int size, offset;
5696 ztest_shared_hdr_t *hdr;
5697 uint8_t *buf;
5698
5699 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5700 PROT_READ, MAP_SHARED, ZTEST_FD_DATA, 0);
5701 ASSERT(hdr != MAP_FAILED);
5702
5703 size = shared_data_size(hdr);
5704
5705 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5706 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
5707 PROT_READ | PROT_WRITE, MAP_SHARED, ZTEST_FD_DATA, 0);
5708 ASSERT(hdr != MAP_FAILED);
5709 buf = (uint8_t *)hdr;
5710
5711 offset = hdr->zh_hdr_size;
5712 ztest_shared_opts = (void *)&buf[offset];
5713 offset += hdr->zh_opts_size;
5714 ztest_shared = (void *)&buf[offset];
5715 offset += hdr->zh_size;
5716 ztest_shared_callstate = (void *)&buf[offset];
5717 offset += hdr->zh_stats_size * hdr->zh_stats_count;
5718 ztest_shared_ds = (void *)&buf[offset];
5719 }
5720
5721 static boolean_t
5722 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5723 {
|