Print this page
2882 implement libzfs_core
2883 changing "canmount" property to "on" should not always remount dataset
2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Chris Siden <christopher.siden@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Bill Pijewski <wdp@joyent.com>
Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com>


  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  25  * Copyright (c) 2012 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <ctype.h>
  29 #include <errno.h>
  30 #include <devid.h>
  31 #include <fcntl.h>
  32 #include <libintl.h>
  33 #include <stdio.h>
  34 #include <stdlib.h>
  35 #include <strings.h>
  36 #include <unistd.h>

  37 #include <sys/efi_partition.h>
  38 #include <sys/vtoc.h>
  39 #include <sys/zfs_ioctl.h>
  40 #include <dlfcn.h>
  41 
  42 #include "zfs_namecheck.h"
  43 #include "zfs_prop.h"
  44 #include "libzfs_impl.h"
  45 #include "zfs_comutil.h"
  46 #include "zfeature_common.h"
  47 
  48 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
  49 
  50 #define DISK_ROOT       "/dev/dsk"
  51 #define RDISK_ROOT      "/dev/rdsk"
  52 #define BACKUP_SLICE    "s2"
  53 
  54 typedef struct prop_flags {
  55         int create:1;   /* Validate property on creation */
  56         int import:1;   /* Validate property on import */


1188 
1189                 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1190                 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1191                     "/") == 0);
1192 
1193                 zfs_close(zhp);
1194         }
1195 
1196 create_failed:
1197         zcmd_free_nvlists(&zc);
1198         nvlist_free(zc_props);
1199         nvlist_free(zc_fsprops);
1200         return (ret);
1201 }
1202 
1203 /*
1204  * Destroy the given pool.  It is up to the caller to ensure that there are no
1205  * datasets left in the pool.
1206  */
1207 int
1208 zpool_destroy(zpool_handle_t *zhp)
1209 {
1210         zfs_cmd_t zc = { 0 };
1211         zfs_handle_t *zfp = NULL;
1212         libzfs_handle_t *hdl = zhp->zpool_hdl;
1213         char msg[1024];
1214 
1215         if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1216             (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1217                 return (-1);
1218 
1219         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));

1220 
1221         if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1222                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1223                     "cannot destroy '%s'"), zhp->zpool_name);
1224 
1225                 if (errno == EROFS) {
1226                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1227                             "one or more devices is read only"));
1228                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1229                 } else {
1230                         (void) zpool_standard_error(hdl, errno, msg);
1231                 }
1232 
1233                 if (zfp)
1234                         zfs_close(zfp);
1235                 return (-1);
1236         }
1237 
1238         if (zfp) {
1239                 remove_mountpoint(zfp);


1354                         break;
1355 
1356                 default:
1357                         (void) zpool_standard_error(hdl, errno, msg);
1358                 }
1359 
1360                 ret = -1;
1361         } else {
1362                 ret = 0;
1363         }
1364 
1365         zcmd_free_nvlists(&zc);
1366 
1367         return (ret);
1368 }
1369 
1370 /*
1371  * Exports the pool from the system.  The caller must ensure that there are no
1372  * mounted datasets in the pool.
1373  */
1374 int
1375 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)

1376 {
1377         zfs_cmd_t zc = { 0 };
1378         char msg[1024];
1379 
1380         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1381             "cannot export '%s'"), zhp->zpool_name);
1382 
1383         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1384         zc.zc_cookie = force;
1385         zc.zc_guid = hardforce;

1386 
1387         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1388                 switch (errno) {
1389                 case EXDEV:
1390                         zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1391                             "use '-f' to override the following errors:\n"
1392                             "'%s' has an active shared spare which could be"
1393                             " used by other pools once '%s' is exported."),
1394                             zhp->zpool_name, zhp->zpool_name);
1395                         return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1396                             msg));
1397                 default:
1398                         return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1399                             msg));
1400                 }
1401         }
1402 
1403         return (0);
1404 }
1405 
1406 int
1407 zpool_export(zpool_handle_t *zhp, boolean_t force)
1408 {
1409         return (zpool_export_common(zhp, force, B_FALSE));
1410 }
1411 
1412 int
1413 zpool_export_force(zpool_handle_t *zhp)
1414 {
1415         return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1416 }
1417 
1418 static void
1419 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1420     nvlist_t *config)
1421 {
1422         nvlist_t *nv = NULL;
1423         uint64_t rewindto;
1424         int64_t loss = -1;
1425         struct tm t;
1426         char timestr[128];
1427 
1428         if (!hdl->libzfs_printerr || config == NULL)
1429                 return;
1430 
1431         if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1432             nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1433                 return;
1434         }
1435 


