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$");
34 #include <sys/mntent.h>
39 struct be_mountcheck_info {
44 struct be_mount_info {
47 const char *mountpoint;
54 be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
56 struct be_mountcheck_info *info;
61 info = (struct be_mountcheck_info *)data;
62 if (!zfs_is_mounted(zfs_hdl, &mountpoint))
64 if (strcmp(mountpoint, info->path) == 0) {
65 info->name = strdup(zfs_get_name(zfs_hdl));
74 * Called from be_mount, uses the given zfs_handle and attempts to
75 * mount it at the passed mountpoint. If the deepmount flag is set, continue
76 * calling the function for each child dataset.
79 be_mount_iter(zfs_handle_t *zfs_hdl, void *data)
83 char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN];
84 struct be_mount_info *info;
86 info = (struct be_mount_info *)data;
88 if (zfs_is_mounted(zfs_hdl, &mountpoint)) {
94 * canmount and mountpoint are both ignored for the BE dataset, because
95 * the rest of the system (kernel and loader) will effectively do the
98 if (info->depth == 0) {
99 snprintf(tmp, BE_MAXPATHLEN, "%s", info->mountpoint);
101 if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) ==
105 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt,
106 BE_MAXPATHLEN, NULL, NULL, 0, 1))
110 * We've encountered mountpoint=none at some intermediate
111 * dataset (e.g. zroot/var) that will have children that may
112 * need to be mounted. Skip mounting it, but iterate through
115 if (strcmp("none", zfs_mnt) == 0)
118 mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt);
119 snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint,
123 if ((err = zfs_mount_at(zfs_hdl, NULL, info->mntflags, tmp)) != 0) {
126 return (set_error(info->lbh, BE_ERR_PATHLEN));
130 return (set_error(info->lbh, BE_ERR_BADPATH));
132 return (set_error(info->lbh, BE_ERR_PERMS));
134 return (set_error(info->lbh, BE_ERR_PATHBUSY));
136 return (set_error(info->lbh, BE_ERR_UNKNOWN));
140 if (!info->deepmount)
145 err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info);
152 be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
157 struct be_mount_info *info;
159 info = (struct be_mount_info *)data;
162 if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
167 if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
172 if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
175 return (set_error(info->lbh, BE_ERR_PATHLEN));
179 return (set_error(info->lbh, BE_ERR_BADPATH));
181 return (set_error(info->lbh, BE_ERR_PERMS));
183 return (set_error(info->lbh, BE_ERR_PATHBUSY));
185 return (set_error(info->lbh, BE_ERR_UNKNOWN));
195 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
197 char be[BE_MAXPATHLEN];
198 zfs_handle_t *root_hdl;
199 struct be_mountcheck_info info;
200 prop_data_t propinfo;
202 bzero(&be, BE_MAXPATHLEN);
203 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
204 ZFS_TYPE_FILESYSTEM)) == NULL)
205 return (BE_ERR_ZFSOPEN);
209 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
212 if (info.name != NULL) {
213 if (details != NULL) {
214 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
215 ZFS_TYPE_FILESYSTEM)) == NULL) {
217 return (BE_ERR_ZFSOPEN);
221 propinfo.list = details;
222 propinfo.single_object = false;
223 prop_list_builder_cb(root_hdl, &propinfo);
236 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags,
239 char be[BE_MAXPATHLEN];
240 char mnt_temp[BE_MAXPATHLEN];
241 int mntflags, mntdeep;
243 struct be_mount_info info;
246 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
247 return (set_error(lbh, err));
249 if ((err = be_exists(lbh, bootenv)) != 0)
250 return (set_error(lbh, err));
252 if (is_mounted(lbh->lzh, be, NULL))
253 return (set_error(lbh, BE_ERR_MOUNTED));
255 mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0;
256 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
258 /* Create mountpoint if it is not specified */
259 if (mountpoint == NULL) {
260 strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
261 if (mkdtemp(mnt_temp) == NULL)
262 return (set_error(lbh, BE_ERR_IO));
265 if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
266 return (set_error(lbh, BE_ERR_ZFSOPEN));
270 info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint;
271 info.mntflags = mntflags;
272 info.deepmount = mntdeep;
275 if((err = be_mount_iter(zhdl, &info) != 0)) {
281 if (result_loc != NULL)
282 strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
285 return (BE_ERR_SUCCESS);
292 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags)
295 char be[BE_MAXPATHLEN];
296 zfs_handle_t *root_hdl;
297 struct be_mount_info info;
299 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
300 return (set_error(lbh, err));
302 if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
303 return (set_error(lbh, BE_ERR_ZFSOPEN));
307 info.mountpoint = NULL;
308 info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0;
311 if ((err = be_umount_iter(root_hdl, &info)) != 0) {
317 return (BE_ERR_SUCCESS);
321 * This function will blow away the input buffer as needed if we're discovered
322 * to be looking at a root-mount. If the mountpoint is naturally beyond the
323 * root, however, the buffer may be left intact and a pointer to the section
324 * past altroot will be returned instead for the caller's perusal.
327 be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint)
330 if (lbh->altroot_len == 0)
332 if (mountpoint == NULL || *mountpoint == '\0')
335 if (mountpoint[lbh->altroot_len] == '\0') {
336 *(mountpoint + 1) = '\0';
339 return (mountpoint + lbh->altroot_len);