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$");
33 #include <sys/zfs_context.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 return (be_proplist_update(&data));
116 be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
118 zfs_handle_t *snap_hdl;
124 data.single_object = true;
125 if ((snap_hdl = zfs_open(lbh->lzh, name,
126 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL)
127 return (BE_ERR_ZFSOPEN);
129 ret = prop_list_builder_cb(snap_hdl, &data);
135 be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props)
137 zfs_handle_t *ds_hdl;
143 data.single_object = false;
144 if ((ds_hdl = zfs_open(lbh->lzh, name,
145 ZFS_TYPE_FILESYSTEM)) == NULL)
146 return (BE_ERR_ZFSOPEN);
148 ret = snapshot_proplist_update(ds_hdl, &data);
154 * Internal callback function used by zfs_iter_filesystems. For each dataset in
155 * the bootenv root, populate an nvlist_t of its relevant properties.
158 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
160 char buf[512], *mountpoint;
164 const char *dataset, *name;
169 * some system for defining constants for the nvlist keys
172 data = (prop_data_t *)data_p;
175 if (data->single_object)
178 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
180 dataset = zfs_get_name(zfs_hdl);
181 nvlist_add_string(props, "dataset", dataset);
183 name = strrchr(dataset, '/') + 1;
184 nvlist_add_string(props, "name", name);
186 mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
189 nvlist_add_string(props, "mounted", mountpoint);
191 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512,
192 NULL, NULL, 0, 1) == 0)
193 nvlist_add_string(props, "mountpoint", buf);
195 if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
196 NULL, NULL, 0, 1) == 0)
197 nvlist_add_string(props, "origin", buf);
199 if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
200 NULL, NULL, 0, 1) == 0)
201 nvlist_add_string(props, "creation", buf);
203 nvlist_add_boolean_value(props, "active",
204 (strcmp(be_active_path(lbh), dataset) == 0));
206 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
207 NULL, NULL, 0, 1) == 0)
208 nvlist_add_string(props, "used", buf);
210 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
211 NULL, NULL, 0, 1) == 0)
212 nvlist_add_string(props, "usedds", buf);
214 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
215 NULL, NULL, 0, 1) == 0)
216 nvlist_add_string(props, "usedsnap", buf);
218 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
219 NULL, NULL, 0, 1) == 0)
220 nvlist_add_string(props, "usedrefreserv", buf);
222 if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
223 NULL, NULL, 0, 1) == 0)
224 nvlist_add_string(props, "referenced", buf);
226 nvlist_add_boolean_value(props, "nextboot",
227 (strcmp(be_nextboot_path(lbh), dataset) == 0));
229 if (!data->single_object)
230 nvlist_add_nvlist(data->list, name, props);
237 * Updates the properties of each bootenv in the libbe handle
238 * XXX TODO: ensure that this is always consistent (run after adds, deletes,
242 be_proplist_update(prop_data_t *data)
244 zfs_handle_t *root_hdl;
246 if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
247 ZFS_TYPE_FILESYSTEM)) == NULL)
248 return (BE_ERR_ZFSOPEN);
250 /* XXX TODO: some error checking here */
251 zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
259 snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data)
262 return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data,
268 be_prop_list_alloc(nvlist_t **be_list)
271 return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
275 * frees property list and its children
278 be_prop_list_free(nvlist_t *be_list)
283 be_pair = nvlist_next_nvpair(be_list, NULL);
284 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
285 nvlist_free(prop_list);
287 while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
288 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
289 nvlist_free(prop_list);
298 be_exists(libbe_handle_t *lbh, char *be)
300 char buf[BE_MAXPATHLEN];
302 be_root_concat(lbh, be, buf);
304 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET))
305 return (BE_ERR_NOENT);
307 return (BE_ERR_SUCCESS);