3557 /*
3558  * Upgrade a ZFS pool to the latest on-disk version.
3559  */
3560 int
3561 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3562 {
3563         zfs_cmd_t zc = { 0 };
3564         libzfs_handle_t *hdl = zhp->zpool_hdl;
3565 
3566         (void) strcpy(zc.zc_name, zhp->zpool_name);
3567         zc.zc_cookie = new_version;
3568 
3569         if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3570                 return (zpool_standard_error_fmt(hdl, errno,
3571                     dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3572                     zhp->zpool_name));
3573         return (0);
3574 }
3575 
3576 void
3577 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3578     char *history_str)
3579 {
3580         int i;
3581 
3582         (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3583         for (i = 1; i < argc; i++) {
3584                 if (strlen(history_str) + 1 + strlen(argv[i]) >
3585                     HIS_MAX_RECORD_LEN)
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
3596 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3597 {
3598         if (history_str == NULL)
3599                 return (EINVAL);
3600 
3601         if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3602                 return (EINVAL);
3603 
3604         if (hdl->libzfs_log_str != NULL)
3605                 free(hdl->libzfs_log_str);
3606 
3607         if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3608                 return (no_memory(hdl));
3609 
3610         return (0);







3611 }
3612 
3613 /*
3614  * Perform ioctl to get some command history of a pool.
3615  *
3616  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
3617  * logical offset of the history buffer to start reading from.
3618  *
3619  * Upon return, 'off' is the next logical offset to read from and
3620  * 'len' is the actual amount of bytes read into 'buf'.
3621  */
3622 static int
3623 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3624 {
3625         zfs_cmd_t zc = { 0 };
3626         libzfs_handle_t *hdl = zhp->zpool_hdl;
3627 
3628         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3629 
3630         zc.zc_history = (uint64_t)(uintptr_t)buf;




  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  25  * Copyright (c) 2012 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <ctype.h>
  29 #include <errno.h>
  30 #include <devid.h>
  31 #include <fcntl.h>
  32 #include <libintl.h>
  33 #include <stdio.h>
  34 #include <stdlib.h>
  35 #include <strings.h>
  36 #include <unistd.h>
  37 #include <libgen.h>
  38 #include <sys/efi_partition.h>
  39 #include <sys/vtoc.h>
  40 #include <sys/zfs_ioctl.h>
  41 #include <dlfcn.h>
  42 
  43 #include "zfs_namecheck.h"
  44 #include "zfs_prop.h"
  45 #include "libzfs_impl.h"
  46 #include "zfs_comutil.h"
  47 #include "zfeature_common.h"
  48 
  49 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
  50 
  51 #define DISK_ROOT       "/dev/dsk"
  52 #define RDISK_ROOT      "/dev/rdsk"
  53 #define BACKUP_SLICE    "s2"
  54 
  55 typedef struct prop_flags {
  56         int create:1;   /* Validate property on creation */
  57         int import:1;   /* Validate property on import */


1189 
1190                 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1191                 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1192                     "/") == 0);
1193 
1194                 zfs_close(zhp);
1195         }
1196 
1197 create_failed:
1198         zcmd_free_nvlists(&zc);
1199         nvlist_free(zc_props);
1200         nvlist_free(zc_fsprops);
1201         return (ret);
1202 }
1203 
1204 /*
1205  * Destroy the given pool.  It is up to the caller to ensure that there are no
1206  * datasets left in the pool.
1207  */
1208 int
1209 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1210 {
1211         zfs_cmd_t zc = { 0 };
1212         zfs_handle_t *zfp = NULL;
1213         libzfs_handle_t *hdl = zhp->zpool_hdl;
1214         char msg[1024];
1215 
1216         if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1217             (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1218                 return (-1);
1219 
1220         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1221         zc.zc_history = (uint64_t)(uintptr_t)log_str;
1222 
1223         if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1224                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1225                     "cannot destroy '%s'"), zhp->zpool_name);
1226 
1227                 if (errno == EROFS) {
1228                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229                             "one or more devices is read only"));
1230                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1231                 } else {
1232                         (void) zpool_standard_error(hdl, errno, msg);
1233                 }
1234 
1235                 if (zfp)
1236                         zfs_close(zfp);
1237                 return (-1);
1238         }
1239 
1240         if (zfp) {
1241                 remove_mountpoint(zfp);


1356                         break;
1357 
1358                 default:
1359                         (void) zpool_standard_error(hdl, errno, msg);
1360                 }
1361 
1362                 ret = -1;
1363         } else {
1364                 ret = 0;
1365         }
1366 
1367         zcmd_free_nvlists(&zc);
1368 
1369         return (ret);
1370 }
1371 
1372 /*
1373  * Exports the pool from the system.  The caller must ensure that there are no
1374  * mounted datasets in the pool.
1375  */
1376 static int
1377 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1378     const char *log_str)
1379 {
1380         zfs_cmd_t zc = { 0 };
1381         char msg[1024];
1382 
1383         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1384             "cannot export '%s'"), zhp->zpool_name);
1385 
1386         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1387         zc.zc_cookie = force;
1388         zc.zc_guid = hardforce;
1389         zc.zc_history = (uint64_t)(uintptr_t)log_str;
1390 
1391         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1392                 switch (errno) {
1393                 case EXDEV:
1394                         zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1395                             "use '-f' to override the following errors:\n"
1396                             "'%s' has an active shared spare which could be"
1397                             " used by other pools once '%s' is exported."),
1398                             zhp->zpool_name, zhp->zpool_name);
1399                         return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1400                             msg));
1401                 default:
1402                         return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1403                             msg));
1404                 }
1405         }
1406 
1407         return (0);
1408 }
1409 
1410 int
1411 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1412 {
1413         return (zpool_export_common(zhp, force, B_FALSE, log_str));
1414 }
1415 
1416 int
1417 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1418 {
1419         return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1420 }
1421 
1422 static void
1423 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1424     nvlist_t *config)
1425 {
1426         nvlist_t *nv = NULL;
1427         uint64_t rewindto;
1428         int64_t loss = -1;
1429         struct tm t;
1430         char timestr[128];
1431 
1432         if (!hdl->libzfs_printerr || config == NULL)
1433                 return;
1434 
1435         if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1436             nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1437                 return;
1438         }
1439 


