]> git.proxmox.com Git - mirror_zfs.git/blobdiff - cmd/zfs/zfs_main.c
OpenZFS 6550 - cmd/zfs: cleanup gcc warnings
[mirror_zfs.git] / cmd / zfs / zfs_main.c
index 7525afcbfd6df6cb098b7b5e80ae54e10565199e..3b72d60f2284cb66c6425f6d9c287d5990ab7584 100644 (file)
@@ -26,6 +26,7 @@
  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  * Copyright (c) 2013 Steven Hartland.  All rights reserved.
  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
  */
 
 #include <assert.h>
@@ -248,10 +249,11 @@ get_usage(zfs_help_t idx)
        case HELP_PROMOTE:
                return (gettext("\tpromote <clone-filesystem>\n"));
        case HELP_RECEIVE:
-               return (gettext("\treceive [-vnFu] <filesystem|volume|"
+               return (gettext("\treceive [-vnsFu] <filesystem|volume|"
                    "snapshot>\n"
-                   "\treceive [-vnFu] [-o origin=<snapshot>] [-d | -e] "
-                   "<filesystem>\n"));
+                   "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
+                   "<filesystem>\n"
+                   "\treceive -A <filesystem|volume>\n"));
        case HELP_RENAME:
                return (gettext("\trename [-f] <filesystem|volume|snapshot> "
                    "<filesystem|volume|snapshot>\n"
@@ -260,15 +262,16 @@ get_usage(zfs_help_t idx)
        case HELP_ROLLBACK:
                return (gettext("\trollback [-rRf] <snapshot>\n"));
        case HELP_SEND:
-               return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
+               return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
                    "<snapshot>\n"
                    "\tsend [-Le] [-i snapshot|bookmark] "
-                   "<filesystem|volume|snapshot>\n"));
+                   "<filesystem|volume|snapshot>\n"
+                   "\tsend [-nvPe] -t <receive_resume_token>\n"));
        case HELP_SET:
                return (gettext("\tset <property=value> ... "
                    "<filesystem|volume|snapshot> ...\n"));
        case HELP_SHARE:
-               return (gettext("\tshare <-a | filesystem>\n"));
+               return (gettext("\tshare <-a [nfs|smb] | filesystem>\n"));
        case HELP_SNAPSHOT:
                return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
                    "<filesystem|volume>@<snap> ...\n"));
@@ -277,7 +280,7 @@ get_usage(zfs_help_t idx)
                    "<-a | filesystem|mountpoint>\n"));
        case HELP_UNSHARE:
                return (gettext("\tunshare "
-                   "<-a | filesystem|mountpoint>\n"));
+                   "<-a [nfs|smb] | filesystem|mountpoint>\n"));
        case HELP_ALLOW:
                return (gettext("\tallow <filesystem|volume>\n"
                    "\tallow [-ldug] "
@@ -610,7 +613,12 @@ zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
         */
        if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
            zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
-               if (zfs_mount(zhp, NULL, 0) != 0) {
+               if (geteuid() != 0) {
+                       (void) fprintf(stderr, gettext("filesystem "
+                           "successfully created, but it may only be "
+                           "mounted by root\n"));
+                       ret = 1;
+               } else if (zfs_mount(zhp, NULL, 0) != 0) {
                        (void) fprintf(stderr, gettext("filesystem "
                            "successfully created, but not mounted\n"));
                        ret = 1;
@@ -652,8 +660,10 @@ zfs_do_clone(int argc, char **argv)
        while ((c = getopt(argc, argv, "o:p")) != -1) {
                switch (c) {
                case 'o':
-                       if (parseprop(props, optarg) != 0)
+                       if (parseprop(props, optarg) != 0) {
+                               nvlist_free(props);
                                return (1);
+                       }
                        break;
                case 'p':
                        parents = B_TRUE;
@@ -685,8 +695,10 @@ zfs_do_clone(int argc, char **argv)
        }
 
        /* open the source dataset */
-       if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
+       if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
+               nvlist_free(props);
                return (1);
+       }
 
        if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
            ZFS_TYPE_VOLUME)) {
@@ -696,10 +708,16 @@ zfs_do_clone(int argc, char **argv)
                 * complain.
                 */
                if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
-                   ZFS_TYPE_VOLUME))
+                   ZFS_TYPE_VOLUME)) {
+                       zfs_close(zhp);
+                       nvlist_free(props);
                        return (0);
-               if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
+               }
+               if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
+                       zfs_close(zhp);
+                       nvlist_free(props);
                        return (1);
+               }
        }
 
        /* pass to libzfs */
@@ -840,7 +858,7 @@ zfs_do_create(int argc, char **argv)
                char *strval;
                char msg[1024];
 
-               if ((p = strchr(argv[0], '/')))
+               if ((p = strchr(argv[0], '/')) != NULL)
                        *p = '\0';
                zpool_handle = zpool_open(g_zfs, argv[0]);
                if (p != NULL)
