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;
52 be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
54 struct be_mountcheck_info *info;
59 info = (struct be_mountcheck_info *)data;
60 if (!zfs_is_mounted(zfs_hdl, &mountpoint))
62 if (strcmp(mountpoint, info->path) == 0) {
63 info->name = strdup(zfs_get_name(zfs_hdl));
72 * Called from be_mount, uses the given zfs_handle and attempts to
73 * mount it at the passed mountpoint. If the deepmount flag is set, continue
74 * calling the function for each child dataset.
77 be_mount_iter(zfs_handle_t *zfs_hdl, void *data)
81 char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN];
82 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,
123 if ((err = zmount(zfs_get_name(zfs_hdl), tmp, info->mntflags,
124 __DECONST(char *, MNTTYPE_ZFS), NULL, 0, &opt, 1)) != 0) {
127 return (set_error(info->lbh, BE_ERR_PATHLEN));
131 return (set_error(info->lbh, BE_ERR_BADPATH));
133 return (set_error(info->lbh, BE_ERR_PERMS));
135 return (set_error(info->lbh, BE_ERR_PATHBUSY));
137 return (set_error(info->lbh, BE_ERR_UNKNOWN));
141 if (!info->deepmount)
146 err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info);
153 be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
158 struct be_mount_info *info;
160 info = (struct be_mount_info *)data;
163 if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
168 if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
173 if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
176 return (set_error(info->lbh, BE_ERR_PATHLEN));
180 return (set_error(info->lbh, BE_ERR_BADPATH));
182 return (set_error(info->lbh, BE_ERR_PERMS));
184 return (set_error(info->lbh, BE_ERR_PATHBUSY));
186 return (set_error(info->lbh, BE_ERR_UNKNOWN));
196 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
198 char be[BE_MAXPATHLEN];
199 zfs_handle_t *root_hdl;
200 struct be_mountcheck_info info;
201 prop_data_t propinfo;
203 bzero(&be, BE_MAXPATHLEN);
204 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
205 ZFS_TYPE_FILESYSTEM)) == NULL)
206 return (BE_ERR_ZFSOPEN);
210 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
213 if (info.name != NULL) {
214 if (details != NULL) {
215 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
216 ZFS_TYPE_FILESYSTEM)) == NULL) {
218 return (BE_ERR_ZFSOPEN);
222 propinfo.list = details;
223 propinfo.single_object = false;
224 prop_list_builder_cb(root_hdl, &propinfo);
237 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags,
240 char be[BE_MAXPATHLEN];
241 char mnt_temp[BE_MAXPATHLEN];
242 int mntflags, mntdeep;
244 struct be_mount_info info;
247 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
248 return (set_error(lbh, err));
250 if ((err = be_exists(lbh, bootenv)) != 0)
251 return (set_error(lbh, err));
253 if (is_mounted(lbh->lzh, be, NULL))
254 return (set_error(lbh, BE_ERR_MOUNTED));
256 mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0;
257 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
259 /* Create mountpoint if it is not specified */
260 if (mountpoint == NULL) {
261 strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
262 if (mkdtemp(mnt_temp) == NULL)
263 return (set_error(lbh, BE_ERR_IO));
266 if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
267 return (set_error(lbh, BE_ERR_ZFSOPEN));
271 info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint;
272 info.mntflags = mntflags;
273 info.deepmount = mntdeep;
276 if((err = be_mount_iter(zhdl, &info) != 0)) {
282 if (result_loc != NULL)
283 strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
286 return (BE_ERR_SUCCESS);
293 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags)
296 char be[BE_MAXPATHLEN];
297 zfs_handle_t *root_hdl;
298 struct be_mount_info info;
300 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
301 return (set_error(lbh, err));
303 if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
304 return (set_error(lbh, BE_ERR_ZFSOPEN));
308 info.mountpoint = NULL;
309 info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0;
312 if ((err = be_umount_iter(root_hdl, &info)) != 0) {
318 return (BE_ERR_SUCCESS);
322 * This function will blow away the input buffer as needed if we're discovered
323 * to be looking at a root-mount. If the mountpoint is naturally beyond the
324 * root, however, the buffer may be left intact and a pointer to the section
325 * past altroot will be returned instead for the caller's perusal.
328 be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint)
331 if (lbh->altroot_len == 0)
333 if (mountpoint == NULL || *mountpoint == '\0')
336 if (mountpoint[lbh->altroot_len] == '\0') {
337 *(mountpoint + 1) = '\0';
340 return (mountpoint + lbh->altroot_len);