Skip to content

Commit a91ab4a

Browse files
committed
zstream: consolidate shared code
zstream currently contains three identical copies of dump_record(), which appear to all be drawn from libzfs_sendrecv.c. The original is marked internal. This PR adds zstream_util.[hc] and puts the shared code there along with a couple of other items in common. No functional changes. Signed-off-by: Garth Snyder <garth@garthsnyder.com>
1 parent 1e1d64d commit a91ab4a

File tree

8 files changed

+162
-119
lines changed

8 files changed

+162
-119
lines changed

cmd/zstream/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ zstream_SOURCES = \
1111
%D%/zstream_dump.c \
1212
%D%/zstream_recompress.c \
1313
%D%/zstream_redup.c \
14-
%D%/zstream_token.c
14+
%D%/zstream_token.c \
15+
%D%/zstream_util.c \
16+
%D%/zstream_util.h
1517

1618
zstream_LDADD = \
1719
libzfs.la \

cmd/zstream/zstream.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
extern "C" {
2626
#endif
2727

28-
extern void *safe_calloc(size_t n);
29-
extern int sfread(void *buf, size_t size, FILE *fp);
30-
extern void *safe_malloc(size_t size);
3128
extern int zstream_do_redup(int, char *[]);
3229
extern int zstream_do_dump(int, char *[]);
3330
extern int zstream_do_decompress(int argc, char *argv[]);

cmd/zstream/zstream_decompress.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,7 @@
3737
#include <sys/zstd/zstd.h>
3838
#include "zfs_fletcher.h"
3939
#include "zstream.h"
40-
41-
static int
42-
dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
43-
zio_cksum_t *zc, int outfd)
44-
{
45-
assert(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum)
46-
== sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
47-
fletcher_4_incremental_native(drr,
48-
offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
49-
if (drr->drr_type != DRR_BEGIN) {
50-
assert(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
51-
drr_checksum.drr_checksum));
52-
drr->drr_u.drr_checksum.drr_checksum = *zc;
53-
}
54-
fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
55-
sizeof (zio_cksum_t), zc);
56-
if (write(outfd, drr, sizeof (*drr)) == -1)
57-
return (errno);
58-
if (payload_len != 0) {
59-
fletcher_4_incremental_native(payload, payload_len, zc);
60-
if (write(outfd, payload, payload_len) == -1)
61-
return (errno);
62-
}
63-
return (0);
64-
}
40+
#include "zstream_util.h"
6541

6642
int
6743
zstream_do_decompress(int argc, char *argv[])

cmd/zstream/zstream_dump.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <sys/zio.h>
4545
#include <zfs_fletcher.h>
4646
#include "zstream.h"
47+
#include "zstream_util.h"
4748

