diff options
Diffstat (limited to 'lib/libzfs/os/linux')
-rw-r--r-- | lib/libzfs/os/linux/libzfs_mount_os.c | 431 | ||||
-rw-r--r-- | lib/libzfs/os/linux/libzfs_pool_os.c | 352 | ||||
-rw-r--r-- | lib/libzfs/os/linux/libzfs_util_os.c | 275 |
3 files changed, 0 insertions, 1058 deletions
diff --git a/lib/libzfs/os/linux/libzfs_mount_os.c b/lib/libzfs/os/linux/libzfs_mount_os.c deleted file mode 100644 index 585d22d9e5b3..000000000000 --- a/lib/libzfs/os/linux/libzfs_mount_os.c +++ /dev/null @@ -1,431 +0,0 @@ -// SPDX-License-Identifier: CDDL-1.0 -/* - * CDDL HEADER START - * - * The contents of this file are subject to the terms of the - * Common Development and Distribution License (the "License"). - * You may not use this file except in compliance with the License. - * - * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE - * or https://opensource.org/licenses/CDDL-1.0. - * See the License for the specific language governing permissions - * and limitations under the License. - * - * When distributing Covered Code, include this CDDL HEADER in each - * file and include the License file at usr/src/OPENSOLARIS.LICENSE. - * If applicable, add the following below this CDDL HEADER, with the - * fields enclosed by brackets "[]" replaced with your own identifying - * information: Portions Copyright [yyyy] [name of copyright owner] - * - * CDDL HEADER END - */ - -/* - * Copyright 2015 Nexenta Systems, Inc. All rights reserved. - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2014, 2021 by Delphix. All rights reserved. - * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> - * Copyright 2017 RackTop Systems. - * Copyright (c) 2018 Datto Inc. - * Copyright 2018 OmniOS Community Edition (OmniOSce) Association. - */ - -#include <dirent.h> -#include <dlfcn.h> -#include <errno.h> -#include <fcntl.h> -#include <libgen.h> -#include <libintl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <zone.h> -#include <sys/mntent.h> -#include <sys/mount.h> -#include <sys/stat.h> -#include <sys/vfs.h> -#include <sys/dsl_crypt.h> -#include <libzfs.h> - -#include "../../libzfs_impl.h" -#include <thread_pool.h> - -#define ZS_COMMENT 0x00000000 /* comment */ -#define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */ - -typedef struct option_map { - const char *name; - unsigned long mntmask; - unsigned long zfsmask; -} option_map_t; - -static const option_map_t option_map[] = { - /* Canonicalized filesystem independent options from mount(8) */ - { MNTOPT_NOAUTO, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_DEFAULTS, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_NODEVICES, MS_NODEV, ZS_COMMENT }, - { MNTOPT_DEVICES, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_DIRSYNC, MS_DIRSYNC, ZS_COMMENT }, - { MNTOPT_NOEXEC, MS_NOEXEC, ZS_COMMENT }, - { MNTOPT_EXEC, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_GROUP, MS_GROUP, ZS_COMMENT }, - { MNTOPT_NETDEV, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_NOFAIL, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_NOSUID, MS_NOSUID, ZS_COMMENT }, - { MNTOPT_SUID, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_OWNER, MS_OWNER, ZS_COMMENT }, - { MNTOPT_REMOUNT, MS_REMOUNT, ZS_COMMENT }, - { MNTOPT_RO, MS_RDONLY, ZS_COMMENT }, - { MNTOPT_RW, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_SYNC, MS_SYNCHRONOUS, ZS_COMMENT }, - { MNTOPT_USER, MS_USERS, ZS_COMMENT }, - { MNTOPT_USERS, MS_USERS, ZS_COMMENT }, - /* acl flags passed with util-linux-2.24 mount command */ - { MNTOPT_ACL, MS_POSIXACL, ZS_COMMENT }, - { MNTOPT_NOACL, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_POSIXACL, MS_POSIXACL, ZS_COMMENT }, - /* - * Case sensitive options are just listed here to silently - * ignore the error if passed with zfs mount command. - */ - { MNTOPT_CASESENSITIVE, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_CASEINSENSITIVE, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_CASEMIXED, MS_COMMENT, ZS_COMMENT }, -#ifdef MS_NOATIME - { MNTOPT_NOATIME, MS_NOATIME, ZS_COMMENT }, - { MNTOPT_ATIME, MS_COMMENT, ZS_COMMENT }, -#endif -#ifdef MS_NODIRATIME - { MNTOPT_NODIRATIME, MS_NODIRATIME, ZS_COMMENT }, - { MNTOPT_DIRATIME, MS_COMMENT, ZS_COMMENT }, -#endif -#ifdef MS_RELATIME - { MNTOPT_RELATIME, MS_RELATIME, ZS_COMMENT }, - { MNTOPT_NORELATIME, MS_COMMENT, ZS_COMMENT }, -#endif -#ifdef MS_STRICTATIME - { MNTOPT_STRICTATIME, MS_STRICTATIME, ZS_COMMENT }, - { MNTOPT_NOSTRICTATIME, MS_COMMENT, ZS_COMMENT }, -#endif -#ifdef MS_LAZYTIME - { MNTOPT_LAZYTIME, MS_LAZYTIME, ZS_COMMENT }, -#endif - { MNTOPT_CONTEXT, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_FSCONTEXT, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_DEFCONTEXT, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_ROOTCONTEXT, MS_COMMENT, ZS_COMMENT }, -#ifdef MS_I_VERSION - { MNTOPT_IVERSION, MS_I_VERSION, ZS_COMMENT }, -#endif -#ifdef MS_MANDLOCK - { MNTOPT_NBMAND, MS_MANDLOCK, ZS_COMMENT }, - { MNTOPT_NONBMAND, MS_COMMENT, ZS_COMMENT }, -#endif - /* Valid options not found in mount(8) */ - { MNTOPT_BIND, MS_BIND, ZS_COMMENT }, -#ifdef MS_REC - { MNTOPT_RBIND, MS_BIND|MS_REC, ZS_COMMENT }, -#endif - { MNTOPT_COMMENT, MS_COMMENT, ZS_COMMENT }, -#ifdef MS_NOSUB - { MNTOPT_NOSUB, MS_NOSUB, ZS_COMMENT }, -#endif -#ifdef MS_SILENT - { MNTOPT_QUIET, MS_SILENT, ZS_COMMENT }, -#endif - /* Custom zfs options */ - { MNTOPT_XATTR, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_NOXATTR, MS_COMMENT, ZS_COMMENT }, - { MNTOPT_ZFSUTIL, MS_COMMENT, ZS_ZFSUTIL }, - { NULL, 0, 0 } }; - -/* - * Break the mount option in to a name/value pair. The name is - * validated against the option map and mount flags set accordingly. - */ -static int -parse_option(char *mntopt, unsigned long *mntflags, - unsigned long *zfsflags, int sloppy) -{ - const option_map_t *opt; - char *ptr, *name, *value = NULL; - int error = 0; - - name = strdup(mntopt); - if (name == NULL) - return (ENOMEM); - - for (ptr = name; ptr && *ptr; ptr++) { - if (*ptr == '=') { - *ptr = '\0'; - value = ptr+1; - VERIFY3P(value, !=, NULL); - break; - } - } - - for (opt = option_map; opt->name != NULL; opt++) { - if (strncmp(name, opt->name, strlen(name)) == 0) { - *mntflags |= opt->mntmask; - *zfsflags |= opt->zfsmask; - error = 0; - goto out; - } - } - - if (!sloppy) - error = ENOENT; -out: - /* If required further process on the value may be done here */ - free(name); - return (error); -} - -/* - * Translate the mount option string in to MS_* mount flags for the - * kernel vfs. When sloppy is non-zero unknown options will be ignored - * otherwise they are considered fatal are copied in to badopt. - */ -int -zfs_parse_mount_options(const char *mntopts, unsigned long *mntflags, - unsigned long *zfsflags, int sloppy, char *badopt, char *mtabopt) -{ - int error = 0, quote = 0, flag = 0, count = 0; - char *ptr, *opt, *opts; - - opts = strdup(mntopts); - if (opts == NULL) - return (ENOMEM); - - *mntflags = 0; - opt = NULL; - - /* - * Scan through all mount options which must be comma delimited. - * We must be careful to notice regions which are double quoted - * and skip commas in these regions. Each option is then checked - * to determine if it is a known option. - */ - for (ptr = opts; ptr && !flag; ptr++) { - if (opt == NULL) - opt = ptr; - - if (*ptr == '"') - quote = !quote; - - if (quote) - continue; - - if (*ptr == '\0') - flag = 1; - - if ((*ptr == ',') || (*ptr == '\0')) { - *ptr = '\0'; - - error = parse_option(opt, mntflags, zfsflags, sloppy); - if (error) { - strcpy(badopt, opt); - goto out; - - } - - if (!(*mntflags & MS_REMOUNT) && - !(*zfsflags & ZS_ZFSUTIL) && - mtabopt != NULL) { - if (count > 0) - strlcat(mtabopt, ",", MNT_LINE_MAX); - - strlcat(mtabopt, opt, MNT_LINE_MAX); - count++; - } - - opt = NULL; - } - } - -out: - free(opts); - return (error); -} - -static void -append_mntopt(const char *name, const char *val, char *mntopts, - char *mtabopt, boolean_t quote) -{ - char tmp[MNT_LINE_MAX]; - - snprintf(tmp, MNT_LINE_MAX, quote ? ",%s=\"%s\"" : ",%s=%s", name, val); - - if (mntopts) - strlcat(mntopts, tmp, MNT_LINE_MAX); - - if (mtabopt) - strlcat(mtabopt, tmp, MNT_LINE_MAX); -} - -static void -zfs_selinux_setcontext(zfs_handle_t *zhp, zfs_prop_t zpt, const char *name, - char *mntopts, char *mtabopt) -{ - char context[ZFS_MAXPROPLEN]; - - if (zfs_prop_get(zhp, zpt, context, sizeof (context), - NULL, NULL, 0, B_FALSE) == 0) { - if (strcmp(context, "none") != 0) - append_mntopt(name, context, mntopts, mtabopt, B_TRUE); - } -} - -void -zfs_adjust_mount_options(zfs_handle_t *zhp, const char *mntpoint, - char *mntopts, char *mtabopt) -{ - char prop[ZFS_MAXPROPLEN]; - - /* - * Checks to see if the ZFS_PROP_SELINUX_CONTEXT exists - * if it does, create a tmp variable in case it's needed - * checks to see if the selinux context is set to the default - * if it is, allow the setting of the other context properties - * this is needed because the 'context' property overrides others - * if it is not the default, set the 'context' property - */ - if (zfs_prop_get(zhp, ZFS_PROP_SELINUX_CONTEXT, prop, sizeof (prop), - NULL, NULL, 0, B_FALSE) == 0) { - if (strcmp(prop, "none") == 0) { - zfs_selinux_setcontext(zhp, ZFS_PROP_SELINUX_FSCONTEXT, - MNTOPT_FSCONTEXT, mntopts, mtabopt); - zfs_selinux_setcontext(zhp, ZFS_PROP_SELINUX_DEFCONTEXT, - MNTOPT_DEFCONTEXT, mntopts, mtabopt); - zfs_selinux_setcontext(zhp, - ZFS_PROP_SELINUX_ROOTCONTEXT, MNTOPT_ROOTCONTEXT, - mntopts, mtabopt); - } else { - append_mntopt(MNTOPT_CONTEXT, prop, - mntopts, mtabopt, B_TRUE); - } - } - - /* A hint used to determine an auto-mounted snapshot mount point */ - append_mntopt(MNTOPT_MNTPOINT, mntpoint, mntopts, NULL, B_FALSE); -} - -/* - * By default the filesystem by preparing the mount options (i.e. parsing - * some flags from the "opts" parameter into the "flags" parameter) and then - * directly calling the system call mount(2). We don't need the mount utility - * or update /etc/mtab, because this is a symlink on all modern systems. - * - * If the environment variable ZFS_MOUNT_HELPER is set, we fall back to the - * previous behavior: - * 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. - */ -int -do_mount(zfs_handle_t *zhp, const char *mntpt, const char *opts, int flags) -{ - const char *src = zfs_get_name(zhp); - int error = 0; - - if (!libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) { - char badopt[MNT_LINE_MAX] = {0}; - unsigned long mntflags = flags, zfsflags = 0; - char myopts[MNT_LINE_MAX] = {0}; - - if (zfs_parse_mount_options(opts, &mntflags, - &zfsflags, 0, badopt, NULL)) { - return (EINVAL); - } - strlcat(myopts, opts, MNT_LINE_MAX); - zfs_adjust_mount_options(zhp, mntpt, myopts, NULL); - if (mount(src, mntpt, MNTTYPE_ZFS, mntflags, myopts)) { - return (errno); - } - } else { - char *argv[9] = { - (char *)"/bin/mount", - (char *)"--no-canonicalize", - (char *)"-t", (char *)MNTTYPE_ZFS, - (char *)"-o", (char *)opts, - (char *)src, - (char *)mntpt, - (char *)NULL }; - - /* Return only the most critical mount error */ - error = libzfs_run_process(argv[0], argv, - STDOUT_VERBOSE|STDERR_VERBOSE); - if (error) { - if (error & MOUNT_FILEIO) { - error = EIO; - } else if (error & MOUNT_USER) { - error = EINTR; - } else if (error & MOUNT_SOFTWARE) { - error = EPIPE; - } else if (error & MOUNT_BUSY) { - error = EBUSY; - } else if (error & MOUNT_SYSERR) { - error = EAGAIN; - } else if (error & MOUNT_USAGE) { - error = EINVAL; - } else - error = ENXIO; /* Generic error */ - } - } - - return (error); -} - -int -do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags) -{ - (void) zhp; - - if (!libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) { - int rv = umount2(mntpt, flags); - - return (rv < 0 ? errno : 0); - } - - char *argv[7] = { - (char *)"/bin/umount", - (char *)"-t", (char *)MNTTYPE_ZFS, - NULL, NULL, NULL, NULL }; - int rc, count = 3; - - if (flags & MS_FORCE) - argv[count++] = (char *)"-f"; - - if (flags & MS_DETACH) - argv[count++] = (char *)"-l"; - - argv[count] = (char *)mntpt; - rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE); - - return (rc ? EINVAL : 0); -} - -int -zfs_mount_delegation_check(void) -{ - return ((geteuid() != 0) ? EACCES : 0); -} - -/* Called from the tail end of zpool_disable_datasets() */ -void -zpool_disable_datasets_os(zpool_handle_t *zhp, boolean_t force) -{ - (void) zhp, (void) force; -} - -/* Called from the tail end of zfs_unmount() */ -void -zpool_disable_volume_os(const char *name) -{ - (void) name; -} diff --git a/lib/libzfs/os/linux/libzfs_pool_os.c b/lib/libzfs/os/linux/libzfs_pool_os.c deleted file mode 100644 index aef169a3f880..000000000000 --- a/lib/libzfs/os/linux/libzfs_pool_os.c +++ /dev/null @@ -1,352 +0,0 @@ -// SPDX-License-Identifier: CDDL-1.0 -/* - * CDDL HEADER START - * - * The contents of this file are subject to the terms of the - * Common Development and Distribution License (the "License"). - * You may not use this file except in compliance with the License. - * - * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE - * or https://opensource.org/licenses/CDDL-1.0. - * See the License for the specific language governing permissions - * and limitations under the License. - * - * When distributing Covered Code, include this CDDL HEADER in each - * file and include the License file at usr/src/OPENSOLARIS.LICENSE. - * If applicable, add the following below this CDDL HEADER, with the - * fields enclosed by brackets "[]" replaced with your own identifying - * information: Portions Copyright [yyyy] [name of copyright owner] - * - * CDDL HEADER END - */ - -/* - * Copyright 2015 Nexenta Systems, Inc. All rights reserved. - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2011, 2018 by Delphix. All rights reserved. - * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> - * Copyright (c) 2018 Datto Inc. - * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. - * Copyright (c) 2017, Intel Corporation. - * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com> - */ - -#include <errno.h> -#include <libintl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <libgen.h> -#include <zone.h> -#include <sys/stat.h> -#include <sys/efi_partition.h> -#include <sys/systeminfo.h> -#include <sys/zfs_ioctl.h> -#include <sys/vdev_disk.h> -#include <dlfcn.h> -#include <libzutil.h> - -#include "zfs_namecheck.h" -#include "zfs_prop.h" -#include "../../libzfs_impl.h" -#include "zfs_comutil.h" -#include "zfeature_common.h" - -/* - * If the device has being dynamically expanded then we need to relabel - * the disk to use the new unallocated space. - */ -int -zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg) -{ - int fd, error; - - if ((fd = open(path, O_RDWR|O_DIRECT|O_CLOEXEC)) < 0) { - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " - "relabel '%s': unable to open device: %d"), path, errno); - return (zfs_error(hdl, EZFS_OPENFAILED, msg)); - } - - /* - * It's possible that we might encounter an error if the device - * does not have any unallocated space left. If so, we simply - * ignore that error and continue on. - */ - error = efi_use_whole_disk(fd); - - /* Flush the buffers to disk and invalidate the page cache. */ - (void) fsync(fd); - (void) ioctl(fd, BLKFLSBUF); - - (void) close(fd); - if (error && error != VT_ENOSPC) { - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " - "relabel '%s': unable to read disk capacity"), path); - return (zfs_error(hdl, EZFS_NOCAP, msg)); - } - return (0); -} - -/* - * Read the EFI label from the config, if a label does not exist then - * pass back the error to the caller. If the caller has passed a non-NULL - * diskaddr argument then we set it to the starting address of the EFI - * partition. - */ -static int -read_efi_label(nvlist_t *config, diskaddr_t *sb) -{ - const char *path; - int fd; - char diskname[MAXPATHLEN]; - int err = -1; - - if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) - return (err); - - (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT, - strrchr(path, '/')); - if ((fd = open(diskname, O_RDONLY|O_DIRECT|O_CLOEXEC)) >= 0) { - struct dk_gpt *vtoc; - - if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { - if (sb != NULL) - *sb = vtoc->efi_parts[0].p_start; - efi_free(vtoc); - } - (void) close(fd); - } - return (err); -} - -/* - * determine where a partition starts on a disk in the current - * configuration - */ -static diskaddr_t -find_start_block(nvlist_t *config) -{ - nvlist_t **child; - uint_t c, children; - diskaddr_t sb = MAXOFFSET_T; - uint64_t wholedisk; - - if (nvlist_lookup_nvlist_array(config, - ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { - if (nvlist_lookup_uint64(config, - ZPOOL_CONFIG_WHOLE_DISK, - &wholedisk) != 0 || !wholedisk) { - return (MAXOFFSET_T); - } - if (read_efi_label(config, &sb) < 0) - sb = MAXOFFSET_T; - return (sb); - } - - for (c = 0; c < children; c++) { - sb = find_start_block(child[c]); - if (sb != MAXOFFSET_T) { - return (sb); - } - } - return (MAXOFFSET_T); -} - -static int -zpool_label_disk_check(char *path) -{ - struct dk_gpt *vtoc; - int fd, err; - - if ((fd = open(path, O_RDONLY|O_DIRECT|O_CLOEXEC)) < 0) - return (errno); - - if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) { - (void) close(fd); - return (err); - } - - if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) { - efi_free(vtoc); - (void) close(fd); - return (EIDRM); - } - - efi_free(vtoc); - (void) close(fd); - return (0); -} - -/* - * Generate a unique partition name for the ZFS member. Partitions must - * have unique names to ensure udev will be able to create symlinks under - * /dev/disk/by-partlabel/ for all pool members. The partition names are - * of the form <pool>-<unique-id>. - */ -static void -zpool_label_name(char *label_name, int label_size) -{ - uint64_t id = 0; - int fd; - - fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); - if (fd >= 0) { - if (read(fd, &id, sizeof (id)) != sizeof (id)) - id = 0; - - close(fd); - } - - if (id == 0) - id = (((uint64_t)rand()) << 32) | (uint64_t)rand(); - - snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id); -} - -/* - * Label an individual disk. The name provided is the short name, - * stripped of any leading /dev path. - */ -int -zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name) -{ - char path[MAXPATHLEN]; - struct dk_gpt *vtoc; - int rval, fd; - size_t resv = EFI_MIN_RESV_SIZE; - uint64_t slice_size; - diskaddr_t start_block; - char errbuf[ERRBUFLEN]; - - /* prepare an error message just in case */ - (void) snprintf(errbuf, sizeof (errbuf), - dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); - - if (zhp) { - nvlist_t *nvroot = fnvlist_lookup_nvlist(zhp->zpool_config, - ZPOOL_CONFIG_VDEV_TREE); - - if (zhp->zpool_start_block == 0) - start_block = find_start_block(nvroot); - else - start_block = zhp->zpool_start_block; - zhp->zpool_start_block = start_block; - } else { - /* new pool */ - start_block = NEW_START_BLOCK; - } - - (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name); - - if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL|O_CLOEXEC)) < 0) { - /* - * This shouldn't happen. We've long since verified that this - * is a valid device. - */ - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " - "label '%s': unable to open device: %d"), path, errno); - return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); - } - - if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { - /* - * The only way this can fail is if we run out of memory, or we - * were unable to read the disk's capacity - */ - if (errno == ENOMEM) - (void) no_memory(hdl); - - (void) close(fd); - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " - "label '%s': unable to read disk capacity"), path); - - return (zfs_error(hdl, EZFS_NOCAP, errbuf)); - } - - slice_size = vtoc->efi_last_u_lba + 1; - slice_size -= EFI_MIN_RESV_SIZE; - if (start_block == MAXOFFSET_T) - start_block = NEW_START_BLOCK; - slice_size -= start_block; - slice_size = P2ALIGN_TYPED(slice_size, PARTITION_END_ALIGNMENT, - uint64_t); - - vtoc->efi_parts[0].p_start = start_block; - vtoc->efi_parts[0].p_size = slice_size; - - if (vtoc->efi_parts[0].p_size * vtoc->efi_lbasize < SPA_MINDEVSIZE) { - (void) close(fd); - efi_free(vtoc); - - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " - "label '%s': partition would be less than the minimum " - "device size (64M)"), path); - return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); - } - - /* - * Why we use V_USR: V_BACKUP confuses users, and is considered - * disposable by some EFI utilities (since EFI doesn't have a backup - * slice). V_UNASSIGNED is supposed to be used only for zero size - * partitions, and efi_write() will fail if we use it. - * Other available types were all pretty specific. - * V_USR is as close to reality as we - * can get, in the absence of V_OTHER. - */ - vtoc->efi_parts[0].p_tag = V_USR; - zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN); - - vtoc->efi_parts[8].p_start = slice_size + start_block; - vtoc->efi_parts[8].p_size = resv; - vtoc->efi_parts[8].p_tag = V_RESERVED; - - rval = efi_write(fd, vtoc); - - /* Flush the buffers to disk and invalidate the page cache. */ - (void) fsync(fd); - (void) ioctl(fd, BLKFLSBUF); - - if (rval == 0) - rval = efi_rescan(fd); - - /* - * Some block drivers (like pcata) may not support EFI GPT labels. - * Print out a helpful error message directing the user to manually - * label the disk and give a specific slice. - */ - if (rval != 0) { - (void) close(fd); - efi_free(vtoc); - - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using " - "parted(8) and then provide a specific slice: %d"), rval); - return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); - } - - (void) close(fd); - efi_free(vtoc); - - (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name); - (void) zfs_append_partition(path, MAXPATHLEN); - - /* Wait to udev to signal use the device has settled. */ - rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT); - if (rval) { - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to " - "detect device partitions on '%s': %d"), path, rval); - return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); - } - - /* We can't be to paranoid. Read the label back and verify it. */ - (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name); - rval = zpool_label_disk_check(path); - if (rval) { - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written " - "EFI label on '%s' is damaged. Ensure\nthis device " - "is not in use, and is functioning properly: %d"), - path, rval); - return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); - } - return (0); -} diff --git a/lib/libzfs/os/linux/libzfs_util_os.c b/lib/libzfs/os/linux/libzfs_util_os.c deleted file mode 100644 index 55dfdf3723bd..000000000000 --- a/lib/libzfs/os/linux/libzfs_util_os.c +++ /dev/null @@ -1,275 +0,0 @@ -// SPDX-License-Identifier: CDDL-1.0 -/* - * CDDL HEADER START - * - * The contents of this file are subject to the terms of the - * Common Development and Distribution License (the "License"). - * You may not use this file except in compliance with the License. - * - * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE - * or https://opensource.org/licenses/CDDL-1.0. - * See the License for the specific language governing permissions - * and limitations under the License. - * - * When distributing Covered Code, include this CDDL HEADER in each - * file and include the License file at usr/src/OPENSOLARIS.LICENSE. - * If applicable, add the following below this CDDL HEADER, with the - * fields enclosed by brackets "[]" replaced with your own identifying - * information: Portions Copyright [yyyy] [name of copyright owner] - * - * CDDL HEADER END - */ - -/* - * Copyright (c) 2021 Klara, Inc. - */ - -#include <alloca.h> -#include <errno.h> -#include <fcntl.h> -#include <libintl.h> -#include <math.h> -#include <poll.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <strings.h> -#include <sys/inotify.h> -#include <sys/mntent.h> -#include <sys/mnttab.h> -#include <sys/stat.h> -#include <sys/timerfd.h> -#include <sys/types.h> -#include <sys/wait.h> -#include <unistd.h> - -#include <libzfs.h> -#include <libzfs_core.h> - -#include "../../libzfs_impl.h" -#include "zfs_prop.h" -#include <libzutil.h> -#include <sys/zfs_sysfs.h> - -#define ZDIFF_SHARESDIR "/.zfs/shares/" - -const char * -libzfs_error_init(int error) -{ - switch (error) { - case ENXIO: - return (dgettext(TEXT_DOMAIN, "The ZFS modules are not " - "loaded.\nTry running 'modprobe zfs' as root " - "to load them.")); - case ENOENT: - return (dgettext(TEXT_DOMAIN, "/dev/zfs and /proc/self/mounts " - "are required.\nTry running 'udevadm trigger' and 'mount " - "-t proc proc /proc' as root.")); - case ENOEXEC: - return (dgettext(TEXT_DOMAIN, "The ZFS modules cannot be " - "auto-loaded.\nTry running 'modprobe zfs' as " - "root to manually load them.")); - case EACCES: - return (dgettext(TEXT_DOMAIN, "Permission denied the " - "ZFS utilities must be run as root.")); - default: - return (dgettext(TEXT_DOMAIN, "Failed to initialize the " - "libzfs library.")); - } -} - -/* - * zfs(4) is loaded by udev if there's a fstype=zfs device present, - * but if there isn't, load them automatically; - * always wait for ZFS_DEV to appear via udev. - * - * Environment variables: - * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV, - * defaults to 10, max. 10 min. - */ -int -libzfs_load_module(void) -{ - if (access(ZFS_DEV, F_OK) == 0) - return (0); - - if (access(ZFS_SYSFS_DIR, F_OK) != 0) { - char *argv[] = {(char *)"modprobe", (char *)"zfs", NULL}; - if (libzfs_run_process("modprobe", argv, 0)) - return (ENOEXEC); - - if (access(ZFS_SYSFS_DIR, F_OK) != 0) - return (ENXIO); - } - - const char *timeout_str = getenv("ZFS_MODULE_TIMEOUT"); - int seconds = 10; - if (timeout_str) - seconds = MIN(strtol(timeout_str, NULL, 0), 600); - struct itimerspec timeout = {.it_value.tv_sec = MAX(seconds, 0)}; - - int ino = inotify_init1(IN_CLOEXEC); - if (ino == -1) - return (ENOENT); - inotify_add_watch(ino, ZFS_DEVDIR, IN_CREATE); - - if (access(ZFS_DEV, F_OK) == 0) { - close(ino); - return (0); - } else if (seconds == 0) { - close(ino); - return (ENOENT); - } - - size_t evsz = sizeof (struct inotify_event) + NAME_MAX + 1; - struct inotify_event *ev = alloca(evsz); - - int tout = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); - if (tout == -1) { - close(ino); - return (ENOENT); - } - timerfd_settime(tout, 0, &timeout, NULL); - - int ret = ENOENT; - struct pollfd pfds[] = { - {.fd = ino, .events = POLLIN}, - {.fd = tout, .events = POLLIN}, - }; - while (poll(pfds, ARRAY_SIZE(pfds), -1) != -1) { - if (pfds[0].revents & POLLIN) { - verify(read(ino, ev, evsz) > - sizeof (struct inotify_event)); - if (strncmp(ev->name, &ZFS_DEV[sizeof (ZFS_DEVDIR)], - ev->len) == 0) { - ret = 0; - break; - } - } - if (pfds[1].revents & POLLIN) - break; - } - close(tout); - close(ino); - return (ret); -} - -int -find_shares_object(differ_info_t *di) -{ - char fullpath[MAXPATHLEN]; - struct stat64 sb = { 0 }; - - (void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN); - (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN); - - if (stat64(fullpath, &sb) != 0) { - (void) snprintf(di->errbuf, sizeof (di->errbuf), - dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath); - return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf)); - } - - di->shares = (uint64_t)sb.st_ino; - return (0); -} - -int -zfs_destroy_snaps_nvl_os(libzfs_handle_t *hdl, nvlist_t *snaps) -{ - (void) hdl, (void) snaps; - return (0); -} - -/* - * Return allocated loaded module version, or NULL on error (with errno set) - */ -char * -zfs_version_kernel(void) -{ - FILE *f = fopen(ZFS_SYSFS_DIR "/version", "re"); - if (f == NULL) - return (NULL); - - char *ret = NULL; - size_t l; - ssize_t read; - if ((read = getline(&ret, &l, f)) == -1) { - int err = errno; - fclose(f); - errno = err; - return (NULL); - } - - fclose(f); - if (ret[read - 1] == '\n') - ret[read - 1] = '\0'; - return (ret); -} - -/* - * Add or delete the given filesystem to/from the given user namespace. - */ -int -zfs_userns(zfs_handle_t *zhp, const char *nspath, int attach) -{ - libzfs_handle_t *hdl = zhp->zfs_hdl; - zfs_cmd_t zc = {"\0"}; - char errbuf[1024]; - unsigned long cmd; - int ret; - - if (attach) { - (void) snprintf(errbuf, sizeof (errbuf), - dgettext(TEXT_DOMAIN, "cannot add '%s' to namespace"), - zhp->zfs_name); - } else { - (void) snprintf(errbuf, sizeof (errbuf), - dgettext(TEXT_DOMAIN, "cannot remove '%s' from namespace"), - zhp->zfs_name); - } - - switch (zhp->zfs_type) { - case ZFS_TYPE_VOLUME: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "volumes can not be namespaced")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_SNAPSHOT: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "snapshots can not be namespaced")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_BOOKMARK: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "bookmarks can not be namespaced")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_VDEV: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "vdevs can not be namespaced")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_INVALID: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "invalid zfs_type_t: ZFS_TYPE_INVALID")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_POOL: - zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, - "pools can not be namespaced")); - return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); - case ZFS_TYPE_FILESYSTEM: - break; - } - assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM); - - (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); - zc.zc_objset_type = DMU_OST_ZFS; - zc.zc_cleanup_fd = open(nspath, O_RDONLY); - if (zc.zc_cleanup_fd < 0) { - return (zfs_error(hdl, EZFS_NOT_USER_NAMESPACE, errbuf)); - } - - cmd = attach ? ZFS_IOC_USERNS_ATTACH : ZFS_IOC_USERNS_DETACH; - if ((ret = zfs_ioctl(hdl, cmd, &zc)) != 0) - zfs_standard_error(hdl, errno, errbuf); - - (void) close(zc.zc_cleanup_fd); - - return (ret); -} |