4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
33 #include <sys/mnttab.h>
34 #include <sys/mntent.h>
35 #include <sys/types.h>
39 #include <libzfs_core.h>
41 #include "../../libzfs_impl.h"
44 #include <sys/zfs_sysfs.h>
46 #define ZDIFF_SHARESDIR "/.zfs/shares/"
49 zfs_ioctl(libzfs_handle_t
*hdl
, int request
, zfs_cmd_t
*zc
)
51 return (ioctl(hdl
->libzfs_fd
, request
, zc
));
55 libzfs_error_init(int error
)
59 return (dgettext(TEXT_DOMAIN
, "The ZFS modules are not "
60 "loaded.\nTry running '/sbin/modprobe zfs' as root "
63 return (dgettext(TEXT_DOMAIN
, "/dev/zfs and /proc/self/mounts "
64 "are required.\nTry running 'udevadm trigger' and 'mount "
65 "-t proc proc /proc' as root."));
67 return (dgettext(TEXT_DOMAIN
, "The ZFS modules cannot be "
68 "auto-loaded.\nTry running '/sbin/modprobe zfs' as "
69 "root to manually load them."));
71 return (dgettext(TEXT_DOMAIN
, "Permission denied the "
72 "ZFS utilities must be run as root."));
74 return (dgettext(TEXT_DOMAIN
, "Failed to initialize the "
80 libzfs_module_loaded(const char *module
)
82 const char path_prefix
[] = "/sys/module/";
85 memcpy(path
, path_prefix
, sizeof (path_prefix
) - 1);
86 strcpy(path
+ sizeof (path_prefix
) - 1, module
);
88 return (access(path
, F_OK
) == 0);
92 * Verify the required ZFS_DEV device is available and optionally attempt
93 * to load the ZFS modules. Under normal circumstances the modules
94 * should already have been loaded by some external mechanism.
96 * Environment variables:
97 * - ZFS_MODULE_LOADING="YES|yes|ON|on" - Attempt to load modules.
98 * - ZFS_MODULE_TIMEOUT="<seconds>" - Seconds to wait for ZFS_DEV
101 libzfs_load_module_impl(const char *module
)
103 char *argv
[4] = {"/sbin/modprobe", "-q", (char *)module
, (char *)0};
104 char *load_str
, *timeout_str
;
105 long timeout
= 10; /* seconds */
106 long busy_timeout
= 10; /* milliseconds */
110 /* Optionally request module loading */
111 if (!libzfs_module_loaded(module
)) {
112 load_str
= getenv("ZFS_MODULE_LOADING");
114 if (!strncasecmp(load_str
, "YES", strlen("YES")) ||
115 !strncasecmp(load_str
, "ON", strlen("ON")))
122 if (libzfs_run_process("/sbin/modprobe", argv
, 0))
126 if (!libzfs_module_loaded(module
))
131 * Device creation by udev is asynchronous and waiting may be
132 * required. Busy wait for 10ms and then fall back to polling every
133 * 10ms for the allowed timeout (default 10s, max 10m). This is
134 * done to optimize for the common case where the device is
135 * immediately available and to avoid penalizing the possible
136 * case where udev is slow or unable to create the device.
138 timeout_str
= getenv("ZFS_MODULE_TIMEOUT");
140 timeout
= strtol(timeout_str
, NULL
, 0);
141 timeout
= MAX(MIN(timeout
, (10 * 60)), 0); /* 0 <= N <= 600 */
146 fd
= open(ZFS_DEV
, O_RDWR
| O_CLOEXEC
);
150 } else if (errno
!= ENOENT
) {
152 } else if (NSEC2MSEC(gethrtime() - start
) < busy_timeout
) {
155 usleep(10 * MILLISEC
);
157 } while (NSEC2MSEC(gethrtime() - start
) < (timeout
* MILLISEC
));
163 libzfs_load_module(void)
165 return (libzfs_load_module_impl(ZFS_DRIVER
));
169 find_shares_object(differ_info_t
*di
)
171 char fullpath
[MAXPATHLEN
];
172 struct stat64 sb
= { 0 };
174 (void) strlcpy(fullpath
, di
->dsmnt
, MAXPATHLEN
);
175 (void) strlcat(fullpath
, ZDIFF_SHARESDIR
, MAXPATHLEN
);
177 if (stat64(fullpath
, &sb
) != 0) {
178 (void) snprintf(di
->errbuf
, sizeof (di
->errbuf
),
179 dgettext(TEXT_DOMAIN
, "Cannot stat %s"), fullpath
);
180 return (zfs_error(di
->zhp
->zfs_hdl
, EZFS_DIFF
, di
->errbuf
));
183 di
->shares
= (uint64_t)sb
.st_ino
;
188 * Fill given version buffer with zfs kernel version read from ZFS_SYSFS_DIR
189 * Returns 0 on success, and -1 on error (with errno set)
192 zfs_version_kernel(char *version
, int len
)
198 if ((fd
= open(ZFS_SYSFS_DIR
"/version", O_RDONLY
| O_CLOEXEC
)) == -1)
201 if ((rlen
= read(fd
, version
, len
)) == -1) {
209 version
[rlen
-1] = '\0'; /* discard '\n' */