2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5 * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 * Copyright (c) 2019 Wes Maag <wes@jwmaag.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 struct be_mountcheck_info {
42 struct be_mount_info {
45 const char *mountpoint;
51 be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
53 struct be_mountcheck_info *info;
58 info = (struct be_mountcheck_info *)data;
59 if (!zfs_is_mounted(zfs_hdl, &mountpoint))
61 if (strcmp(mountpoint, info->path) == 0) {
62 info->name = strdup(zfs_get_name(zfs_hdl));
71 * Called from be_mount, uses the given zfs_handle and attempts to
72 * mount it at the passed mountpoint. If the deepmount flag is set, continue
73 * calling the function for each child dataset.
76 be_mount_iter(zfs_handle_t *zfs_hdl, void *data)
80 char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN];
81 struct be_mount_info *info;
83 info = (struct be_mount_info *)data;
85 if (zfs_is_mounted(zfs_hdl, &mountpoint)) {
90 if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
93 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt, BE_MAXPATHLEN,
97 if (strcmp("none", zfs_mnt) != 0) {
100 mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt);
102 snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint,
105 if ((err = zmount(zfs_get_name(zfs_hdl), tmp, info->mntflags,
106 __DECONST(char *, MNTTYPE_ZFS), NULL, 0, &opt, 1)) != 0) {
109 return (set_error(info->lbh, BE_ERR_PATHLEN));
113 return (set_error(info->lbh, BE_ERR_BADPATH));
115 return (set_error(info->lbh, BE_ERR_PERMS));
117 return (set_error(info->lbh, BE_ERR_PATHBUSY));
119 return (set_error(info->lbh, BE_ERR_UNKNOWN));
124 if (!info->deepmount)
127 return (zfs_iter_filesystems(zfs_hdl, be_mount_iter, info));
132 be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
137 struct be_mount_info *info;
139 info = (struct be_mount_info *)data;
141 if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
145 if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
150 if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
153 return (set_error(info->lbh, BE_ERR_PATHLEN));
157 return (set_error(info->lbh, BE_ERR_BADPATH));
159 return (set_error(info->lbh, BE_ERR_PERMS));
161 return (set_error(info->lbh, BE_ERR_PATHBUSY));
163 return (set_error(info->lbh, BE_ERR_UNKNOWN));
173 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
175 char be[BE_MAXPATHLEN];
176 zfs_handle_t *root_hdl;
177 struct be_mountcheck_info info;
178 prop_data_t propinfo;
180 bzero(&be, BE_MAXPATHLEN);
181 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
182 ZFS_TYPE_FILESYSTEM)) == NULL)
183 return (BE_ERR_ZFSOPEN);
187 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
190 if (info.name != NULL) {
191 if (details != NULL) {
192 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
193 ZFS_TYPE_FILESYSTEM)) == NULL) {
195 return (BE_ERR_ZFSOPEN);
199 propinfo.list = details;
200 propinfo.single_object = false;
201 prop_list_builder_cb(root_hdl, &propinfo);
214 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags,
217 char be[BE_MAXPATHLEN];
218 char mnt_temp[BE_MAXPATHLEN];
219 int mntflags, mntdeep;
221 struct be_mount_info info;
224 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
225 return (set_error(lbh, err));
227 if ((err = be_exists(lbh, bootenv)) != 0)
228 return (set_error(lbh, err));
230 if (is_mounted(lbh->lzh, be, NULL))
231 return (set_error(lbh, BE_ERR_MOUNTED));
233 mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0;
234 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
236 /* Create mountpoint if it is not specified */
237 if (mountpoint == NULL) {
238 strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
239 if (mkdtemp(mnt_temp) == NULL)
240 return (set_error(lbh, BE_ERR_IO));
243 if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
244 return (set_error(lbh, BE_ERR_ZFSOPEN));
248 info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint;
249 info.mntflags = mntflags;
250 info.deepmount = mntdeep;
252 if((err = be_mount_iter(zhdl, &info) != 0)) {
258 if (result_loc != NULL)
259 strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
262 return (BE_ERR_SUCCESS);
269 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags)
272 char be[BE_MAXPATHLEN];
273 zfs_handle_t *root_hdl;
274 struct be_mount_info info;
276 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
277 return (set_error(lbh, err));
279 if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
280 return (set_error(lbh, BE_ERR_ZFSOPEN));
284 info.mountpoint = NULL;
285 info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0;
287 if ((err = be_umount_iter(root_hdl, &info)) != 0) {
293 return (BE_ERR_SUCCESS);
297 * This function will blow away the input buffer as needed if we're discovered
298 * to be looking at a root-mount. If the mountpoint is naturally beyond the
299 * root, however, the buffer may be left intact and a pointer to the section
300 * past altroot will be returned instead for the caller's perusal.
303 be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint)
306 if (lbh->altroot_len == 0)
308 if (mountpoint == NULL || *mountpoint == '\0')
311 if (mountpoint[lbh->altroot_len] == '\0') {
312 *(mountpoint + 1) = '\0';
315 return (mountpoint + lbh->altroot_len);