Add linux idmap support
[zfs.git] / lib / libzfs / libzfs_pool.c
index 7df7e91..42f3038 100644 (file)
@@ -62,7 +62,7 @@ typedef struct prop_flags {
 static int
 zpool_get_all_props(zpool_handle_t *zhp)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
@@ -178,6 +178,8 @@ char *
 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
 {
        switch (state) {
+       default:
+               break;
        case VDEV_STATE_CLOSED:
        case VDEV_STATE_OFFLINE:
                return (gettext("OFFLINE"));
@@ -228,7 +230,7 @@ zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
 
                case ZPOOL_PROP_GUID:
                        intval = zpool_get_prop_int(zhp, prop, &src);
-                       (void) snprintf(buf, len, "%llu", intval);
+                       (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
                        break;
 
                case ZPOOL_PROP_ALTROOT:
@@ -295,7 +297,7 @@ zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
                            vs->vs_aux), len);
                        break;
                default:
-                       (void) snprintf(buf, len, "%llu", intval);
+                       (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
                }
                break;
 
@@ -421,6 +423,8 @@ zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
                 * Perform additional checking for specific properties.
                 */
                switch (prop) {
+               default:
+                       break;
                case ZPOOL_PROP_VERSION:
                        if (intval < version || intval > SPA_VERSION) {
                                zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
@@ -565,7 +569,7 @@ error:
 int
 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret = -1;
        char errbuf[1024];
        nvlist_t *nvl = NULL;
@@ -730,7 +734,10 @@ zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
                                zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
                                    "multiple '@' delimiters in name"));
                                break;
-
+                       case NAME_ERR_NO_AT:
+                               zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
+                                   "permission set is missing '@'"));
+                               break;
                        }
                }
                return (B_FALSE);
@@ -877,7 +884,7 @@ int
 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
     nvlist_t *props, nvlist_t *fsprops)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        nvlist_t *zc_fsprops = NULL;
        nvlist_t *zc_props = NULL;
        char msg[1024];
@@ -1009,7 +1016,7 @@ create_failed:
 int
 zpool_destroy(zpool_handle_t *zhp)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        zfs_handle_t *zfp = NULL;
        libzfs_handle_t *hdl = zhp->zpool_hdl;
        char msg[1024];
@@ -1052,7 +1059,7 @@ zpool_destroy(zpool_handle_t *zhp)
 int
 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret;
        libzfs_handle_t *hdl = zhp->zpool_hdl;
        char msg[1024];
