From c1c4c81fd75a7d0c14011ea1cf6113ecdd80e16a Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Wed, 26 Feb 2020 18:12:12 +0000 Subject: [PATCH] loader: replace zfs_alloc/zfs_free with malloc/free Use common memory management. --- stand/libsa/zfs/zfs.c | 5 +-- stand/libsa/zfs/zfsimpl.c | 76 +++++++++++++++---------------------- sys/cddl/boot/zfs/zfssubr.c | 65 +++++++++++++++++++++---------- 3 files changed, 76 insertions(+), 70 deletions(-) diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c index 9276ab4d6132..3129344bdf48 100644 --- a/stand/libsa/zfs/zfs.c +++ b/stand/libsa/zfs/zfs.c @@ -419,7 +419,7 @@ vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes) /* Return of partial sector data requires a bounce buffer. */ if ((head > 0) || do_tail_read) { - bouncebuf = zfs_alloc(secsz); + bouncebuf = malloc(secsz); if (bouncebuf == NULL) { printf("vdev_read: out of memory\n"); return (ENOMEM); @@ -464,8 +464,7 @@ vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes) ret = 0; error: - if (bouncebuf != NULL) - zfs_free(bouncebuf, secsz); + free(bouncebuf); return (ret); } diff --git a/stand/libsa/zfs/zfsimpl.c b/stand/libsa/zfs/zfsimpl.c index 50ebe6293c33..33d4817b64fd 100644 --- a/stand/libsa/zfs/zfsimpl.c +++ b/stand/libsa/zfs/zfsimpl.c @@ -138,9 +138,6 @@ static spa_list_t zfs_pools; static const dnode_phys_t *dnode_cache_obj; static uint64_t dnode_cache_bn; static char *dnode_cache_buf; -static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr; - -#define TEMP_SIZE (1024 * 1024) static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf); static int zfs_get_root(const spa_t *spa, uint64_t *objid); @@ -167,38 +164,11 @@ zfs_init(void) STAILQ_INIT(&zfs_vdevs); STAILQ_INIT(&zfs_pools); - zfs_temp_buf = malloc(TEMP_SIZE); - zfs_temp_end = zfs_temp_buf + TEMP_SIZE; - zfs_temp_ptr = zfs_temp_buf; dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE); zfs_init_crc(); } -static void * -zfs_alloc(size_t size) -{ - char *ptr; - - if (zfs_temp_ptr + size > zfs_temp_end) { - panic("ZFS: out of temporary buffer space"); - } - ptr = zfs_temp_ptr; - zfs_temp_ptr += size; - - return (ptr); -} - -static void -zfs_free(void *ptr, size_t size) -{ - - zfs_temp_ptr -= size; - if (zfs_temp_ptr != ptr) { - panic("ZFS: zfs_alloc()/zfs_free() mismatch"); - } -} - static int xdr_int(const unsigned char **xdr, int *ip) { @@ -2151,17 +2121,20 @@ zio_read(const spa_t *spa, const blkptr_t *bp, void *buf) ASSERT(size <= BPE_PAYLOAD_SIZE); if (cpfunc != ZIO_COMPRESS_OFF) - pbuf = zfs_alloc(size); + pbuf = malloc(size); else pbuf = buf; + if (pbuf == NULL) + return (ENOMEM); + decode_embedded_bp_compressed(bp, pbuf); error = 0; if (cpfunc != ZIO_COMPRESS_OFF) { error = zio_decompress_data(cpfunc, pbuf, size, buf, BP_GET_LSIZE(bp)); - zfs_free(pbuf, size); + free(pbuf); } if (error != 0) printf("ZFS: i/o error - unable to decompress " @@ -2198,10 +2171,15 @@ zio_read(const spa_t *spa, const blkptr_t *bp, void *buf) size = P2ROUNDUP(size, align); } if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF) - pbuf = zfs_alloc(size); + pbuf = malloc(size); else pbuf = buf; + if (pbuf == NULL) { + error = ENOMEM; + break; + } + if (DVA_GET_GANG(dva)) error = zio_read_gang(spa, bp, pbuf); else @@ -2214,12 +2192,13 @@ zio_read(const spa_t *spa, const blkptr_t *bp, void *buf) bcopy(pbuf, buf, BP_GET_PSIZE(bp)); } if (buf != pbuf) - zfs_free(pbuf, size); + free(pbuf); if (error == 0) break; } if (error != 0) printf("ZFS: i/o error - all block copies unavailable\n"); + return (error); } @@ -3418,10 +3397,14 @@ zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb) int error; size = BP_GET_LSIZE(bp); - buf = zfs_alloc(size); - error = zio_read(spa, bp, buf); + buf = malloc(size); + if (buf == NULL) + error = ENOMEM; + else + error = zio_read(spa, bp, buf); + if (error != 0) { - zfs_free(buf, size); + free(buf); return (error); } sahdrp = buf; @@ -3438,8 +3421,7 @@ zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb) SA_GID_OFFSET); sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize + SA_SIZE_OFFSET); - if (buf != NULL) - zfs_free(buf, size); + free(buf); } return (0); @@ -3457,9 +3439,9 @@ zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) int hdrsize; char *p; - if (dn->dn_bonuslen != 0) + if (dn->dn_bonuslen != 0) { sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); - else { + } else { blkptr_t *bp; if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0) @@ -3467,10 +3449,13 @@ zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) bp = DN_SPILL_BLKPTR(dn); size = BP_GET_LSIZE(bp); - buf = zfs_alloc(size); - rc = zio_read(spa, bp, buf); + buf = malloc(size); + if (buf == NULL) + rc = ENOMEM; + else + rc = zio_read(spa, bp, buf); if (rc != 0) { - zfs_free(buf, size); + free(buf); return (rc); } sahdrp = buf; @@ -3478,8 +3463,7 @@ zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) hdrsize = SA_HDR_SIZE(sahdrp); p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET); memcpy(path, p, psize); - if (buf != NULL) - zfs_free(buf, size); + free(buf); return (0); } /* diff --git a/sys/cddl/boot/zfs/zfssubr.c b/sys/cddl/boot/zfs/zfssubr.c index e15cae438d71..0588764535b7 100644 --- a/sys/cddl/boot/zfs/zfssubr.c +++ b/sys/cddl/boot/zfs/zfssubr.c @@ -43,9 +43,6 @@ static uint64_t zfs_crc64_table[256]; for (;;) ; \ } while (0) -#define kmem_alloc(size, flag) zfs_alloc((size)) -#define kmem_free(ptr, size) zfs_free((ptr), (size)) - static void zfs_init_crc(void) { @@ -376,9 +373,6 @@ zap_hash(uint64_t salt, const char *name) return (crc); } -static void *zfs_alloc(size_t size); -static void zfs_free(void *ptr, size_t size); - typedef struct raidz_col { uint64_t rc_devidx; /* child device index for I/O */ uint64_t rc_offset; /* device offset */ @@ -981,7 +975,11 @@ vdev_raidz_matrix_reconstruct(raidz_map_t *rm, int n, int nmissing, log = 0; /* gcc */ psize = sizeof (invlog[0][0]) * n * nmissing; - p = zfs_alloc(psize); + p = malloc(psize); + if (p == NULL) { + printf("Out of memory\n"); + return; + } for (pp = p, i = 0; i < nmissing; i++) { invlog[i] = pp; @@ -1037,7 +1035,7 @@ vdev_raidz_matrix_reconstruct(raidz_map_t *rm, int n, int nmissing, } } - zfs_free(p, psize); + free(p); } static int @@ -1098,7 +1096,11 @@ vdev_raidz_reconstruct_general(raidz_map_t *rm, int *tgts, int ntgts) psize = (sizeof (rows[0][0]) + sizeof (invrows[0][0])) * nmissing_rows * n + sizeof (used[0]) * n; - p = kmem_alloc(psize, KM_SLEEP); + p = malloc(psize); + if (p == NULL) { + printf("Out of memory\n"); + return (code); + } for (pp = p, i = 0; i < nmissing_rows; i++) { rows[i] = pp; @@ -1141,7 +1143,7 @@ vdev_raidz_reconstruct_general(raidz_map_t *rm, int *tgts, int ntgts) vdev_raidz_matrix_reconstruct(rm, n, nmissing_rows, missing_rows, invrows, used); - kmem_free(p, psize); + free(p); return (code); } @@ -1214,7 +1216,9 @@ vdev_raidz_map_alloc(void *data, off_t offset, size_t size, uint64_t unit_shift, ASSERT3U(acols, <=, scols); - rm = zfs_alloc(offsetof(raidz_map_t, rm_col[scols])); + rm = malloc(offsetof(raidz_map_t, rm_col[scols])); + if (rm == NULL) + return (rm); rm->rm_cols = acols; rm->rm_scols = scols; @@ -1259,8 +1263,16 @@ vdev_raidz_map_alloc(void *data, off_t offset, size_t size, uint64_t unit_shift, ASSERT3U(rm->rm_asize - asize, ==, rm->rm_nskip << unit_shift); ASSERT3U(rm->rm_nskip, <=, nparity); - for (c = 0; c < rm->rm_firstdatacol; c++) - rm->rm_col[c].rc_data = zfs_alloc(rm->rm_col[c].rc_size); + for (c = 0; c < rm->rm_firstdatacol; c++) { + rm->rm_col[c].rc_data = malloc(rm->rm_col[c].rc_size); + if (rm->rm_col[c].rc_data == NULL) { + c++; + while (c != 0) + free(rm->rm_col[--c].rc_data); + free(rm); + return (NULL); + } + } rm->rm_col[c].rc_data = data; @@ -1312,9 +1324,9 @@ vdev_raidz_map_free(raidz_map_t *rm) int c; for (c = rm->rm_firstdatacol - 1; c >= 0; c--) - zfs_free(rm->rm_col[c].rc_data, rm->rm_col[c].rc_size); + free(rm->rm_col[c].rc_data); - zfs_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_scols])); + free(rm); } static vdev_t * @@ -1358,8 +1370,12 @@ raidz_parity_verify(raidz_map_t *rm) rc = &rm->rm_col[c]; if (!rc->rc_tried || rc->rc_error != 0) continue; - orig[c] = zfs_alloc(rc->rc_size); - bcopy(rc->rc_data, orig[c], rc->rc_size); + orig[c] = malloc(rc->rc_size); + if (orig[c] != NULL) { + bcopy(rc->rc_data, orig[c], rc->rc_size); + } else { + printf("Out of memory\n"); + } } vdev_raidz_generate_parity(rm); @@ -1368,11 +1384,12 @@ raidz_parity_verify(raidz_map_t *rm) rc = &rm->rm_col[c]; if (!rc->rc_tried || rc->rc_error != 0) continue; - if (bcmp(orig[c], rc->rc_data, rc->rc_size) != 0) { + if (orig[c] == NULL || + bcmp(orig[c], rc->rc_data, rc->rc_size) != 0) { rc->rc_error = ECKSUM; ret++; } - zfs_free(orig[c], rc->rc_size); + free(orig[c]); } return (ret); @@ -1440,7 +1457,11 @@ vdev_raidz_combrec(const spa_t *spa, raidz_map_t *rm, const blkptr_t *bp, ASSERT(orig[i] != NULL); } - orig[n - 1] = zfs_alloc(rm->rm_col[0].rc_size); + orig[n - 1] = malloc(rm->rm_col[0].rc_size); + if (orig[n - 1] == NULL) { + ret = ENOMEM; + goto done; + } current = 0; next = tgts[current]; @@ -1523,7 +1544,7 @@ vdev_raidz_combrec(const spa_t *spa, raidz_map_t *rm, const blkptr_t *bp, n--; done: for (i = n - 1; i >= 0; i--) { - zfs_free(orig[i], rm->rm_col[0].rc_size); + free(orig[i]); } return (ret); @@ -1552,6 +1573,8 @@ vdev_raidz_read(vdev_t *vd, const blkptr_t *bp, void *data, rm = vdev_raidz_map_alloc(data, offset, bytes, tvd->v_ashift, vd->v_nchildren, vd->v_nparity); + if (rm == NULL) + return (ENOMEM); /* * Iterate over the columns in reverse order so that we hit the parity