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>
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$");
36 struct be_mountcheck_info {
42 be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
44 struct be_mountcheck_info *info;
49 info = (struct be_mountcheck_info *)data;
50 if (!zfs_is_mounted(zfs_hdl, &mountpoint))
52 if (strcmp(mountpoint, info->path) == 0) {
53 info->name = strdup(zfs_get_name(zfs_hdl));
65 be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
67 char be[BE_MAXPATHLEN];
68 zfs_handle_t *root_hdl;
69 struct be_mountcheck_info info;
72 bzero(&be, BE_MAXPATHLEN);
73 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
74 ZFS_TYPE_FILESYSTEM)) == NULL)
75 return (BE_ERR_ZFSOPEN);
79 zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
82 if (info.name != NULL) {
83 if (details != NULL) {
84 if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
85 ZFS_TYPE_FILESYSTEM)) == NULL) {
87 return (BE_ERR_ZFSOPEN);
91 propinfo.list = details;
92 propinfo.single_object = false;
93 prop_list_builder_cb(root_hdl, &propinfo);
106 be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags,
109 char be[BE_MAXPATHLEN];
110 char mnt_temp[BE_MAXPATHLEN];
114 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
115 return (set_error(lbh, err));
117 if ((err = be_exists(lbh, bootenv)) != 0)
118 return (set_error(lbh, err));
120 if (is_mounted(lbh->lzh, be, NULL))
121 return (set_error(lbh, BE_ERR_MOUNTED));
123 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
125 /* Create mountpoint if it is not specified */
126 if (mountpoint == NULL) {
127 strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
128 if (mkdtemp(mnt_temp) == NULL)
129 return (set_error(lbh, BE_ERR_IO));
133 if ((err = zmount(be, (mountpoint == NULL) ? mnt_temp : mountpoint,
134 mntflags, __DECONST(char *, MNTTYPE_ZFS), NULL, 0, &opt, 1)) != 0) {
137 return (set_error(lbh, BE_ERR_PATHLEN));
141 return (set_error(lbh, BE_ERR_BADPATH));
143 return (set_error(lbh, BE_ERR_PERMS));
145 return (set_error(lbh, BE_ERR_PATHBUSY));
147 return (set_error(lbh, BE_ERR_UNKNOWN));
151 if (result_loc != NULL)
152 strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
155 return (BE_ERR_SUCCESS);
163 be_unmount(libbe_handle_t *lbh, char *bootenv, int flags)
166 char be[BE_MAXPATHLEN];
167 struct statfs *mntbuf;
171 if ((err = be_root_concat(lbh, bootenv, be)) != 0)
172 return (set_error(lbh, err));
174 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
176 return (set_error(lbh, BE_ERR_IO));
177 return (set_error(lbh, BE_ERR_NOMOUNT));
181 for (int i = 0; i < mntsize; ++i) {
182 /* 0x000000de is the type number of zfs */
183 if (mntbuf[i].f_type != 0x000000de)
186 if (strcmp(mntbuf[i].f_mntfromname, be) == 0) {
187 mntpath = mntbuf[i].f_mntonname;
193 return (set_error(lbh, BE_ERR_NOMOUNT));
195 mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
197 if ((err = unmount(mntpath, mntflags)) != 0) {
200 return (set_error(lbh, BE_ERR_PATHLEN));
204 return (set_error(lbh, BE_ERR_BADPATH));
206 return (set_error(lbh, BE_ERR_PERMS));
208 return (set_error(lbh, BE_ERR_PATHBUSY));
210 return (set_error(lbh, BE_ERR_UNKNOWN));
214 return (set_error(lbh, BE_ERR_SUCCESS));