1b179da01SKyle Evans /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
328f16a0fSKyle Evans *
428f16a0fSKyle Evans * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5b179da01SKyle Evans * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
60a603a6eSKyle Evans * Copyright (c) 2019 Wes Maag <wes@jwmaag.org>
728f16a0fSKyle Evans *
828f16a0fSKyle Evans * Redistribution and use in source and binary forms, with or without
928f16a0fSKyle Evans * modification, are permitted provided that the following conditions
1028f16a0fSKyle Evans * are met:
1128f16a0fSKyle Evans * 1. Redistributions of source code must retain the above copyright
1228f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer.
1328f16a0fSKyle Evans * 2. Redistributions in binary form must reproduce the above copyright
1428f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer in the
1528f16a0fSKyle Evans * documentation and/or other materials provided with the distribution.
1628f16a0fSKyle Evans *
1728f16a0fSKyle Evans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1828f16a0fSKyle Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1928f16a0fSKyle Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2028f16a0fSKyle Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2128f16a0fSKyle Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2228f16a0fSKyle Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2328f16a0fSKyle Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2428f16a0fSKyle Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2528f16a0fSKyle Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2628f16a0fSKyle Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2728f16a0fSKyle Evans * SUCH DAMAGE.
2828f16a0fSKyle Evans */
2928f16a0fSKyle Evans
30b6e7c421SKyle Evans #include <sys/cdefs.h>
31485172f5SKyle Evans #include <sys/mntent.h>
32485172f5SKyle Evans
3328f16a0fSKyle Evans #include "be.h"
3428f16a0fSKyle Evans #include "be_impl.h"
3528f16a0fSKyle Evans
36843e39ceSKyle Evans struct be_mountcheck_info {
37843e39ceSKyle Evans const char *path;
38843e39ceSKyle Evans char *name;
39843e39ceSKyle Evans };
40843e39ceSKyle Evans
410a603a6eSKyle Evans struct be_mount_info {
420a603a6eSKyle Evans libbe_handle_t *lbh;
430a603a6eSKyle Evans const char *be;
440a603a6eSKyle Evans const char *mountpoint;
450a603a6eSKyle Evans int mntflags;
460a603a6eSKyle Evans int deepmount;
47011fdcbfSKyle Evans int depth;
480a603a6eSKyle Evans };
490a603a6eSKyle Evans
50843e39ceSKyle Evans static int
be_mountcheck_cb(zfs_handle_t * zfs_hdl,void * data)51843e39ceSKyle Evans be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
52843e39ceSKyle Evans {
53843e39ceSKyle Evans struct be_mountcheck_info *info;
54843e39ceSKyle Evans char *mountpoint;
55843e39ceSKyle Evans
56843e39ceSKyle Evans if (data == NULL)
57843e39ceSKyle Evans return (1);
58843e39ceSKyle Evans info = (struct be_mountcheck_info *)data;
59843e39ceSKyle Evans if (!zfs_is_mounted(zfs_hdl, &mountpoint))
60843e39ceSKyle Evans return (0);
61843e39ceSKyle Evans if (strcmp(mountpoint, info->path) == 0) {
62843e39ceSKyle Evans info->name = strdup(zfs_get_name(zfs_hdl));
63cc4deabcSKyle Evans free(mountpoint);
64843e39ceSKyle Evans return (1);
65843e39ceSKyle Evans }
66cc4deabcSKyle Evans free(mountpoint);
67843e39ceSKyle Evans return (0);
68843e39ceSKyle Evans }
69843e39ceSKyle Evans
70843e39ceSKyle Evans /*
710a603a6eSKyle Evans * Called from be_mount, uses the given zfs_handle and attempts to
720a603a6eSKyle Evans * mount it at the passed mountpoint. If the deepmount flag is set, continue
730a603a6eSKyle Evans * calling the function for each child dataset.
740a603a6eSKyle Evans */
750a603a6eSKyle Evans static int
be_mount_iter(zfs_handle_t * zfs_hdl,void * data)760a603a6eSKyle Evans be_mount_iter(zfs_handle_t *zfs_hdl, void *data)
770a603a6eSKyle Evans {
780a603a6eSKyle Evans int err;
790a603a6eSKyle Evans char *mountpoint;
800a603a6eSKyle Evans char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN];
810a603a6eSKyle Evans struct be_mount_info *info;
820a603a6eSKyle Evans
830a603a6eSKyle Evans info = (struct be_mount_info *)data;
840a603a6eSKyle Evans
850a603a6eSKyle Evans if (zfs_is_mounted(zfs_hdl, &mountpoint)) {
860a603a6eSKyle Evans free(mountpoint);
870a603a6eSKyle Evans return (0);
880a603a6eSKyle Evans }
890a603a6eSKyle Evans
90011fdcbfSKyle Evans /*
913afb4dc2SKyle Evans * canmount and mountpoint are both ignored for the BE dataset, because
923afb4dc2SKyle Evans * the rest of the system (kernel and loader) will effectively do the
933afb4dc2SKyle Evans * same.
94011fdcbfSKyle Evans */
953afb4dc2SKyle Evans if (info->depth == 0) {
96011fdcbfSKyle Evans snprintf(tmp, BE_MAXPATHLEN, "%s", info->mountpoint);
97011fdcbfSKyle Evans } else {
983afb4dc2SKyle Evans if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) ==
993afb4dc2SKyle Evans ZFS_CANMOUNT_OFF)
1003afb4dc2SKyle Evans return (0);
1013afb4dc2SKyle Evans
1023afb4dc2SKyle Evans if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt,
1033afb4dc2SKyle Evans BE_MAXPATHLEN, NULL, NULL, 0, 1))
1043afb4dc2SKyle Evans return (1);
1053afb4dc2SKyle Evans
1063afb4dc2SKyle Evans /*
1073afb4dc2SKyle Evans * We've encountered mountpoint=none at some intermediate
1083afb4dc2SKyle Evans * dataset (e.g. zroot/var) that will have children that may
1093afb4dc2SKyle Evans * need to be mounted. Skip mounting it, but iterate through
1103afb4dc2SKyle Evans * the children.
1113afb4dc2SKyle Evans */
1123afb4dc2SKyle Evans if (strcmp("none", zfs_mnt) == 0)
1133afb4dc2SKyle Evans goto skipmount;
1143afb4dc2SKyle Evans
1150a603a6eSKyle Evans mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt);
1160a603a6eSKyle Evans snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint,
1170a603a6eSKyle Evans mountpoint);
118011fdcbfSKyle Evans }
1190a603a6eSKyle Evans
120c7a19fd7SKyle Evans if ((err = zfs_mount_at(zfs_hdl, NULL, info->mntflags, tmp)) != 0) {
1210a603a6eSKyle Evans switch (errno) {
1220a603a6eSKyle Evans case ENAMETOOLONG:
1230a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PATHLEN));
1240a603a6eSKyle Evans case ELOOP:
1250a603a6eSKyle Evans case ENOENT:
1260a603a6eSKyle Evans case ENOTDIR:
1270a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_BADPATH));
1280a603a6eSKyle Evans case EPERM:
1290a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PERMS));
1300a603a6eSKyle Evans case EBUSY:
1310a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PATHBUSY));
1320a603a6eSKyle Evans default:
1330a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_UNKNOWN));
1340a603a6eSKyle Evans }
1350a603a6eSKyle Evans }
1360a603a6eSKyle Evans
1370a603a6eSKyle Evans if (!info->deepmount)
1380a603a6eSKyle Evans return (0);
1390a603a6eSKyle Evans
14088a95076SKyle Evans skipmount:
141011fdcbfSKyle Evans ++info->depth;
142d411c1d6SMartin Matuska err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info);
143011fdcbfSKyle Evans --info->depth;
144011fdcbfSKyle Evans return (err);
1450a603a6eSKyle Evans }
1460a603a6eSKyle Evans
1470a603a6eSKyle Evans
1480a603a6eSKyle Evans static int
be_umount_iter(zfs_handle_t * zfs_hdl,void * data)1490a603a6eSKyle Evans be_umount_iter(zfs_handle_t *zfs_hdl, void *data)
1500a603a6eSKyle Evans {
1510a603a6eSKyle Evans
1520a603a6eSKyle Evans int err;
1530a603a6eSKyle Evans char *mountpoint;
1540a603a6eSKyle Evans struct be_mount_info *info;
1550a603a6eSKyle Evans
1560a603a6eSKyle Evans info = (struct be_mount_info *)data;
1570a603a6eSKyle Evans
158011fdcbfSKyle Evans ++info->depth;
159d411c1d6SMartin Matuska if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) {
1600a603a6eSKyle Evans return (err);
1610a603a6eSKyle Evans }
162011fdcbfSKyle Evans --info->depth;
1630a603a6eSKyle Evans
1640a603a6eSKyle Evans if (!zfs_is_mounted(zfs_hdl, &mountpoint)) {
1650a603a6eSKyle Evans return (0);
1660a603a6eSKyle Evans }
1670a603a6eSKyle Evans free(mountpoint);
1680a603a6eSKyle Evans
1690a603a6eSKyle Evans if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) {
1700a603a6eSKyle Evans switch (errno) {
1710a603a6eSKyle Evans case ENAMETOOLONG:
1720a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PATHLEN));
1730a603a6eSKyle Evans case ELOOP:
1740a603a6eSKyle Evans case ENOENT:
1750a603a6eSKyle Evans case ENOTDIR:
1760a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_BADPATH));
1770a603a6eSKyle Evans case EPERM:
1780a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PERMS));
1790a603a6eSKyle Evans case EBUSY:
1800a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_PATHBUSY));
1810a603a6eSKyle Evans default:
1820a603a6eSKyle Evans return (set_error(info->lbh, BE_ERR_UNKNOWN));
1830a603a6eSKyle Evans }
1840a603a6eSKyle Evans }
1850a603a6eSKyle Evans return (0);
1860a603a6eSKyle Evans }
1870a603a6eSKyle Evans
1880a603a6eSKyle Evans /*
189843e39ceSKyle Evans * usage
190843e39ceSKyle Evans */
191843e39ceSKyle Evans int
be_mounted_at(libbe_handle_t * lbh,const char * path,nvlist_t * details)192843e39ceSKyle Evans be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
193843e39ceSKyle Evans {
19455b0e92bSKyle Evans char be[BE_MAXPATHLEN];
195843e39ceSKyle Evans zfs_handle_t *root_hdl;
196843e39ceSKyle Evans struct be_mountcheck_info info;
197843e39ceSKyle Evans prop_data_t propinfo;
198843e39ceSKyle Evans
19955b0e92bSKyle Evans bzero(&be, BE_MAXPATHLEN);
200843e39ceSKyle Evans if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
201843e39ceSKyle Evans ZFS_TYPE_FILESYSTEM)) == NULL)
202843e39ceSKyle Evans return (BE_ERR_ZFSOPEN);
203843e39ceSKyle Evans
204843e39ceSKyle Evans info.path = path;
205843e39ceSKyle Evans info.name = NULL;
206d411c1d6SMartin Matuska zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
207843e39ceSKyle Evans zfs_close(root_hdl);
208843e39ceSKyle Evans
209843e39ceSKyle Evans if (info.name != NULL) {
210843e39ceSKyle Evans if (details != NULL) {
21131190aa0SKyle Evans if ((root_hdl = zfs_open(lbh->lzh, info.name,
212843e39ceSKyle Evans ZFS_TYPE_FILESYSTEM)) == NULL) {
213843e39ceSKyle Evans free(info.name);
214843e39ceSKyle Evans return (BE_ERR_ZFSOPEN);
215843e39ceSKyle Evans }
216843e39ceSKyle Evans
217843e39ceSKyle Evans propinfo.lbh = lbh;
218843e39ceSKyle Evans propinfo.list = details;
2194146029bSKyle Evans propinfo.single_object = false;
220d155d8e1SKyle Evans propinfo.bootonce = NULL;
221843e39ceSKyle Evans prop_list_builder_cb(root_hdl, &propinfo);
222843e39ceSKyle Evans zfs_close(root_hdl);
223843e39ceSKyle Evans }
224843e39ceSKyle Evans free(info.name);
225843e39ceSKyle Evans return (0);
226843e39ceSKyle Evans }
227843e39ceSKyle Evans return (1);
228843e39ceSKyle Evans }
229843e39ceSKyle Evans
23028f16a0fSKyle Evans /*
23128f16a0fSKyle Evans * usage
23228f16a0fSKyle Evans */
23328f16a0fSKyle Evans int
be_mount(libbe_handle_t * lbh,const char * bootenv,const char * mountpoint,int flags,char * result_loc)2345773e924SKyle Evans be_mount(libbe_handle_t *lbh, const char *bootenv, const char *mountpoint,
2355773e924SKyle Evans int flags, char *result_loc)
23628f16a0fSKyle Evans {
23728f16a0fSKyle Evans char be[BE_MAXPATHLEN];
23828f16a0fSKyle Evans char mnt_temp[BE_MAXPATHLEN];
2390a603a6eSKyle Evans int mntflags, mntdeep;
24028f16a0fSKyle Evans int err;
2410a603a6eSKyle Evans struct be_mount_info info;
2420a603a6eSKyle Evans zfs_handle_t *zhdl;
24328f16a0fSKyle Evans
244b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, bootenv, be)) != 0)
24528f16a0fSKyle Evans return (set_error(lbh, err));
24628f16a0fSKyle Evans
247162ec569SKyle Evans if ((err = be_exists(lbh, bootenv)) != 0)
248162ec569SKyle Evans return (set_error(lbh, err));
24928f16a0fSKyle Evans
250cc4deabcSKyle Evans if (is_mounted(lbh->lzh, be, NULL))
25128f16a0fSKyle Evans return (set_error(lbh, BE_ERR_MOUNTED));
25228f16a0fSKyle Evans
2530a603a6eSKyle Evans mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0;
25428f16a0fSKyle Evans mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0;
25528f16a0fSKyle Evans
25628f16a0fSKyle Evans /* Create mountpoint if it is not specified */
25728f16a0fSKyle Evans if (mountpoint == NULL) {
258a8e44f4dSKyle Evans strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp));
259bfe0869cSKyle Evans if (mkdtemp(mnt_temp) == NULL)
260f1ca70d3SKyle Evans return (set_error(lbh, BE_ERR_IO));
26128f16a0fSKyle Evans }
26228f16a0fSKyle Evans
2630a603a6eSKyle Evans if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
2640a603a6eSKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN));
2650a603a6eSKyle Evans
2660a603a6eSKyle Evans info.lbh = lbh;
2670a603a6eSKyle Evans info.be = be;
2680a603a6eSKyle Evans info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint;
2690a603a6eSKyle Evans info.mntflags = mntflags;
2700a603a6eSKyle Evans info.deepmount = mntdeep;
271011fdcbfSKyle Evans info.depth = 0;
2720a603a6eSKyle Evans
2730a603a6eSKyle Evans if((err = be_mount_iter(zhdl, &info) != 0)) {
2740a603a6eSKyle Evans zfs_close(zhdl);
2750a603a6eSKyle Evans return (err);
276f1ca70d3SKyle Evans }
2770a603a6eSKyle Evans zfs_close(zhdl);
27828f16a0fSKyle Evans
279bfe0869cSKyle Evans if (result_loc != NULL)
280a8e44f4dSKyle Evans strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint,
281a8e44f4dSKyle Evans BE_MAXPATHLEN);
28228f16a0fSKyle Evans
28328f16a0fSKyle Evans return (BE_ERR_SUCCESS);
28428f16a0fSKyle Evans }
28528f16a0fSKyle Evans
28628f16a0fSKyle Evans /*
28728f16a0fSKyle Evans * usage
28828f16a0fSKyle Evans */
28928f16a0fSKyle Evans int
be_unmount(libbe_handle_t * lbh,const char * bootenv,int flags)2905773e924SKyle Evans be_unmount(libbe_handle_t *lbh, const char *bootenv, int flags)
29128f16a0fSKyle Evans {
2920a603a6eSKyle Evans int err;
29328f16a0fSKyle Evans char be[BE_MAXPATHLEN];
294efd6500dSKyle Evans zfs_handle_t *root_hdl;
2950a603a6eSKyle Evans struct be_mount_info info;
29628f16a0fSKyle Evans
297b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, bootenv, be)) != 0)
29828f16a0fSKyle Evans return (set_error(lbh, err));
29928f16a0fSKyle Evans
300efd6500dSKyle Evans if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL)
301efd6500dSKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN));
30228f16a0fSKyle Evans
3030a603a6eSKyle Evans info.lbh = lbh;
3040a603a6eSKyle Evans info.be = be;
3050a603a6eSKyle Evans info.mountpoint = NULL;
3060a603a6eSKyle Evans info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0;
307011fdcbfSKyle Evans info.depth = 0;
30828f16a0fSKyle Evans
3090a603a6eSKyle Evans if ((err = be_umount_iter(root_hdl, &info)) != 0) {
310efd6500dSKyle Evans zfs_close(root_hdl);
3110a603a6eSKyle Evans return (err);
312f1ca70d3SKyle Evans }
31328f16a0fSKyle Evans
3140a603a6eSKyle Evans zfs_close(root_hdl);
315efd6500dSKyle Evans return (BE_ERR_SUCCESS);
31628f16a0fSKyle Evans }
317fc13fc1cSKyle Evans
318fc13fc1cSKyle Evans /*
319fc13fc1cSKyle Evans * This function will blow away the input buffer as needed if we're discovered
320fc13fc1cSKyle Evans * to be looking at a root-mount. If the mountpoint is naturally beyond the
321fc13fc1cSKyle Evans * root, however, the buffer may be left intact and a pointer to the section
322fc13fc1cSKyle Evans * past altroot will be returned instead for the caller's perusal.
323fc13fc1cSKyle Evans */
324fc13fc1cSKyle Evans char *
be_mountpoint_augmented(libbe_handle_t * lbh,char * mountpoint)325fc13fc1cSKyle Evans be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint)
326fc13fc1cSKyle Evans {
327fc13fc1cSKyle Evans
328fc13fc1cSKyle Evans if (lbh->altroot_len == 0)
329fc13fc1cSKyle Evans return (mountpoint);
330fc13fc1cSKyle Evans if (mountpoint == NULL || *mountpoint == '\0')
331fc13fc1cSKyle Evans return (mountpoint);
332fc13fc1cSKyle Evans
333fc13fc1cSKyle Evans if (mountpoint[lbh->altroot_len] == '\0') {
334fc13fc1cSKyle Evans *(mountpoint + 1) = '\0';
335fc13fc1cSKyle Evans return (mountpoint);
336fc13fc1cSKyle Evans } else
337fc13fc1cSKyle Evans return (mountpoint + lbh->altroot_len);
338fc13fc1cSKyle Evans }
339