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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/mntent.h>
38 struct be_mountcheck_info {
43 struct be_mount_info {
46 const char *mountpoint;
53 be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
55 struct be_mountcheck_info *info;
60 info = (struct be_mountcheck_info *)data;
61 if (!zfs_is_mounted(zfs_hdl, &mountpoint))
63 if (strcmp(mountpoint, info->path) == 0) {
64 info->name = strdup(zfs_get_name(zfs_hdl));
73 * Called from be_mount, uses the given zfs_handle and attempts to
74 * mount it at the passed mountpoint. If the deepmount flag is set, continue
75 * calling the function for each child dataset.
78 be_mount_iter(zfs_handle_t *zfs_hdl, void *data)
82 char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN];
83 struct be_mount_info *info;
85 info = (struct be_mount_info *)data;
87 if (zfs_is_mounted(zfs_hdl, &mountpoint)) {
93 * canmount and mountpoint are both ignored for the BE dataset, because
94 * the rest of the system (kernel and loader) will effectively do the
97 if (info->depth == 0) {
98 snprintf(tmp, BE_MAXPATHLEN, "%s", info->mountpoint);
100 if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) ==
104 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt,
105 BE_MAXPATHLEN, NULL, NULL, 0, 1))
109 * We've encountered mountpoint=none at some intermediate
110 * dataset (e.g. zroot/var) that will have children that may
111 * need to be mounted. Skip mounting it, but iterate through
114 if (strcmp("none", zfs_mnt) == 0)
117 mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt);
118 snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint,
122 if ((err = zfs_mount_at(zfs_hdl, NULL, info->mntflags, tmp)) != 0) {
125 return (set_error(info->lbh, BE_ERR_PATHLEN));
129 return (set_error(info->lbh, BE_ERR_BADPATH));
131 return (set_error(info->lbh, BE_ERR_PERMS));
133 return (set_error(info->lbh, BE_ERR_PATHBUSY));
135 return (set_error(info->lbh, BE_ERR_UNKNOWN));
139 if (!info->deepmount)
144 err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info);
151 be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
156 struct be_mount_info *info;
158 info = (struct be_mount_info *)data;
161 if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
166 if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
171 if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
174 return (set_error(info->lbh, BE_ERR_PATHLEN));
178 return (set_error(info->lbh, BE_ERR_BADPATH));
180 return (set_error(info->lbh, BE_ERR_PERMS));
182 return (set_error(info->lbh, BE_ERR_PATHBUSY));
184 return (set_error(info->lbh, BE_ERR_UNKNOWN));
194 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
196 char be[BE_MAXPATHLEN];
197 zfs_handle_t *root_hdl;
198 struct be_mountcheck_info info;
199 prop_data_t propinfo;
201 bzero(&be, BE_MAXPATHLEN);
202 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
203 ZFS_TYPE_FILESYSTEM)) == NULL)
204 return (BE_ERR_ZFSOPEN);
208 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
211 if (info.name != NULL) {
212 if (details != NULL) {
213 if ((root_hdl = zfs_open(lbh->lzh, info.name,
214 ZFS_TYPE_FILESYSTEM)) == NULL) {
216 return (BE_ERR_ZFSOPEN);
220 propinfo.list = details;
221 propinfo.single_object = false;
222 propinfo.bootonce = NULL;
223 prop_list_builder_cb(root_hdl, &propinfo);
236 be_mount(libbe_handle_t *lbh, const char *bootenv, const char *mountpoint,
237 int flags, char *result_loc)
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, const 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);