4849
/*
4950
* If dump mode is enabled, the number of bytes to print per line
@@ -60,18 +61,6 @@ static FILE *send_stream = 0;
6061
static boolean_t do_byteswap = B_FALSE;
6162
static boolean_t do_cksum = B_TRUE;
6263

63-
void *
64-
safe_malloc(size_t size)
65-
{
66-
void *rv = malloc(size);
67-
if (rv == NULL) {
68-
(void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n",
69-
size);
70-
abort();
71-
}
72-
return (rv);
73-
}
74-
7564
/*
7665
* ssread - send stream read.
7766
*

cmd/zstream/zstream_recompress.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,7 @@
3737
#include <sys/zstd/zstd.h>
3838
#include "zfs_fletcher.h"
3939
#include "zstream.h"
40-
41-
static int
42-
dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
43-
zio_cksum_t *zc, int outfd)
44-
{
45-
assert(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum)
46-
== sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
47-
fletcher_4_incremental_native(drr,
48-
offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
49-
if (drr->drr_type != DRR_BEGIN) {
50-
assert(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
51-
drr_checksum.drr_checksum));
52-
drr->drr_u.drr_checksum.drr_checksum = *zc;
53-
}
54-
fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
55-
sizeof (zio_cksum_t), zc);
56-
if (write(outfd, drr, sizeof (*drr)) == -1)
57-
return (errno);
58-
if (payload_len != 0) {
59-
fletcher_4_incremental_native(payload, payload_len, zc);
60-
if (write(outfd, payload, payload_len) == -1)
61-
return (errno);
62-
}
63-
return (0);
64-
}
40+
#include "zstream_util.h"
6541

6642
int
6743
zstream_do_recompress(int argc, char *argv[])

cmd/zstream/zstream_redup.c

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <sys/zio_checksum.h>
3838
#include "zfs_fletcher.h"
3939
#include "zstream.h"
40+
#include "zstream_util.h"
4041

4142

4243
#define MAX_RDT_PHYSMEM_PERCENT 20
@@ -57,34 +58,6 @@ typedef struct redup_table {
5758
int numhashbits;
5859
} redup_table_t;
5960

60-
void *
61-
safe_calloc(size_t n)
62-
{
63-
void *rv = calloc(1, n);
64-
if (rv == NULL) {
65-
fprintf(stderr,
66-
"Error: could not allocate %u bytes of memory\n",
67-
(int)n);
68-
exit(1);
69-
}
70-
return (rv);
71-
}
72-
73-
/*
74-
* Safe version of fread(), exits on error.
75-
*/
76-
int
77-
sfread(void *buf, size_t size, FILE *fp)
78-
{
79-
int rv = fread(buf, size, 1, fp);
80-
if (rv == 0 && ferror(fp)) {
81-
(void) fprintf(stderr, "Error while reading file: %s\n",
82-
strerror(errno));
83-
exit(1);
84-
}
85-
return (rv);
86-
}
87-
8861
/*
8962
* Safe version of pread(), exits on error.
9063
*/
@@ -104,31 +77,6 @@ spread(int fd, void *buf, size_t count, off_t offset)
10477
}
10578
}
10679

107-
static int
108-
dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
109-
zio_cksum_t *zc, int outfd)
110-
{
111-
assert(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum)
112-
== sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
113-
fletcher_4_incremental_native(drr,
114-
offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
115-
if (drr->drr_type != DRR_BEGIN) {
116-
assert(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
117-
drr_checksum.drr_checksum));
118-
drr->drr_u.drr_checksum.drr_checksum = *zc;
119-
}
120-
fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
121-
sizeof (zio_cksum_t), zc);
122-
if (write(outfd, drr, sizeof (*drr)) == -1)
123-
return (errno);
124-
if (payload_len != 0) {
125-
fletcher_4_incremental_native(payload, payload_len, zc);
126-
if (write(outfd, payload, payload_len) == -1)
127-
return (errno);
128-
}
129-
return (0);
130-
}
131-
13280
static void
13381
rdt_insert(redup_table_t *rdt,
13482
uint64_t guid, uint64_t object, uint64_t offset, uint64_t stream_offset)