3561 /*
3562  * Upgrade a ZFS pool to the latest on-disk version.
3563  */
3564 int
3565 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3566 {
3567         zfs_cmd_t zc = { 0 };
3568         libzfs_handle_t *hdl = zhp->zpool_hdl;
3569 
3570         (void) strcpy(zc.zc_name, zhp->zpool_name);
3571         zc.zc_cookie = new_version;
3572 
3573         if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3574                 return (zpool_standard_error_fmt(hdl, errno,
3575                     dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3576                     zhp->zpool_name));
3577         return (0);
3578 }
3579 
3580 void
3581 zfs_save_arguments(int argc, char **argv, char *string, int len)

3582 {
3583         (void) strlcpy(string, basename(argv[0]), len);
3584         for (int i = 1; i < argc; i++) {
3585                 (void) strlcat(string, " ", len);
3586                 (void) strlcat(string, argv[i], len);





3587         }
3588 }
3589 



3590 int
3591 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3592 {
3593         zfs_cmd_t zc = { 0 };
3594         nvlist_t *args;
3595         int err;








3596 
3597         args = fnvlist_alloc();
3598         fnvlist_add_string(args, "message", message);
3599         err = zcmd_write_src_nvlist(hdl, &zc, args);
3600         if (err == 0)
3601                 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3602         nvlist_free(args);
3603         zcmd_free_nvlists(&zc);
3604         return (err);
3605 }
3606 
3607 /*
3608  * Perform ioctl to get some command history of a pool.
3609  *
3610  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
3611  * logical offset of the history buffer to start reading from.
3612  *
3613  * Upon return, 'off' is the next logical offset to read from and
3614  * 'len' is the actual amount of bytes read into 'buf'.
3615  */
3616 static int
3617 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3618 {
3619         zfs_cmd_t zc = { 0 };
3620         libzfs_handle_t *hdl = zhp->zpool_hdl;
3621 
3622         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3623 
3624         zc.zc_history = (uint64_t)(uintptr_t)buf;