Skip to content

Commit

Permalink
ZAP: Fix leaf references on zap_expand_leaf() errors
Browse files Browse the repository at this point in the history
Depending on kind of error zap_expand_leaf() may return with or
without valid leaf reference held.  Make sure it returns NULL if
due to error it has no leaf to return.  Make its callers to check
the returned leaf pointer, and release the leaf if it is not NULL.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Alexander Motin <mav@FreeBSD.org>
Sponsored by:	iXsystems, Inc.
Closes openzfs#12366 
Closes openzfs#16159
  • Loading branch information
amotin authored and lundman committed Sep 4, 2024
1 parent 6a46f81 commit 4251ee3
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions module/zfs/zap.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
uint64_t object = zap->zap_object;

zap_put_leaf(l);
*lp = l = NULL;
zap_unlockdir(zap, tag);
err = zap_lockdir(os, object, tx, RW_WRITER,
FALSE, FALSE, tag, &zn->zn_zap);
Expand Down Expand Up @@ -920,21 +921,17 @@ fzap_add_cd(zap_name_t *zn,
} else if (err == EAGAIN) {
err = zap_expand_leaf(zn, l, tag, tx, &l);
zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
if (err == 0) {
if (err == 0)
goto retry;
} else if (err == ENOSPC) {
/*
* If we failed to expand the leaf, then bailout
* as there is no point trying
* zap_put_leaf_maybe_grow_ptrtbl().
*/
return (err);
}
}

out:
if (zap != NULL)
zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
if (l != NULL) {
if (err == ENOSPC)
zap_put_leaf(l);
else
zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
}
return (err);
}

Expand Down Expand Up @@ -991,8 +988,12 @@ fzap_update(zap_name_t *zn,
goto retry;
}

if (zap != NULL)
zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
if (l != NULL) {
if (err == ENOSPC)
zap_put_leaf(l);
else
zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
}
return (err);
}

Expand Down

0 comments on commit 4251ee3

Please sign in to comment.