Author: tsoome Date: Wed Feb 26 18:12:12 2020 New Revision: 358343 URL: https://svnweb.freebsd.org/changeset/base/358343
Log: loader: replace zfs_alloc/zfs_free with malloc/free Use common memory management. Modified: head/stand/libsa/zfs/zfs.c head/stand/libsa/zfs/zfsimpl.c head/sys/cddl/boot/zfs/zfssubr.c Modified: head/stand/libsa/zfs/zfs.c ============================================================================== --- head/stand/libsa/zfs/zfs.c Wed Feb 26 16:51:45 2020 (r358342) +++ head/stand/libsa/zfs/zfs.c Wed Feb 26 18:12:12 2020 (r358343) @@ -419,7 +419,7 @@ vdev_read(vdev_t *vdev, void *priv, off_t offset, void /* 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 ret = 0; error: - if (bouncebuf != NULL) - zfs_free(bouncebuf, secsz); + free(bouncebuf); return (ret); } Modified: head/stand/libsa/zfs/zfsimpl.c ============================================================================== --- head/stand/libsa/zfs/zfsimpl.c Wed Feb 26 16:51:45 2020 (r358342) +++ head/stand/libsa/zfs/zfsimpl.c Wed Feb 26 18:12:12 2020 (r358343) @@ -138,10 +138,7 @@ 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); static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result); @@ -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 *b 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 *b 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 *b 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, str 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, str 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, 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, 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, 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); } /* Modified: head/sys/cddl/boot/zfs/zfssubr.c ============================================================================== --- head/sys/cddl/boot/zfs/zfssubr.c Wed Feb 26 16:51:45 2020 (r358342) +++ head/sys/cddl/boot/zfs/zfssubr.c Wed Feb 26 18:12:12 2020 (r358343) @@ -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, 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, } } - zfs_free(p, psize); + free(p); } static int @@ -1098,7 +1096,11 @@ vdev_raidz_reconstruct_general(raidz_map_t *rm, int *t 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 *t 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 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 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, 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, 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 * 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 _______________________________________________ svn-src-all@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"