+ * The filesystem is mounted by invoking the system mount utility rather
+ * than by the system call mount(2). This ensures that the /etc/mtab
+ * file is correctly locked for the update. Performing our own locking
+ * and /etc/mtab update requires making an unsafe assumption about how
+ * the mount utility performs its locking. Unfortunately, this also means
+ * in the case of a mount failure we do not have the exact errno. We must
+ * make due with return value from the mount process.
+ *
+ * In the long term a shared library called libmount is under development
+ * which provides a common API to address the locking and errno issues.
+ * Once the standard mount utility has been updated to use this library
+ * we can add an autoconf check to conditionally use it.
+ *
+ * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
+ */
+
+static int
+do_mount(const char *src, const char *mntpt, char *opts)
+{
+ char *argv[8] = {
+ "/bin/mount",
+ "-t", MNTTYPE_ZFS,
+ "-o", opts,
+ (char *)src,
+ (char *)mntpt,
+ (char *)NULL };
+ int rc;
+
+ /* Return only the most critical mount error */
+ rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
+ if (rc) {
+ if (rc & MOUNT_FILEIO)
+ return EIO;
+ if (rc & MOUNT_USER)
+ return EINTR;
+ if (rc & MOUNT_SOFTWARE)
+ return EPIPE;
+ if (rc & MOUNT_SYSERR)
+ return EAGAIN;
+ if (rc & MOUNT_USAGE)
+ return EINVAL;
+
+ return ENXIO; /* Generic error */
+ }
+
+ return 0;
+}
+
+static int
+do_unmount(const char *mntpt, int flags)
+{
+ char force_opt[] = "-f";
+ char lazy_opt[] = "-l";
+ char *argv[7] = {
+ "/bin/umount",
+ "-t", MNTTYPE_ZFS,
+ NULL, NULL, NULL, NULL };
+ int rc, count = 3;
+
+ if (flags & MS_FORCE) {
+ argv[count] = force_opt;
+ count++;
+ }
+
+ if (flags & MS_DETACH) {
+ argv[count] = lazy_opt;
+ count++;
+ }
+
+ argv[count] = (char *)mntpt;
+ rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
+
+ return (rc ? EINVAL : 0);
+}
+
+static int
+zfs_add_option(zfs_handle_t *zhp, char *options, int len,
+ zfs_prop_t prop, char *on, char *off)
+{
+ char *source;
+ uint64_t value;
+
+ /* Skip adding duplicate default options */
+ if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
+ return (0);
+
+ /*
+ * zfs_prop_get_int() to not used to ensure our mount options
+ * are not influenced by the current /etc/mtab contents.
+ */
+ value = getprop_uint64(zhp, prop, &source);
+
+ (void) strlcat(options, ",", len);
+ (void) strlcat(options, value ? on : off, len);
+
+ return (0);
+}
+
+static int
+zfs_add_options(zfs_handle_t *zhp, char *options, int len)
+{
+ int error = 0;
+
+ error = zfs_add_option(zhp, options, len,
+ ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_XATTR, MNTOPT_XATTR, MNTOPT_NOXATTR);
+ error = error ? error : zfs_add_option(zhp, options, len,
+ ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
+
+ return (error);
+}
+
+/*