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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/zfs_context.h>
33 #include <libzfsbootenv.h>
38 static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data);
41 * Returns the name of the active boot environment
44 be_active_name(libbe_handle_t *lbh)
47 if (*lbh->rootfs != '\0')
48 return (strrchr(lbh->rootfs, '/') + sizeof(char));
55 * Returns full path of the active boot environment
58 be_active_path(libbe_handle_t *lbh)
65 * Returns the name of the next active boot environment
68 be_nextboot_name(libbe_handle_t *lbh)
71 if (*lbh->bootfs != '\0')
72 return (strrchr(lbh->bootfs, '/') + sizeof(char));
79 * Returns full path of the active boot environment
82 be_nextboot_path(libbe_handle_t *lbh)
90 * Returns the path of the boot environment root dataset
93 be_root_path(libbe_handle_t *lbh)
101 * Populates dsnvl with one nvlist per bootenv dataset describing the properties
102 * of that dataset that we've declared ourselves to care about.
105 be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
111 data.single_object = false;
112 data.bootonce = NULL;
113 return (be_proplist_update(&data));
117 be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
119 zfs_handle_t *snap_hdl;
125 data.single_object = true;
126 data.bootonce = NULL;
127 if ((snap_hdl = zfs_open(lbh->lzh, name,
128 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL)
129 return (BE_ERR_ZFSOPEN);
131 ret = prop_list_builder_cb(snap_hdl, &data);
137 be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props)
139 zfs_handle_t *ds_hdl;
145 data.single_object = false;
146 data.bootonce = NULL;
147 if ((ds_hdl = zfs_open(lbh->lzh, name,
148 ZFS_TYPE_FILESYSTEM)) == NULL)
149 return (BE_ERR_ZFSOPEN);
151 ret = snapshot_proplist_update(ds_hdl, &data);
157 * Internal callback function used by zfs_iter_filesystems. For each dataset in
158 * the bootenv root, populate an nvlist_t of its relevant properties.
161 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
163 char buf[512], *mountpoint;
167 const char *dataset, *name;
172 * some system for defining constants for the nvlist keys
175 data = (prop_data_t *)data_p;
178 if (data->single_object)
181 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
183 dataset = zfs_get_name(zfs_hdl);
184 nvlist_add_string(props, "dataset", dataset);
186 if (data->bootonce != NULL &&
187 strcmp(dataset, data->bootonce) == 0)
188 nvlist_add_boolean_value(props, "bootonce", true);
190 name = strrchr(dataset, '/') + 1;
191 nvlist_add_string(props, "name", name);
193 mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
196 nvlist_add_string(props, "mounted", mountpoint);
198 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512,
199 NULL, NULL, 0, 1) == 0)
200 nvlist_add_string(props, "mountpoint", buf);
202 if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
203 NULL, NULL, 0, 1) == 0)
204 nvlist_add_string(props, "origin", buf);
206 if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
207 NULL, NULL, 0, 1) == 0)
208 nvlist_add_string(props, "creation", buf);
210 nvlist_add_boolean_value(props, "active",
211 (strcmp(be_active_path(lbh), dataset) == 0));
213 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
214 NULL, NULL, 0, 1) == 0)
215 nvlist_add_string(props, "used", buf);
217 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
218 NULL, NULL, 0, 1) == 0)
219 nvlist_add_string(props, "usedds", buf);
221 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
222 NULL, NULL, 0, 1) == 0)
223 nvlist_add_string(props, "usedsnap", buf);
225 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
226 NULL, NULL, 0, 1) == 0)
227 nvlist_add_string(props, "usedrefreserv", buf);
229 if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
230 NULL, NULL, 0, 1) == 0)
231 nvlist_add_string(props, "referenced", buf);
233 nvlist_add_boolean_value(props, "nextboot",
234 (strcmp(be_nextboot_path(lbh), dataset) == 0));
236 if (!data->single_object)
237 nvlist_add_nvlist(data->list, name, props);
244 * Updates the properties of each bootenv in the libbe handle
245 * XXX TODO: ensure that this is always consistent (run after adds, deletes,
249 be_proplist_update(prop_data_t *data)
251 zfs_handle_t *root_hdl;
253 if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
254 ZFS_TYPE_FILESYSTEM)) == NULL)
255 return (BE_ERR_ZFSOPEN);
257 (void) lzbe_get_boot_device(zpool_get_name(data->lbh->active_phandle),
260 /* XXX TODO: some error checking here */
261 zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
269 snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data)
272 return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data,
278 be_prop_list_alloc(nvlist_t **be_list)
281 return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
285 * frees property list and its children
288 be_prop_list_free(nvlist_t *be_list)
293 be_pair = nvlist_next_nvpair(be_list, NULL);
294 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
295 nvlist_free(prop_list);
297 while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
298 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
299 nvlist_free(prop_list);
308 be_exists(libbe_handle_t *lbh, const char *be)
310 char buf[BE_MAXPATHLEN];
312 be_root_concat(lbh, be, buf);
314 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET))
315 return (BE_ERR_NOENT);
317 return (BE_ERR_SUCCESS);