Print this page
6659 nvlist_free(NULL) is a no-op


1008 
1009         if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1010                 return (NULL);
1011 
1012         if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1013                 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1014                     dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1015                 zpool_close(zhp);
1016                 return (NULL);
1017         }
1018 
1019         return (zhp);
1020 }
1021 
1022 /*
1023  * Close the handle.  Simply frees the memory associated with the handle.
1024  */
1025 void
1026 zpool_close(zpool_handle_t *zhp)
1027 {
1028         if (zhp->zpool_config)
1029                 nvlist_free(zhp->zpool_config);
1030         if (zhp->zpool_old_config)
1031                 nvlist_free(zhp->zpool_old_config);
1032         if (zhp->zpool_props)
1033                 nvlist_free(zhp->zpool_props);
1034         free(zhp);
1035 }
1036 
1037 /*
1038  * Return the name of the pool.
1039  */
1040 const char *
1041 zpool_get_name(zpool_handle_t *zhp)
1042 {
1043         return (zhp->zpool_name);
1044 }
1045 
1046 
1047 /*
1048  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1049  */
1050 int
1051 zpool_get_state(zpool_handle_t *zhp)
1052 {


1531         if (altroot != NULL) {
1532                 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1533                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1534                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1535                             newname));
1536                 }
1537 
1538                 if (nvlist_add_string(props,
1539                     zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1540                     nvlist_add_string(props,
1541                     zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1542                         nvlist_free(props);
1543                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1544                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1545                             newname));
1546                 }
1547         }
1548 
1549         ret = zpool_import_props(hdl, config, newname, props,
1550             ZFS_IMPORT_NORMAL);
1551         if (props)
1552                 nvlist_free(props);
1553         return (ret);
1554 }
1555 
1556 static void
1557 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1558     int indent)
1559 {
1560         nvlist_t **child;
1561         uint_t c, children;
1562         char *vname;
1563         uint64_t is_log = 0;
1564 
1565         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1566             &is_log);
1567 
1568         if (name != NULL)
1569                 (void) printf("\t%*s%s%s\n", indent, "", name,
1570                     is_log ? " [log]" : "");
1571 


2837                 (void) fprintf(stderr, gettext("Internal error: unable to "
2838                     "retrieve pool configuration\n"));
2839                 return (-1);
2840         }
2841 
2842         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2843             == 0);
2844         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2845 
2846         if (props) {
2847                 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2848                 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2849                     props, vers, flags, msg)) == NULL)
2850                         return (-1);
2851         }
2852 
2853         if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2854             &children) != 0) {
2855                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2856                     "Source pool is missing vdev tree"));
2857                 if (zc_props)
2858                         nvlist_free(zc_props);
2859                 return (-1);
2860         }
2861 
2862         varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2863         vcount = 0;
2864 
2865         if (*newroot == NULL ||
2866             nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2867             &newchild, &newchildren) != 0)
2868                 newchildren = 0;
2869 
2870         for (c = 0; c < children; c++) {
2871                 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2872                 char *type;
2873                 nvlist_t **mchild, *vdev;
2874                 uint_t mchildren;
2875                 int entry;
2876 
2877                 /*


2986         if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2987                 goto out;
2988 
2989         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2990                 retval = zpool_standard_error(hdl, errno, msg);
2991                 goto out;
2992         }
2993 
2994         freelist = B_FALSE;
2995         memory_err = B_FALSE;
2996 
2997 out:
2998         if (varray != NULL) {
2999                 int v;
3000 
3001                 for (v = 0; v < vcount; v++)
3002                         nvlist_free(varray[v]);
3003                 free(varray);
3004         }
3005         zcmd_free_nvlists(&zc);
3006         if (zc_props)
3007                 nvlist_free(zc_props);
3008         if (newconfig)
3009                 nvlist_free(newconfig);
3010         if (freelist) {
3011                 nvlist_free(*newroot);
3012                 *newroot = NULL;
3013         }
3014 
3015         if (retval != 0)
3016                 return (retval);
3017 
3018         if (memory_err)
3019                 return (no_memory(hdl));
3020 
3021         return (0);
3022 }
3023 
3024 /*
3025  * Remove the given device.  Currently, this is supported only for hot spares
3026  * and level 2 cache devices.
3027  */
3028 int




1008 
1009         if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1010                 return (NULL);
1011 
1012         if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1013                 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1014                     dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1015                 zpool_close(zhp);
1016                 return (NULL);
1017         }
1018 
1019         return (zhp);
1020 }
1021 
1022 /*
1023  * Close the handle.  Simply frees the memory associated with the handle.
1024  */
1025 void
1026 zpool_close(zpool_handle_t *zhp)
1027 {

1028         nvlist_free(zhp->zpool_config);

1029         nvlist_free(zhp->zpool_old_config);

1030         nvlist_free(zhp->zpool_props);
1031         free(zhp);
1032 }
1033 
1034 /*
1035  * Return the name of the pool.
1036  */
1037 const char *
1038 zpool_get_name(zpool_handle_t *zhp)
1039 {
1040         return (zhp->zpool_name);
1041 }
1042 
1043 
1044 /*
1045  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1046  */
1047 int
1048 zpool_get_state(zpool_handle_t *zhp)
1049 {


1528         if (altroot != NULL) {
1529                 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1530                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1531                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1532                             newname));
1533                 }
1534 
1535                 if (nvlist_add_string(props,
1536                     zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1537                     nvlist_add_string(props,
1538                     zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1539                         nvlist_free(props);
1540                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1541                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1542                             newname));
1543                 }
1544         }
1545 
1546         ret = zpool_import_props(hdl, config, newname, props,
1547             ZFS_IMPORT_NORMAL);

