summaryrefslogtreecommitdiff
path: root/usr/src/lib/libzfs
diff options
context:
space:
mode:
authorChunwei Chen <david.chen@nutanix.com>2020-01-30 12:57:07 -0700
committerJerry Jelinek <jerry.jelinek@joyent.com>2020-02-03 14:38:28 -0700
commita52121ea720d327228a95e9d0c4a426339f08c7f (patch)
treeb57b6d19b2c499258739c58c131404f4845b0f0b /usr/src/lib/libzfs
parentb5c83661f3899fe50c949117084b15d6478d9f5f (diff)
downloadillumos-joyent-a52121ea720d327228a95e9d0c4a426339f08c7f.tar.gz
12260 Fix zpool history unbounded memory usage
Portions contributed by: Jerry Jelinek <jerry.jelinek@joyent.com> Reviewed by: Tom Caputi <tcaputi@datto.com> Reviewed by: Matt Ahrens <matt@delphix.com> Reviewed by: Igor Kozhukhov <igor@dilos.org> Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov> Reviewed by: Sanjay Nadkarni <Sanjay G Nadkarni <snadkarni@tintri.com> Approved by: Dan McDonald <danmcd@joyent.com>
Diffstat (limited to 'usr/src/lib/libzfs')
-rw-r--r--usr/src/lib/libzfs/common/libzfs.h3
-rw-r--r--usr/src/lib/libzfs/common/libzfs_pool.c22
2 files changed, 14 insertions, 11 deletions
diff --git a/usr/src/lib/libzfs/common/libzfs.h b/usr/src/lib/libzfs/common/libzfs.h
index 36b70c11fb..d7411f0b85 100644
--- a/usr/src/lib/libzfs/common/libzfs.h
+++ b/usr/src/lib/libzfs/common/libzfs.h
@@ -459,7 +459,8 @@ typedef enum {
extern char *zpool_vdev_name(libzfs_handle_t *, zpool_handle_t *, nvlist_t *,
int name_flags);
extern int zpool_upgrade(zpool_handle_t *, uint64_t);
-extern int zpool_get_history(zpool_handle_t *, nvlist_t **);
+extern int zpool_get_history(zpool_handle_t *, nvlist_t **, uint64_t *,
+ boolean_t *);
extern int zpool_history_unpack(char *, uint64_t, uint64_t *,
nvlist_t ***, uint_t *);
extern void zpool_obj_to_path(zpool_handle_t *, uint64_t, uint64_t, char *,
diff --git a/usr/src/lib/libzfs/common/libzfs_pool.c b/usr/src/lib/libzfs/common/libzfs_pool.c
index b7c95489c8..20894d450a 100644
--- a/usr/src/lib/libzfs/common/libzfs_pool.c
+++ b/usr/src/lib/libzfs/common/libzfs_pool.c
@@ -4342,33 +4342,37 @@ zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
* Retrieve the command history of a pool.
*/
int
-zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
+zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
+ boolean_t *eof)
{
char *buf;
int buflen = 128 * 1024;
- uint64_t off = 0;
nvlist_t **records = NULL;
uint_t numrecords = 0;
- int err, i;
+ int err = 0, i;
+ uint64_t start = *off;
buf = malloc(buflen);
if (buf == NULL)
return (ENOMEM);
- do {
+ /* process about 1MB a time */
+ while (*off - start < 1024 * 1024) {
uint64_t bytes_read = buflen;
uint64_t leftover;
- if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
+ if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
break;
/* if nothing else was read in, we're at EOF, just return */
- if (!bytes_read)
+ if (!bytes_read) {
+ *eof = B_TRUE;
break;
+ }
if ((err = zpool_history_unpack(buf, bytes_read,
&leftover, &records, &numrecords)) != 0)
break;
- off -= leftover;
+ *off -= leftover;
if (leftover == bytes_read) {
/*
* no progress made, because buffer is not big enough
@@ -4380,9 +4384,7 @@ zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
if (buf == NULL)
return (ENOMEM);
}
-
- /* CONSTCOND */
- } while (1);
+ }
free(buf);