@@ -1273,8 +1291,10 @@ zfs_do_destroy(int argc, char **argv)
                *at = '\0';
                zhp = zfs_open(g_zfs, argv[0],
                    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
-               if (zhp == NULL)
+               if (zhp == NULL) {
+                       nvlist_free(cb.cb_nvl);
                        return (1);
+               }
 
                cb.cb_snapspec = at + 1;
                if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
@@ -1363,7 +1383,7 @@ zfs_do_destroy(int argc, char **argv)
                            "cannot destroy bookmark");
                }
 
-               nvlist_free(cb.cb_nvl);
+               nvlist_free(nvl);
 
                return (err);
        } else {
@@ -1476,7 +1496,7 @@ get_callback(zfs_handle_t *zhp, void *data)
        char buf[ZFS_MAXPROPLEN];
        char rbuf[ZFS_MAXPROPLEN];
        zprop_source_t sourcetype;
-       char source[ZFS_MAXNAMELEN];
+       char source[ZFS_MAX_DATASET_NAME_LEN];
        zprop_get_cbdata_t *cbp = data;
        nvlist_t *user_props = zfs_get_user_props(zhp);
        zprop_list_t *pl = cbp->cb_proplist;
@@ -1956,7 +1976,7 @@ typedef struct upgrade_cbdata {
        uint64_t cb_version;
        boolean_t cb_newer;
        boolean_t cb_foundone;
-       char cb_lastfs[ZFS_MAXNAMELEN];
+       char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
 } upgrade_cbdata_t;
 
 static int
@@ -2155,7 +2175,7 @@ zfs_do_upgrade(int argc, char **argv)
                if (cb.cb_numfailed != 0)
                        ret = 1;
        } else {
-               /* List old-version filesytems */
+               /* List old-version filesystems */
                boolean_t found;
                (void) printf(gettext("This system is currently running "
                    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
@@ -2204,10 +2224,14 @@ enum us_field_types {
        USFIELD_TYPE,
        USFIELD_NAME,
        USFIELD_USED,
-       USFIELD_QUOTA
+       USFIELD_QUOTA,
+       USFIELD_OBJUSED,
+       USFIELD_OBJQUOTA
 };
-static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
-static char *us_field_names[] = { "type", "name", "used", "quota" };
+static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
+                                   "OBJUSED", "OBJQUOTA" };
+static char *us_field_names[] = { "type", "name", "used", "quota",
+                                   "objused", "objquota" };
 #define        USFIELD_LAST    (sizeof (us_field_names) / sizeof (char *))
 
 #define        USTYPE_PSX_GRP  (1 << 0)
@@ -2309,9 +2333,9 @@ compare_nums:
                                        rc = (rv64 < lv64) ? 1 : -1;
                        } else {
                                if ((nvlist_lookup_string(lnvl, propname,
-                                               &lvstr) == ENOENT) ||
+                                   &lvstr) == ENOENT) ||
                                    (nvlist_lookup_string(rnvl, propname,
-                                               &rvstr) == ENOENT)) {
+                                   &rvstr) == ENOENT)) {
                                        goto compare_nums;
                                }
                                rc = strcmp(lvstr, rvstr);
@@ -2330,6 +2354,7 @@ compare_nums:
                        if (rv64 != lv64)
                                rc = (rv64 < lv64) ? 1 : -1;
                        break;
+
                default:
                        break;
                }
@@ -2355,6 +2380,20 @@ compare_nums:
        return (0);
 }
 
