git: 14c5cf3a16c9 - main - makefs/zfs: Avoid generating a GUID of zero
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 26 May 2023 20:06:54 UTC
The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=14c5cf3a16c9c21b49e0ec0a109467d81e5a47ff commit 14c5cf3a16c9c21b49e0ec0a109467d81e5a47ff Author: Mark Johnston <markj@FreeBSD.org> AuthorDate: 2023-05-26 19:14:00 +0000 Commit: Mark Johnston <markj@FreeBSD.org> CommitDate: 2023-05-26 19:38:08 +0000 makefs/zfs: Avoid generating a GUID of zero --- usr.sbin/makefs/zfs.c | 20 ++++++++++++++++++-- usr.sbin/makefs/zfs/dsl.c | 2 +- usr.sbin/makefs/zfs/zfs.h | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/usr.sbin/makefs/zfs.c b/usr.sbin/makefs/zfs.c index 7c2805ae5bbb..4d45926fb33f 100644 --- a/usr.sbin/makefs/zfs.c +++ b/usr.sbin/makefs/zfs.c @@ -274,6 +274,22 @@ nvlist_copy(const nvlist_t *nvl, char *buf, size_t sz) memcpy(buf + sizeof(nvl->nv_header), nvl->nv_data, nvl->nv_size); } +/* + * Avoid returning a GUID of 0, just to avoid the possibility that something + * will interpret that as meaning that the GUID is uninitialized. + */ +uint64_t +randomguid(void) +{ + uint64_t ret; + + do { + ret = ((uint64_t)random() << 32) | random(); + } while (ret == 0); + + return (ret); +} + static nvlist_t * pool_config_nvcreate(zfs_opt_t *zfs) { @@ -529,8 +545,8 @@ pool_init(zfs_opt_t *zfs) { uint64_t dnid; - zfs->poolguid = ((uint64_t)random() << 32) | random(); - zfs->vdevguid = ((uint64_t)random() << 32) | random(); + zfs->poolguid = randomguid(); + zfs->vdevguid = randomguid(); zfs->mos = objset_alloc(zfs, DMU_OST_META); diff --git a/usr.sbin/makefs/zfs/dsl.c b/usr.sbin/makefs/zfs/dsl.c index f6b7dc0ede17..a9ab93a90c38 100644 --- a/usr.sbin/makefs/zfs/dsl.c +++ b/usr.sbin/makefs/zfs/dsl.c @@ -602,7 +602,7 @@ dsl_dataset_alloc(zfs_opt_t *zfs, zfs_dsl_dir_t *dir) ds->phys->ds_creation_txg = TXG - 1; if (ds != zfs->snapds) ds->phys->ds_prev_snap_txg = TXG - 1; - ds->phys->ds_guid = ((uint64_t)random() << 32) | random(); + ds->phys->ds_guid = randomguid(); ds->dir = dir; return (ds); diff --git a/usr.sbin/makefs/zfs/zfs.h b/usr.sbin/makefs/zfs/zfs.h index b585955f7a1d..193ca1248d89 100644 --- a/usr.sbin/makefs/zfs/zfs.h +++ b/usr.sbin/makefs/zfs/zfs.h @@ -168,5 +168,6 @@ struct dnode_cursor *dnode_cursor_init(zfs_opt_t *, zfs_objset_t *, dnode_phys_t *, off_t, off_t); blkptr_t *dnode_cursor_next(zfs_opt_t *, struct dnode_cursor *, off_t); void dnode_cursor_finish(zfs_opt_t *, struct dnode_cursor *); +uint64_t randomguid(void); #endif /* !_MAKEFS_ZFS_H_ */