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)) {
92 if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
95 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt, BE_MAXPATHLEN,
99 if (strcmp("none", zfs_mnt) == 0) {
101 * mountpoint=none; we'll mount it at info->mountpoint assuming
102 * we're at the root. If we're not at the root, we're likely
103 * at some intermediate dataset (e.g. zroot/var) that will have
104 * children that may need to be mounted.
109 snprintf(tmp, BE_MAXPATHLEN, "%s", info->mountpoint);
111 mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt);
112 snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint,
117 if ((err = zmount(zfs_get_name(zfs_hdl), tmp, info->mntflags,
118 __DECONST(char *, MNTTYPE_ZFS), NULL, 0, &opt, 1)) != 0) {
121 return (set_error(info->lbh, BE_ERR_PATHLEN));
125 return (set_error(info->lbh, BE_ERR_BADPATH));
127 return (set_error(info->lbh, BE_ERR_PERMS));
129 return (set_error(info->lbh, BE_ERR_PATHBUSY));
131 return (set_error(info->lbh, BE_ERR_UNKNOWN));
135 if (!info->deepmount)
140 err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info);
147 be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
152 struct be_mount_info *info;
154 info = (struct be_mount_info *)data;
157 if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
162 if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
167 if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
170 return (set_error(info->lbh, BE_ERR_PATHLEN));
174 return (set_error(info->lbh, BE_ERR_BADPATH));
176 return (set_error(info->lbh, BE_ERR_PERMS));
178 return (set_error(info->lbh, BE_ERR_PATHBUSY));
180 return (set_error(info->lbh, BE_ERR_UNKNOWN));
190 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
192 char be[BE_MAXPATHLEN];
193 zfs_handle_t *root_hdl;
194 struct be_mountcheck_info info;
195 prop_data_t propinfo;
197 bzero(&be, BE_MAXPATHLEN);
198 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
199 ZFS_TYPE_FILESYSTEM)) == NULL)
200 return (BE_ERR_ZFSOPEN);
204 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
207 if (info.name != NULL) {
208 if (details != NULL) {
209 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
210 ZFS_TYPE_FILESYSTEM)) == NULL) {
212 return (BE_ERR_ZFSOPEN);
216 propinfo.list = details;
217 propinfo.single_object = false;
218 prop_list_builder_cb(root_hdl, &propinfo);
231 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags,
234 char be[BE_MAXPATHLEN];
235 char mnt_temp[BE_MAXPATHLEN];
236 int mntflags, mntdeep;
238 struct be_mount_info info;
241 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
242 return (set_error(lbh, err));
244 if ((err = be_exists(lbh, bootenv)) != 0)
245 return (set_error(lbh, err));
247 if (is_mounted(lbh->lzh, be, NULL))
248 return (set_error(lbh, BE_ERR_MOUNTED));
250 mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0;
251 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
253 /* Create mountpoint if it is not specified */
254 if (mountpoint == NULL) {
255 strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
256 if (mkdtemp(mnt_temp) == NULL)
257 return (set_error(lbh, BE_ERR_IO));
260 if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
261 return (set_error(lbh, BE_ERR_ZFSOPEN));
265 info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint;
266 info.mntflags = mntflags;
267 info.deepmount = mntdeep;
270 if((err = be_mount_iter(zhdl, &info) != 0)) {
276 if (result_loc != NULL)
277 strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
280 return (BE_ERR_SUCCESS);
287 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags)
290 char be[BE_MAXPATHLEN];
291 zfs_handle_t *root_hdl;
292 struct be_mount_info info;
294 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
295 return (set_error(lbh, err));
297 if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
298 return (set_error(lbh, BE_ERR_ZFSOPEN));
302 info.mountpoint = NULL;
303 info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0;
306 if ((err = be_umount_iter(root_hdl, &info)) != 0) {
312 return (BE_ERR_SUCCESS);
316 * This function will blow away the input buffer as needed if we're discovered
317 * to be looking at a root-mount. If the mountpoint is naturally beyond the
318 * root, however, the buffer may be left intact and a pointer to the section
319 * past altroot will be returned instead for the caller's perusal.
322 be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint)
325 if (lbh->altroot_len == 0)
327 if (mountpoint == NULL || *mountpoint == '\0')
330 if (mountpoint[lbh->altroot_len] == '\0') {
331 *(mountpoint + 1) = '\0';
334 return (mountpoint + lbh->altroot_len);