+static boolean_t
+zfs_prop_is_user(unsigned p)
+{
+       return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
+           p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
+}
+
+static boolean_t
+zfs_prop_is_group(unsigned p)
+{
+       return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
+           p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
+}
+
 static inline const char *
 us_type2str(unsigned field_type)
 {
@@ -2405,7 +2444,7 @@ userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
        if (domain != NULL && domain[0] != '\0') {
 #ifdef HAVE_IDMAP
                /* SMB */
-               char sid[ZFS_MAXNAMELEN + 32];
+               char sid[MAXNAMELEN + 32];
                uid_t id;
                uint64_t classes;
                int err;
@@ -2444,7 +2483,7 @@ userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
 
        if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
                /* POSIX or -i */
-               if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
+               if (zfs_prop_is_group(prop)) {
                        type = USTYPE_PSX_GRP;
                        if (!cb->cb_numname) {
                                struct group *g;
@@ -2492,7 +2531,7 @@ userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
                namelen = strlen(name);
        }
        nameidx = us_field_index("name");
-       if (namelen > cb->cb_width[nameidx])
+       if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
                cb->cb_width[nameidx] = namelen;
 
        /*
@@ -2519,13 +2558,25 @@ userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
                propname = "used";
                if (!nvlist_exists(props, "quota"))
                        (void) nvlist_add_uint64(props, "quota", 0);
-       } else {
+       } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA) {
                propname = "quota";
                if (!nvlist_exists(props, "used"))
                        (void) nvlist_add_uint64(props, "used", 0);
+       } else if (prop == ZFS_PROP_USEROBJUSED ||
+           prop == ZFS_PROP_GROUPOBJUSED) {
+               propname = "objused";
+               if (!nvlist_exists(props, "objquota"))
+                       (void) nvlist_add_uint64(props, "objquota", 0);
+       } else if (prop == ZFS_PROP_USEROBJQUOTA ||
+           prop == ZFS_PROP_GROUPOBJQUOTA) {
+               propname = "objquota";
+               if (!nvlist_exists(props, "objused"))
+                       (void) nvlist_add_uint64(props, "objused", 0);
+       } else {
+               return (-1);
        }
        sizeidx = us_field_index(propname);
-       if (sizelen > cb->cb_width[sizeidx])
+       if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
                cb->cb_width[sizeidx] = sizelen;
 
        if (nvlist_add_uint64(props, propname, space) != 0)
@@ -2539,7 +2590,7 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
     size_t *width, us_node_t *node)
 {
        nvlist_t *nvl = node->usn_nvl;
-       char valstr[ZFS_MAXNAMELEN];
+       char valstr[MAXNAMELEN];
        boolean_t first = B_TRUE;
        int cfield = 0;
        int field;
@@ -2555,7 +2606,7 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
                data_type_t type;
                uint32_t val32;
                uint64_t val64;
-               char *strval = NULL;
+               char *strval = "-";
 
                while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
                        if (strcmp(nvpair_name(nvp),
@@ -2563,7 +2614,7 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
                                break;
                }
 
-               type = nvpair_type(nvp);
+               type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
                switch (type) {
                case DATA_TYPE_UINT32:
                        (void) nvpair_value_uint32(nvp, &val32);
@@ -2574,32 +2625,38 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
                case DATA_TYPE_STRING:
                        (void) nvpair_value_string(nvp, &strval);
                        break;
+               case DATA_TYPE_UNKNOWN:
+                       break;
                default:
                        (void) fprintf(stderr, "invalid data type\n");
                }
 
                switch (field) {
                case USFIELD_TYPE:
-                       strval = (char *)us_type2str(val32);
+                       if (type == DATA_TYPE_UINT32)
+                               strval = (char *)us_type2str(val32);
                        break;
                case USFIELD_NAME:
                        if (type == DATA_TYPE_UINT64) {
                                (void) sprintf(valstr, "%llu",
-                                   (u_longlong_t) val64);
+                                   (u_longlong_t)val64);
                                strval = valstr;
                        }
                        break;
                case USFIELD_USED:
                case USFIELD_QUOTA:
+               case USFIELD_OBJUSED:
+               case USFIELD_OBJQUOTA:
                        if (type == DATA_TYPE_UINT64) {
                                if (parsable) {
                                        (void) sprintf(valstr, "%llu",
-                                           (u_longlong_t) val64);
+                                           (u_longlong_t)val64);
                                } else {
                                        zfs_nicenum(val64, valstr,
                                            sizeof (valstr));
                                }
-                               if (field == USFIELD_QUOTA &&
+                               if ((field == USFIELD_QUOTA ||
+                                   field == USFIELD_OBJQUOTA) &&
                                    strcmp(valstr, "0") == 0)
                                        strval = "none";
                                else
@@ -2617,9 +2674,9 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
                if (scripted)
                        (void) printf("%s", strval);
                else if (field == USFIELD_TYPE || field == USFIELD_NAME)
-                       (void) printf("%-*s", (int) width[field], strval);
+                       (void) printf("%-*s", (int)width[field], strval);
                else
-                       (void) printf("%*s", (int) width[field], strval);
+                       (void) printf("%*s", (int)width[field], strval);
 
                first = B_FALSE;
                cfield++;
@@ -2644,10 +2701,10 @@ print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
                        col = gettext(us_field_hdr[field]);
                        if (field == USFIELD_TYPE || field == USFIELD_NAME) {
                                (void) printf(first ? "%-*s" : "  %-*s",
-                                   (int) width[field], col);
+                                   (int)width[field], col);
                        } else {
                                (void) printf(first ? "%*s" : "  %*s",
-                                   (int) width[field], col);
+                                   (int)width[field], col);
                        }
                        first = B_FALSE;
                        cfield++;
@@ -2671,7 +2728,7 @@ zfs_do_userspace(int argc, char **argv)
        uu_avl_t *avl_tree;
        uu_avl_walk_t *walk;
        char *delim;
-       char deffields[] = "type,name,used,quota";
+       char deffields[] = "type,name,used,quota,objused,objquota";
        char *ofield = NULL;
        char *tfield = NULL;
        int cfield = 0;
@@ -2820,11 +2877,12 @@ zfs_do_userspace(int argc, char **argv)
                cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
 
        for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
-               if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
+               if ((zfs_prop_is_user(p) &&
                    !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
-                   ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
+                   (zfs_prop_is_group(p) &&
                    !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
                        continue;
+
                cb.cb_prop = p;
                if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
                        return (ret);
@@ -3320,7 +3378,7 @@ zfs_do_promote(int argc, char **argv)
  *
  *     -r      Delete any intervening snapshots before doing rollback
  *     -R      Delete any snapshots and their clones
- *     -f      ignored for backwards compatability
+ *     -f      ignored for backwards compatibility
  *
  * Given a filesystem, rollback to a specific snapshot, discarding any changes
  * since then and making it the active dataset.  If more recent snapshots exist,
@@ -3410,7 +3468,7 @@ zfs_do_rollback(int argc, char **argv)
        boolean_t force = B_FALSE;
        rollback_cbdata_t cb = { 0 };
        zfs_handle_t *zhp, *snap;
-       char parentname[ZFS_MAXNAMELEN];
+       char parentname[ZFS_MAX_DATASET_NAME_LEN];
        char *delim;
 
        /* check options */
@@ -3638,8 +3696,11 @@ zfs_do_snapshot(int argc, char **argv)
        while ((c = getopt(argc, argv, "ro:")) != -1) {
                switch (c) {
                case 'o':
-                       if (parseprop(props, optarg) != 0)
+                       if (parseprop(props, optarg) != 0) {
+                               nvlist_free(sd.sd_nvl);
+                               nvlist_free(props);
                                return (1);
+                       }
                        break;
                case 'r':
                        sd.sd_recursive = B_TRUE;
@@ -3702,6 +3763,7 @@ zfs_do_send(int argc, char **argv)
 {
        char *fromname = NULL;
        char *toname = NULL;
+       char *resume_token = NULL;
        char *cp;
        zfs_handle_t *zhp;
        sendflags_t flags = { 0 };
@@ -3710,7 +3772,7 @@ zfs_do_send(int argc, char **argv)
        boolean_t extraverbose = B_FALSE;
 
        /* check options */
-       while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) {
+       while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:c")) != -1) {
                switch (c) {
                case 'i':
                        if (fromname)
@@ -3751,6 +3813,12 @@ zfs_do_send(int argc, char **argv)
                case 'e':
                        flags.embed_data = B_TRUE;
                        break;
+               case 't':
+                       resume_token = optarg;
+                       break;
+               case 'c':
+                       flags.compress = B_TRUE;
+                       break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
                            "'%c' option\n"), optopt);
@@ -3766,14 +3834,28 @@ zfs_do_send(int argc, char **argv)
        argc -= optind;
        argv += optind;
 
-       /* check number of arguments */
-       if (argc < 1) {
-               (void) fprintf(stderr, gettext("missing snapshot argument\n"));
-               usage(B_FALSE);
-       }
-       if (argc > 1) {
-               (void) fprintf(stderr, gettext("too many arguments\n"));
-               usage(B_FALSE);
+       if (resume_token != NULL) {
+               if (fromname != NULL || flags.replicate || flags.props ||
+                   flags.dedup) {
+                       (void) fprintf(stderr,
+                           gettext("invalid flags combined with -t\n"));
+                       usage(B_FALSE);
+               }
+               if (argc != 0) {
+                       (void) fprintf(stderr, gettext("no additional "
+                           "arguments are permitted with -t\n"));
+                       usage(B_FALSE);
+               }
+       } else {
+               if (argc < 1) {
+                       (void) fprintf(stderr,
+                           gettext("missing snapshot argument\n"));
+                       usage(B_FALSE);
+               }
+               if (argc > 1) {
+                       (void) fprintf(stderr, gettext("too many arguments\n"));
+                       usage(B_FALSE);
+               }
        }
 
        if (!flags.dryrun && isatty(STDOUT_FILENO)) {
@@ -3783,12 +3865,17 @@ zfs_do_send(int argc, char **argv)
                return (1);
        }
 
+       if (resume_token != NULL) {
+               return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
+                   resume_token));
+       }
+
        /*
         * Special case sending a filesystem, or from a bookmark.
         */
        if (strchr(argv[0], '@') == NULL ||
            (fromname && strchr(fromname, '#') != NULL)) {
-               char frombuf[ZFS_MAXNAMELEN];
+               char frombuf[ZFS_MAX_DATASET_NAME_LEN];
                enum lzc_send_flags lzc_flags = 0;
 
                if (flags.replicate || flags.doall || flags.props ||
@@ -3808,6 +3895,8 @@ zfs_do_send(int argc, char **argv)
                        lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
                if (flags.embed_data)
                        lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
+               if (flags.compress)
+                       lzc_flags |= LZC_SEND_FLAG_COMPRESS;
 
                if (fromname != NULL &&
                    (fromname[0] == '#' || fromname[0] == '@')) {
@@ -3815,7 +3904,7 @@ zfs_do_send(int argc, char **argv)
                         * Incremental source name begins with # or @.
                         * Default to same fs as target.
                         */
-                       (void) strncpy(frombuf, argv[0], sizeof (frombuf));
+                       (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
                        cp = strchr(frombuf, '@');
                        if (cp != NULL)
                                *cp = '\0';
@@ -3840,7 +3929,7 @@ zfs_do_send(int argc, char **argv)
         * case if they specify the origin.
         */
        if (fromname && (cp = strchr(fromname, '@')) != NULL) {
-               char origin[ZFS_MAXNAMELEN];
+               char origin[ZFS_MAX_DATASET_NAME_LEN];
                zprop_source_t src;
 
                (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
@@ -3888,15 +3977,15 @@ zfs_do_send(int argc, char **argv)
 }
 
 /*
- * zfs receive [-vnFu] [-d | -e] <fs@snap>
- *
  * Restore a backup stream from stdin.
  */
 static int
 zfs_do_receive(int argc, char **argv)
 {
-       int c, err;
+       int c, err = 0;
        recvflags_t flags = { 0 };
+       boolean_t abort_resumable = B_FALSE;
+
        nvlist_t *props;
        nvpair_t *nvp = NULL;
 
@@ -3904,11 +3993,13 @@ zfs_do_receive(int argc, char **argv)
                nomem();
 
        /* check options */
-       while ((c = getopt(argc, argv, ":o:denuvF")) != -1) {
+       while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
                switch (c) {
                case 'o':
-                       if (parseprop(props, optarg) != 0)
+                       if (parseprop(props, optarg) != 0) {
+                               nvlist_free(props);
                                return (1);
+                       }
                        break;
                case 'd':
                        flags.isprefix = B_TRUE;
@@ -3926,9 +4017,15 @@ zfs_do_receive(int argc, char **argv)
                case 'v':
                        flags.verbose = B_TRUE;
                        break;
+               case 's':
+                       flags.resumable = B_TRUE;
+                       break;
                case 'F':
                        flags.force = B_TRUE;
                        break;
+               case 'A':
+                       abort_resumable = B_TRUE;
+                       break;
                case ':':
                        (void) fprintf(stderr, gettext("missing argument for "
                            "'%c' option\n"), optopt);
@@ -3961,15 +4058,60 @@ zfs_do_receive(int argc, char **argv)
                }
        }
 
+       if (abort_resumable) {
+               if (flags.isprefix || flags.istail || flags.dryrun ||
+                   flags.resumable || flags.nomount) {
+                       (void) fprintf(stderr, gettext("invalid option"));
+                       usage(B_FALSE);
+               }
+
+               char namebuf[ZFS_MAX_DATASET_NAME_LEN];
+               (void) snprintf(namebuf, sizeof (namebuf),
+                   "%s/%%recv", argv[0]);
+
+               if (zfs_dataset_exists(g_zfs, namebuf,
+                   ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
+                       zfs_handle_t *zhp = zfs_open(g_zfs,
+                           namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
+                       if (zhp == NULL) {
+                               nvlist_free(props);
+                               return (1);
+                       }
+                       err = zfs_destroy(zhp, B_FALSE);
+                       zfs_close(zhp);
+               } else {
+                       zfs_handle_t *zhp = zfs_open(g_zfs,
+                           argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
+                       if (zhp == NULL)
+                               usage(B_FALSE);
+                       if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
+                           zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
+                           NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
+                               (void) fprintf(stderr,
+                                   gettext("'%s' does not have any "
+                                   "resumable receive state to abort\n"),
+                                   argv[0]);
+                               nvlist_free(props);
+                               zfs_close(zhp);
+                               return (1);
+                       }
+                       err = zfs_destroy(zhp, B_FALSE);
+                       zfs_close(zhp);
+               }
+               nvlist_free(props);
+               return (err != 0);
+       }
+
        if (isatty(STDIN_FILENO)) {
                (void) fprintf(stderr,
                    gettext("Error: Backup stream can not be read "
                    "from a terminal.\n"
                    "You must redirect standard input.\n"));
+               nvlist_free(props);
                return (1);
        }
-
        err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
+       nvlist_free(props);
 
        return (err != 0);
 }
@@ -3996,6 +4138,11 @@ zfs_do_receive(int argc, char **argv)
 #define        ZFS_DELEG_PERM_GROUPQUOTA       "groupquota"
 #define        ZFS_DELEG_PERM_USERUSED         "userused"
 #define        ZFS_DELEG_PERM_GROUPUSED        "groupused"
+#define        ZFS_DELEG_PERM_USEROBJQUOTA     "userobjquota"
+#define        ZFS_DELEG_PERM_GROUPOBJQUOTA    "groupobjquota"
+#define        ZFS_DELEG_PERM_USEROBJUSED      "userobjused"
+#define        ZFS_DELEG_PERM_GROUPOBJUSED     "groupobjused"
+
 #define        ZFS_DELEG_PERM_HOLD             "hold"
 #define        ZFS_DELEG_PERM_RELEASE          "release"
 #define        ZFS_DELEG_PERM_DIFF             "diff"
@@ -4026,6 +4173,10 @@ static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
        { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
        { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
        { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
+       { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
+       { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
+       { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
+       { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
        { NULL, ZFS_DELEG_NOTE_NONE }
 };
 
@@ -4103,6 +4254,10 @@ deleg_perm_type(zfs_deleg_note_t note)
        case ZFS_DELEG_NOTE_USERPROP:
        case ZFS_DELEG_NOTE_USERQUOTA:
        case ZFS_DELEG_NOTE_USERUSED:
+       case ZFS_DELEG_NOTE_USEROBJQUOTA:
+       case ZFS_DELEG_NOTE_USEROBJUSED:
+       case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
+       case ZFS_DELEG_NOTE_GROUPOBJUSED:
                /* other */
                return (gettext("other"));
        default:
@@ -4110,7 +4265,7 @@ deleg_perm_type(zfs_deleg_note_t note)
        }
 }
 
-static int inline
+static int
 who_type2weight(zfs_deleg_who_type_t who_type)
 {
        int res;
@@ -4330,7 +4485,7 @@ fs_perm_fini(fs_perm_t *fsperm)
        uu_avl_destroy(fsperm->fsp_uge_avl);
 }
 
-static void inline
+static void
 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
     zfs_deleg_who_type_t who_type, const char *name, char locality)
 {
@@ -4427,8 +4582,9 @@ parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
                        avl_pool = fspset->fsps_who_perm_avl_pool;
                        avl = fsperm->fsp_uge_avl;
                        break;
+
                default:
-                       break;
+                       assert(!"unhandled zfs_deleg_who_type_t");
                }
 
                if (is_set) {
@@ -4464,6 +4620,7 @@ parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
                                                if (g)
                                                        nice_name = g->gr_name;
                                                break;
+
                                        default:
                                                break;
                                        }
@@ -4480,7 +4637,7 @@ parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
                                who_perm = &node->who_perm;
                        }
                }
-
+               VERIFY3P(who_perm, !=, NULL);
                (void) parse_who_perm(who_perm, nvl2, perm_locality);
        }
 
@@ -4606,6 +4763,19 @@ deleg_perm_comment(zfs_deleg_note_t note)
        case ZFS_DELEG_NOTE_USERUSED:
                str = gettext("Allows reading any userused@... property");
                break;
+       case ZFS_DELEG_NOTE_USEROBJQUOTA:
+               str = gettext("Allows accessing any userobjquota@... property");
+               break;
+       case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
+               str = gettext("Allows accessing any \n\t\t\t\t"
+                   "groupobjquota@... property");
+               break;
+       case ZFS_DELEG_NOTE_GROUPOBJUSED:
+               str = gettext("Allows reading any groupobjused@... property");
+               break;
+       case ZFS_DELEG_NOTE_USEROBJUSED:
+               str = gettext("Allows reading any userobjused@... property");
+               break;
                /* other */
        default:
                str = "";
@@ -4666,9 +4836,9 @@ allow_usage(boolean_t un, boolean_t requested, const char *msg)
        (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
            HELP_ALLOW));
        (void) fprintf(fp, gettext("Options:\n"));
-       for (i = 0; i < (un ? unallow_size : allow_size); i++) {
-               const char *opt = opt_desc[i++];
-               const char *optdsc = opt_desc[i];
+       for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
+               const char *opt = opt_desc[i];
+               const char *optdsc = opt_desc[i + 1];
                (void) fprintf(fp, gettext("  %-10s  %s\n"), opt, optdsc);
        }
 
@@ -4787,9 +4957,9 @@ store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
 {
        int i;
        char ld[2] = { '\0', '\0' };
-       char who_buf[ZFS_MAXNAMELEN+32];
-       char base_type = ZFS_DELEG_WHO_UNKNOWN;
-       char set_type = ZFS_DELEG_WHO_UNKNOWN;
+       char who_buf[MAXNAMELEN + 32];
+       char base_type = '\0';
+       char set_type = '\0';
        nvlist_t *base_nvl = NULL;
        nvlist_t *set_nvl = NULL;
        nvlist_t *nvl;
@@ -4838,8 +5008,10 @@ store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
                        ld[0] = ZFS_DELEG_LOCAL;
                if (descend)
                        ld[1] = ZFS_DELEG_DESCENDENT;
-       default:
                break;
+
+       default:
+               assert(set_type != '\0' && base_type != '\0');
        }
 
        if (perms != NULL) {
@@ -5073,7 +5245,7 @@ print_set_creat_perms(uu_avl_t *who_avl)
        }
 }
 
-static void inline
+static void
 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
     const char *title)
 {
@@ -5124,8 +5296,10 @@ print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
                                case ZFS_DELEG_EVERYONE:
                                        who = gettext("everyone");
                                        who_name = NULL;
-                               default:
                                        break;
+
+                               default:
+                                       assert(who != NULL);
                                }
 
                                prt_who = B_FALSE;
@@ -5151,7 +5325,7 @@ static void
 print_fs_perms(fs_perm_set_t *fspset)
 {
        fs_perm_node_t *node = NULL;
-       char buf[ZFS_MAXNAMELEN+32];
+       char buf[MAXNAMELEN + 32];
        const char *dsname = buf;
 
        for (node = uu_list_first(fspset->fsps_list); node != NULL;
@@ -5160,7 +5334,7 @@ print_fs_perms(fs_perm_set_t *fspset)
                uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
                int left = 0;
 
-               (void) snprintf(buf, ZFS_MAXNAMELEN+32,
+               (void) snprintf(buf, sizeof (buf),
                    gettext("---- Permissions on %s "),
                    node->fspn_fsperm.fsp_name);
                (void) printf("%s", dsname);
@@ -5357,7 +5531,7 @@ zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
 
        for (i = 0; i < argc; ++i) {
                zfs_handle_t *zhp;
-               char parent[ZFS_MAXNAMELEN];
+               char parent[ZFS_MAX_DATASET_NAME_LEN];
                const char *delim;
                char *path = argv[i];
 
@@ -5485,7 +5659,7 @@ holds_callback(zfs_handle_t *zhp, void *data)
        nvlist_t *nvl = NULL;
        nvpair_t *nvp = NULL;
        const char *zname = zfs_get_name(zhp);
-       size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
+       size_t znamelen = strlen(zname);
 
        if (cbp->cb_recursive) {
                const char *snapname;
@@ -5506,7 +5680,7 @@ holds_callback(zfs_handle_t *zhp, void *data)
 
        while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
                const char *tag = nvpair_name(nvp);
-               size_t taglen = strnlen(tag, MAXNAMELEN);
+               size_t taglen = strlen(tag);
                if (taglen > cbp->cb_max_taglen)
                        cbp->cb_max_taglen  = taglen;
        }
@@ -5798,6 +5972,24 @@ share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
                return (0);
        }
 
+       /*
+        * If this filesystem is inconsistent and has a receive resume
+        * token, we can not mount it.
+        */
+       if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
+           zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
+           NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
+               if (!explicit)
+                       return (0);
+
+               (void) fprintf(stderr, gettext("cannot %s '%s': "
+                   "Contains partially-completed state from "
+                   "\"zfs receive -r\", which can be resumed with "
+                   "\"zfs send -t\"\n"),
+                   cmdname, zfs_get_name(zhp));
+               return (1);
+       }
+
        /*
         * At this point, we have verified that the mountpoint and/or
         * shareopts are appropriate for auto management. If the
@@ -5812,10 +6004,10 @@ share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
                shared_smb = zfs_is_shared_smb(zhp, NULL);
 
                if ((shared_nfs && shared_smb) ||
-                   ((shared_nfs && strcmp(shareopts, "on") == 0) &&
-                   (strcmp(smbshareopts, "off") == 0)) ||
-                   ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
-                   (strcmp(shareopts, "off") == 0))) {
+                   (shared_nfs && strcmp(shareopts, "on") == 0 &&
+                   strcmp(smbshareopts, "off") == 0) ||
+                   (shared_smb && strcmp(smbshareopts, "on") == 0 &&
+                   strcmp(shareopts, "off") == 0)) {
                        if (!explicit)
                                return (0);
 
@@ -5999,8 +6191,11 @@ share_mount(int op, int argc, char **argv)
                start_progress_timer();
                get_all_datasets(&dslist, &count, verbose);
 
-               if (count == 0)
+               if (count == 0) {
+                       if (options != NULL)
+                               free(options);
                        return (0);
+               }
 
                qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
 
@@ -6025,14 +6220,18 @@ share_mount(int op, int argc, char **argv)
                }
 
                /*
-                * When mount is given no arguments, go through /etc/mtab and
-                * display any active ZFS mounts.  We hide any snapshots, since
-                * they are controlled automatically.
+                * When mount is given no arguments, go through
+                * /proc/self/mounts and display any active ZFS mounts.
+                * We hide any snapshots, since they are controlled
+                * automatically.
                 */
 
                /* Reopen MNTTAB to prevent reading stale data from open file */
-               if (freopen(MNTTAB, "r", mnttab_file) == NULL)
+               if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
+                       if (options != NULL)
+                               free(options);
                        return (ENOENT);
+               }
 
                while (getmntent(mnttab_file, &entry) == 0) {
                        if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
@@ -6062,6 +6261,9 @@ share_mount(int op, int argc, char **argv)
                }
        }
 
+       if (options != NULL)
+               free(options);
+
        return (ret);
 }
 
@@ -6107,8 +6309,8 @@ unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
 
 /*
  * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
- * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
- * and unmount it appropriately.
+ * absolute path, find the entry /proc/self/mounts, verify that its a
+ * ZFS filesystems, and unmount it appropriately.
  */
 static int
 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
@@ -6121,7 +6323,7 @@ unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
        ino_t path_inode;
 
        /*
-        * Search for the path in /etc/mtab.  Rather than looking for the
+        * Search for the path in /proc/self/mounts. Rather than looking for the
         * specific path, which can be fooled by non-standard paths (i.e. ".."
         * or "//"), we stat() the path and search for the corresponding
         * (major,minor) device pair.
@@ -6152,8 +6354,8 @@ unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
                            "currently mounted\n"), cmdname, path);
                        return (1);
                }
-               (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
-                   path);
+               (void) fprintf(stderr, gettext("warning: %s not in"
+                   "/proc/self/mounts\n"), path);
                if ((ret = umount2(path, flags)) != 0)
                        (void) fprintf(stderr, gettext("%s: %s\n"), path,
                            strerror(errno));
@@ -6242,7 +6444,7 @@ unshare_unmount(int op, int argc, char **argv)
        char sharesmb[ZFS_MAXPROPLEN];
 
        /* check options */
-       while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
+       while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
                switch (c) {
                case 'a':
                        do_all = 1;
@@ -6250,6 +6452,11 @@ unshare_unmount(int op, int argc, char **argv)
                case 'f':
                        flags = MS_FORCE;
                        break;
+               case ':':
+                       (void) fprintf(stderr, gettext("missing argument for "
+                           "'%c' option\n"), optopt);
+                       usage(B_FALSE);
+                       break;
                case '?':
                        (void) fprintf(stderr, gettext("invalid option '%c'\n"),
                            optopt);
@@ -6264,9 +6471,9 @@ unshare_unmount(int op, int argc, char **argv)
                /*
                 * We could make use of zfs_for_each() to walk all datasets in
                 * the system, but this would be very inefficient, especially
-                * since we would have to linearly search /etc/mtab for each
-                * one.  Instead, do one pass through /etc/mtab looking for
-                * zfs entries and call zfs_unmount() for each one.
+                * since we would have to linearly search /proc/self/mounts for
+                * each one. Instead, do one pass through /proc/self/mounts
+                * looking for zfs entries and call zfs_unmount() for each one.
                 *
                 * Things get a little tricky if the administrator has created
                 * mountpoints beneath other ZFS filesystems.  In this case, we
@@ -6281,6 +6488,19 @@ unshare_unmount(int op, int argc, char **argv)
                unshare_unmount_node_t *node;
                uu_avl_index_t idx;
                uu_avl_walk_t *walk;
+               char *protocol = NULL;
+
+               if (op == OP_SHARE && argc > 0) {
+                       if (strcmp(argv[0], "nfs") != 0 &&
+                           strcmp(argv[0], "smb") != 0) {
+                               (void) fprintf(stderr, gettext("share type "
+                                   "must be 'nfs' or 'smb'\n"));
+                               usage(B_FALSE);
+                       }
+                       protocol = argv[0];
+                       argc--;
+                       argv++;
+               }
 
                if (argc != 0) {
                        (void) fprintf(stderr, gettext("too many arguments\n"));
@@ -6373,8 +6593,8 @@ unshare_unmount(int op, int argc, char **argv)
 
                        switch (op) {
                        case OP_SHARE:
-                               if (zfs_unshareall_bypath(node->un_zhp,
-                                   node->un_mountp) != 0)
+                               if (zfs_unshareall_bytype(node->un_zhp,
+                                   node->un_mountp, protocol) != 0)
                                        ret = 1;
                                break;
 
@@ -6575,12 +6795,13 @@ zfs_do_diff(int argc, char **argv)
        if (copy == NULL)
                usage(B_FALSE);
 
-       if ((atp = strchr(copy, '@')))
+       if ((atp = strchr(copy, '@')) != NULL)
                *atp = '\0';
 
-       if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
+       if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
+               free(copy);
                return (1);
-
+       }
        free(copy);
 
        /*
@@ -6604,7 +6825,7 @@ zfs_do_diff(int argc, char **argv)
 static int
 zfs_do_bookmark(int argc, char **argv)
 {
-       char snapname[ZFS_MAXNAMELEN];
+       char snapname[ZFS_MAX_DATASET_NAME_LEN];
        zfs_handle_t *zhp;
        nvlist_t *nvl;
        int ret = 0;
@@ -6645,11 +6866,11 @@ zfs_do_bookmark(int argc, char **argv)
                 * Snapshot name begins with @.
                 * Default to same fs as bookmark.
                 */
-               (void) strncpy(snapname, argv[1], sizeof (snapname));
+               (void) strlcpy(snapname, argv[1], sizeof (snapname));
                *strchr(snapname, '#') = '\0';
                (void) strlcat(snapname, argv[0], sizeof (snapname));
        } else {
-               (void) strncpy(snapname, argv[0], sizeof (snapname));
+               (void) strlcpy(snapname, argv[0], sizeof (snapname));
        }
        zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
        if (zhp == NULL)