cmd/zstream/zstream_util.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: CDDL-1.0
2+
/*
3+
* CDDL HEADER START
4+
*
5+
* This file and its contents are supplied under the terms of the
6+
* Common Development and Distribution License ("CDDL"), version 1.0.
7+
* You may only use this file in accordance with the terms of version
8+
* 1.0 of the CDDL.
9+
*
10+
* A full copy of the text of the CDDL should have accompanied this
11+
* source. A copy of the CDDL is also available via the Internet at
12+
* http://www.illumos.org/license/CDDL.
13+
*
14+
* CDDL HEADER END
15+
*/
16+
17+
/*
18+
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
19+
* Copyright (c) 2011, 2020 by Delphix. All rights reserved.
20+
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
21+
* Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
22+
* All rights reserved
23+
* Copyright (c) 2013 Steven Hartland. All rights reserved.
24+
* Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
25+
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
26+
* Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
27+
* Copyright (c) 2019 Datto Inc.
28+
* Copyright (c) 2024, Klara, Inc.
29+
*/
30+
31+
#include "zstream_util.h"
32+
33+
/*
34+
* From libzfs_sendrecv.c
35+
*/
36+
int
37+
dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
38+
zio_cksum_t *zc, int outfd)
39+
{
40+
ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
41+
==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
42+
fletcher_4_incremental_native(drr,
43+
offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
44+
if (drr->drr_type != DRR_BEGIN) {
45+
ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
46+
drr_checksum.drr_checksum));
47+
drr->drr_u.drr_checksum.drr_checksum = *zc;
48+
}
49+
fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
50+
sizeof (zio_cksum_t), zc);
51+
if (write(outfd, drr, sizeof (*drr)) == -1)
52+
return (errno);
53+
if (payload_len != 0) {
54+
fletcher_4_incremental_native(payload, payload_len, zc);
55+
if (write(outfd, payload, payload_len) == -1)
56+
return (errno);
57+
}
58+
return (0);
59+
}
60+
61+
void *
62+
safe_malloc(size_t size)
63+
{
64+
void *rv = malloc(size);
65+
if (rv == NULL) {
66+
(void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n",
67+
size);
68+
abort();
69+
}
70+
return (rv);
71+
}
72+
73+
void *
74+
safe_calloc(size_t n)
75+
{
76+
void *rv = calloc(1, n);
77+
if (rv == NULL) {
78+
fprintf(stderr,
79+
"Error: could not allocate %u bytes of memory\n",
80+
(int)n);
81+
exit(1);
82+
}
83+
return (rv);
84+
}
85+
86+
/*
87+
* Safe version of fread(), exits on error.
88+
*/
89+
int
90+
sfread(void *buf, size_t size, FILE *fp)
91+
{
92+
int rv = fread(buf, size, 1, fp);
93+
if (rv == 0 && ferror(fp)) {
94+
(void) fprintf(stderr, "Error while reading file: %s\n",
95+
strerror(errno));
96+
exit(1);
97+
}
98+
return (rv);
99+
}
100+

cmd/zstream/zstream_util.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: CDDL-1.0
2+
/*
3+
* CDDL HEADER START
4+
*
5+
* This file and its contents are supplied under the terms of the
6+
* Common Development and Distribution License ("CDDL"), version 1.0.
7+
* You may only use this file in accordance with the terms of version
8+
* 1.0 of the CDDL.
9+
*
10+
* A full copy of the text of the CDDL should have accompanied this
11+
* source. A copy of the CDDL is also available via the Internet at
12+
* http://www.illumos.org/license/CDDL.
13+
*
14+
* CDDL HEADER END
15+
*/
16+
17+
/*
18+
* Copyright (c) 2026 by Garth Snyder. All rights reserved.
19+
*/
20+
21+
#ifndef _ZSTREAM_UTIL_H
22+
#define _ZSTREAM_UTIL_H
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
#include <sys/zfs_ioctl.h>
29+
#include <sys/zio_checksum.h>
30+
31+
/*
32+
* The safe_ versions of the functions below exit(1) if the operation doesn't
33+
* succeed instead of returning an error.
34+
*/
35+
extern void *
36+
safe_malloc(size_t size);
37+
38+
extern void *
39+
safe_calloc(size_t n);
40+
41+
extern int
42+
sfread(void *buf, size_t size, FILE *fp);
43+
44+
/*
45+
* 1) Update checksum with the record header up to drr_checksum.
46+
* 2) Update checksum field in the record header.
47+
* 3) Update checksum with the checksum field in the record header.
48+
* 4) Update checksum with the contents of the payload.
49+
* 5) Write header and payload to fd.
50+
*/
51+
extern int
52+
dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
53+
zio_cksum_t *zc, int outfd);
54+
55+
#endif /* _ZSTREAM_UTIL_H */

0 commit comments

Comments
 (0)