usr/src/lib/libzfs/common/libzfs_pool.c
changeset 13743 95aba6e49b9f
parent 13700 2889e2596bd6
child 14049 4a7f6353bcf0
equal deleted inserted replaced
13742:b6bbdd77139c 13743:95aba6e49b9f
    32 #include <libintl.h>
    32 #include <libintl.h>
    33 #include <stdio.h>
    33 #include <stdio.h>
    34 #include <stdlib.h>
    34 #include <stdlib.h>
    35 #include <strings.h>
    35 #include <strings.h>
    36 #include <unistd.h>
    36 #include <unistd.h>
       
    37 #include <libgen.h>
    37 #include <sys/efi_partition.h>
    38 #include <sys/efi_partition.h>
    38 #include <sys/vtoc.h>
    39 #include <sys/vtoc.h>
    39 #include <sys/zfs_ioctl.h>
    40 #include <sys/zfs_ioctl.h>
    40 #include <dlfcn.h>
    41 #include <dlfcn.h>
    41 
    42 
  1203 /*
  1204 /*
  1204  * Destroy the given pool.  It is up to the caller to ensure that there are no
  1205  * Destroy the given pool.  It is up to the caller to ensure that there are no
  1205  * datasets left in the pool.
  1206  * datasets left in the pool.
  1206  */
  1207  */
  1207 int
  1208 int
  1208 zpool_destroy(zpool_handle_t *zhp)
  1209 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
  1209 {
  1210 {
  1210 	zfs_cmd_t zc = { 0 };
  1211 	zfs_cmd_t zc = { 0 };
  1211 	zfs_handle_t *zfp = NULL;
  1212 	zfs_handle_t *zfp = NULL;
  1212 	libzfs_handle_t *hdl = zhp->zpool_hdl;
  1213 	libzfs_handle_t *hdl = zhp->zpool_hdl;
  1213 	char msg[1024];
  1214 	char msg[1024];
  1215 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
  1216 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
  1216 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
  1217 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
  1217 		return (-1);
  1218 		return (-1);
  1218 
  1219 
  1219 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
  1220 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
       
  1221 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
  1220 
  1222 
  1221 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
  1223 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
  1222 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
  1224 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
  1223 		    "cannot destroy '%s'"), zhp->zpool_name);
  1225 		    "cannot destroy '%s'"), zhp->zpool_name);
  1224 
  1226 
  1369 
  1371 
  1370 /*
  1372 /*
  1371  * Exports the pool from the system.  The caller must ensure that there are no
  1373  * Exports the pool from the system.  The caller must ensure that there are no
  1372  * mounted datasets in the pool.
  1374  * mounted datasets in the pool.
  1373  */
  1375  */
  1374 int
  1376 static int
  1375 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
  1377 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
       
  1378     const char *log_str)
  1376 {
  1379 {
  1377 	zfs_cmd_t zc = { 0 };
  1380 	zfs_cmd_t zc = { 0 };
  1378 	char msg[1024];
  1381 	char msg[1024];
  1379 
  1382 
  1380 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
  1383 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
  1381 	    "cannot export '%s'"), zhp->zpool_name);
  1384 	    "cannot export '%s'"), zhp->zpool_name);
  1382 
  1385 
  1383 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
  1386 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
  1384 	zc.zc_cookie = force;
  1387 	zc.zc_cookie = force;
  1385 	zc.zc_guid = hardforce;
  1388 	zc.zc_guid = hardforce;
       
  1389 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
  1386 
  1390 
  1387 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
  1391 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
  1388 		switch (errno) {
  1392 		switch (errno) {
  1389 		case EXDEV:
  1393 		case EXDEV:
  1390 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
  1394 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
  1402 
  1406 
  1403 	return (0);
  1407 	return (0);
  1404 }
  1408 }
  1405 
  1409 
  1406 int
  1410 int
  1407 zpool_export(zpool_handle_t *zhp, boolean_t force)
  1411 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
  1408 {
  1412 {
  1409 	return (zpool_export_common(zhp, force, B_FALSE));
  1413 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
  1410 }
  1414 }
  1411 
  1415 
  1412 int
  1416 int
  1413 zpool_export_force(zpool_handle_t *zhp)
  1417 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
  1414 {
  1418 {
  1415 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
  1419 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
  1416 }
  1420 }
  1417 
  1421 
  1418 static void
  1422 static void
  1419 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
  1423 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
  1420     nvlist_t *config)
  1424     nvlist_t *config)
  3572 		    zhp->zpool_name));
  3576 		    zhp->zpool_name));
  3573 	return (0);
  3577 	return (0);
  3574 }
  3578 }
  3575 
  3579 
  3576 void
  3580 void
  3577 zpool_set_history_str(const char *subcommand, int argc, char **argv,
  3581 zfs_save_arguments(int argc, char **argv, char *string, int len)
  3578     char *history_str)
  3582 {
  3579 {
  3583 	(void) strlcpy(string, basename(argv[0]), len);
  3580 	int i;
  3584 	for (int i = 1; i < argc; i++) {
  3581 
  3585 		(void) strlcat(string, " ", len);
  3582 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
  3586 		(void) strlcat(string, argv[i], len);
  3583 	for (i = 1; i < argc; i++) {
  3587 	}
  3584 		if (strlen(history_str) + 1 + strlen(argv[i]) >
  3588 }
  3585 		    HIS_MAX_RECORD_LEN)
  3589 
  3586 			break;
       
  3587 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
       
  3588 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
       
  3589 	}
       
  3590 }
       
  3591 
       
  3592 /*
       
  3593  * Stage command history for logging.
       
  3594  */
       
  3595 int
  3590 int
  3596 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
  3591 zpool_log_history(libzfs_handle_t *hdl, const char *message)
  3597 {
  3592 {
  3598 	if (history_str == NULL)
  3593 	zfs_cmd_t zc = { 0 };
  3599 		return (EINVAL);
  3594 	nvlist_t *args;
  3600 
  3595 	int err;
  3601 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
  3596 
  3602 		return (EINVAL);
  3597 	args = fnvlist_alloc();
  3603 
  3598 	fnvlist_add_string(args, "message", message);
  3604 	if (hdl->libzfs_log_str != NULL)
  3599 	err = zcmd_write_src_nvlist(hdl, &zc, args);
  3605 		free(hdl->libzfs_log_str);
  3600 	if (err == 0)
  3606 
  3601 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
  3607 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
  3602 	nvlist_free(args);
  3608 		return (no_memory(hdl));
  3603 	zcmd_free_nvlists(&zc);
  3609 
  3604 	return (err);
  3610 	return (0);
       
  3611 }
  3605 }
  3612 
  3606 
  3613 /*
  3607 /*
  3614  * Perform ioctl to get some command history of a pool.
  3608  * Perform ioctl to get some command history of a pool.
  3615  *
  3609  *