@@ -1176,7 +1183,7 @@ zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
 int
 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
 
        (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
@@ -1238,7 +1245,7 @@ zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
        (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
 
        if (localtime_r((time_t *)&rewindto, &t) != NULL &&
-           strftime(timestr, 128, 0, &t) != 0) {
+           strftime(timestr, 128, "%c", &t) != 0) {
                if (dryrun) {
                        (void) printf(dgettext(TEXT_DOMAIN,
                            "Would be able to return %s "
@@ -1253,13 +1260,14 @@ zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
                        (void) printf(dgettext(TEXT_DOMAIN,
                            "%s approximately %lld "),
                            dryrun ? "Would discard" : "Discarded",
-                           (loss + 30) / 60);
+                           ((longlong_t)loss + 30) / 60);
                        (void) printf(dgettext(TEXT_DOMAIN,
                            "minutes of transactions.\n"));
                } else if (loss > 0) {
                        (void) printf(dgettext(TEXT_DOMAIN,
                            "%s approximately %lld "),
-                           dryrun ? "Would discard" : "Discarded", loss);
+                           dryrun ? "Would discard" : "Discarded",
+                           (longlong_t)loss);
                        (void) printf(dgettext(TEXT_DOMAIN,
                            "seconds of transactions.\n"));
                }
@@ -1298,7 +1306,7 @@ zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
            "Recovery is possible, but will result in some data loss.\n"));
 
        if (localtime_r((time_t *)&rewindto, &t) != NULL &&
-           strftime(timestr, 128, 0, &t) != 0) {
+           strftime(timestr, 128, "%c", &t) != 0) {
                (void) printf(dgettext(TEXT_DOMAIN,
                    "\tReturning the pool to its state as of %s\n"
                    "\tshould correct the problem.  "),
@@ -1312,11 +1320,13 @@ zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
        if (loss > 120) {
                (void) printf(dgettext(TEXT_DOMAIN,
                    "Approximately %lld minutes of data\n"
-                   "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
+                   "\tmust be discarded, irreversibly.  "),
+                   ((longlong_t)loss + 30) / 60);
        } else if (loss > 0) {
                (void) printf(dgettext(TEXT_DOMAIN,
                    "Approximately %lld seconds of data\n"
-                   "\tmust be discarded, irreversibly.  "), loss);
+                   "\tmust be discarded, irreversibly.  "),
+                   (longlong_t)loss);
        }
        if (edata != 0 && edata != UINT64_MAX) {
                if (edata == 1) {
@@ -1419,7 +1429,7 @@ int
 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
     nvlist_t *props, int flags)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        zpool_rewind_policy_t policy;
        nvlist_t *nv = NULL;
        nvlist_t *nvinfo = NULL;
@@ -1589,7 +1599,7 @@ zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
 int
 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
@@ -2133,7 +2143,7 @@ int
 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
     vdev_state_t *newstate)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tgt;
        boolean_t avail_spare, l2cache, islog;
@@ -2205,7 +2215,7 @@ zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
 int
 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tgt;
        boolean_t avail_spare, l2cache;
@@ -2255,12 +2265,12 @@ zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
 int
 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) snprintf(msg, sizeof (msg),
-           dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
+           dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
 
        (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
        zc.zc_guid = guid;
@@ -2290,12 +2300,12 @@ zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
 int
 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) snprintf(msg, sizeof (msg),
-           dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
+           dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
 
        (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
        zc.zc_guid = guid;
@@ -2344,7 +2354,7 @@ int
 zpool_vdev_attach(zpool_handle_t *zhp,
     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        int ret;
        nvlist_t *tgt;
@@ -2518,7 +2528,7 @@ zpool_vdev_attach(zpool_handle_t *zhp,
 int
 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tgt;
        boolean_t avail_spare, l2cache;
@@ -2616,7 +2626,7 @@ int
 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
     nvlist_t *props, splitflags_t flags)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
        nvlist_t **varray = NULL, *zc_props = NULL;
@@ -2827,7 +2837,7 @@ out:
 int
 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tgt;
        boolean_t avail_spare, l2cache, islog;
@@ -2872,7 +2882,7 @@ zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
 int
 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        nvlist_t *tgt;
        zpool_rewind_policy_t policy;
@@ -2948,13 +2958,13 @@ zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
 int
 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char msg[1024];
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) snprintf(msg, sizeof (msg),
            dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
-           guid);
+           (u_longlong_t)guid);
 
        (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
        zc.zc_guid = guid;
@@ -3032,7 +3042,7 @@ path_to_devid(const char *path)
 static void
 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
 
        (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
        (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
@@ -3183,7 +3193,7 @@ zbookmark_compare(const void *a, const void *b)
 int
 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        uint64_t count;
        zbookmark_t *zb = NULL;
        int i;
@@ -3279,7 +3289,7 @@ nomem:
 int
 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) strcpy(zc.zc_name, zhp->zpool_name);
@@ -3341,7 +3351,7 @@ zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
 static int
 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        libzfs_handle_t *hdl = zhp->zpool_hdl;
 
        (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
@@ -3464,18 +3474,106 @@ zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
        return (err);
 }
 
+/*
+ * Retrieve the next event.  If there is a new event available 'nvp' will
+ * contain a newly allocated nvlist and 'dropped' will be set to the number
+ * of missed events since the last call to this function.  When 'nvp' is
+ * set to NULL it indicates no new events are available.  In either case
+ * the function returns 0 and it is up to the caller to free 'nvp'.  In
+ * the case of a fatal error the function will return a non-zero value.
+ * When the function is called in blocking mode it will not return until
+ * a new event is available.
+ */
+int
+zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
+    int *dropped, int block, int cleanup_fd)
+{
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
+       int error = 0;
+
+       *nvp = NULL;
+       *dropped = 0;
+       zc.zc_cleanup_fd = cleanup_fd;
+
+       if (!block)
+               zc.zc_guid = ZEVENT_NONBLOCK;
+
+       if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
+               return (-1);
+
+retry:
+       if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
+               switch (errno) {
+               case ESHUTDOWN:
+                       error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
+                           dgettext(TEXT_DOMAIN, "zfs shutdown"));
+                       goto out;
+               case ENOENT:
+                       /* Blocking error case should not occur */
+                       if (block)
+                               error = zpool_standard_error_fmt(hdl, errno,
+                                   dgettext(TEXT_DOMAIN, "cannot get event"));
+
+                       goto out;
+               case ENOMEM:
+                       if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
+                               error = zfs_error_fmt(hdl, EZFS_NOMEM,
+                                   dgettext(TEXT_DOMAIN, "cannot get event"));
+                               goto out;
+                       } else {
+                               goto retry;
+                       }
+               default:
+                       error = zpool_standard_error_fmt(hdl, errno,
+                           dgettext(TEXT_DOMAIN, "cannot get event"));
+                       goto out;
+               }
+       }
+
+       error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
+       if (error != 0)
+               goto out;
+
+       *dropped = (int)zc.zc_cookie;
+out:
+       zcmd_free_nvlists(&zc);
+
+       return (error);
+}
+
+/*
+ * Clear all events.
+ */
+int
+zpool_events_clear(libzfs_handle_t *hdl, int *count)
+{
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
+       char msg[1024];
+
+       (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
+           "cannot clear events"));
+
+       if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
+               return (zpool_standard_error_fmt(hdl, errno, msg));
+
+       if (count != NULL)
+               *count = (int)zc.zc_cookie; /* # of events cleared */
+
+       return (0);
+}
+
 void
 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
     char *pathname, size_t len)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        boolean_t mounted = B_FALSE;
        char *mntpnt = NULL;
        char dsname[MAXNAMELEN];
 
        if (dsobj == 0) {
                /* special case for the MOS */
-               (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
+               (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
                return;
        }
 
@@ -3486,7 +3584,7 @@ zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
            ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
                /* just write out a path of two object numbers */
                (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
-                   dsobj, obj);
+                   (longlong_t)dsobj, (longlong_t)obj);
                return;
        }
        (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
@@ -3507,7 +3605,7 @@ zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
                            dsname, zc.zc_value);
                }
        } else {
-               (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
+               (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
        }
        free(mntpnt);
 }