1548         nvlist_free(props);
1549         return (ret);
1550 }
1551 
1552 static void
1553 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1554     int indent)
1555 {
1556         nvlist_t **child;
1557         uint_t c, children;
1558         char *vname;
1559         uint64_t is_log = 0;
1560 
1561         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1562             &is_log);
1563 
1564         if (name != NULL)
1565                 (void) printf("\t%*s%s%s\n", indent, "", name,
1566                     is_log ? " [log]" : "");
1567 


2833                 (void) fprintf(stderr, gettext("Internal error: unable to "
2834                     "retrieve pool configuration\n"));
2835                 return (-1);
2836         }
2837 
2838         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2839             == 0);
2840         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2841 
2842         if (props) {
2843                 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2844                 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2845                     props, vers, flags, msg)) == NULL)
2846                         return (-1);
2847         }
2848 
2849         if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2850             &children) != 0) {
2851                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2852                     "Source pool is missing vdev tree"));

2853                 nvlist_free(zc_props);
2854                 return (-1);
2855         }
2856 
2857         varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2858         vcount = 0;
2859 
2860         if (*newroot == NULL ||
2861             nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2862             &newchild, &newchildren) != 0)
2863                 newchildren = 0;
2864 
2865         for (c = 0; c < children; c++) {
2866                 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2867                 char *type;
2868                 nvlist_t **mchild, *vdev;
2869                 uint_t mchildren;
2870                 int entry;
2871 
2872                 /*


2981         if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2982                 goto out;
2983 
2984         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2985                 retval = zpool_standard_error(hdl, errno, msg);
2986                 goto out;
2987         }
2988 
2989         freelist = B_FALSE;
2990         memory_err = B_FALSE;
2991 
2992 out:
2993         if (varray != NULL) {
2994                 int v;
2995 
2996                 for (v = 0; v < vcount; v++)
2997                         nvlist_free(varray[v]);
2998                 free(varray);
2999         }
3000         zcmd_free_nvlists(&zc);

3001         nvlist_free(zc_props);

3002         nvlist_free(newconfig);
3003         if (freelist) {
3004                 nvlist_free(*newroot);
3005                 *newroot = NULL;
3006         }
3007 
3008         if (retval != 0)
3009                 return (retval);
3010 
3011         if (memory_err)
3012                 return (no_memory(hdl));
3013 
3014         return (0);
3015 }
3016 
3017 /*
3018  * Remove the given device.  Currently, this is supported only for hot spares
3019  * and level 2 cache devices.
3020  */
3021 int