xref: /freebsd/lib/libbe/be_info.c (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5  * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "be.h"
34 #include "be_impl.h"
35 
36 static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data);
37 
38 /*
39  * Returns the name of the active boot environment
40  */
41 const char *
42 be_active_name(libbe_handle_t *lbh)
43 {
44 
45 	if (*lbh->rootfs != '\0')
46 		return (strrchr(lbh->rootfs, '/') + sizeof(char));
47 	else
48 		return (lbh->rootfs);
49 }
50 
51 
52 /*
53  * Returns full path of the active boot environment
54  */
55 const char *
56 be_active_path(libbe_handle_t *lbh)
57 {
58 
59 	return (lbh->rootfs);
60 }
61 
62 /*
63  * Returns the name of the next active boot environment
64  */
65 const char *
66 be_nextboot_name(libbe_handle_t *lbh)
67 {
68 
69 	if (*lbh->bootfs != '\0')
70 		return (strrchr(lbh->bootfs, '/') + sizeof(char));
71 	else
72 		return (lbh->bootfs);
73 }
74 
75 
76 /*
77  * Returns full path of the active boot environment
78  */
79 const char *
80 be_nextboot_path(libbe_handle_t *lbh)
81 {
82 
83 	return (lbh->bootfs);
84 }
85 
86 
87 /*
88  * Returns the path of the boot environment root dataset
89  */
90 const char *
91 be_root_path(libbe_handle_t *lbh)
92 {
93 
94 	return (lbh->root);
95 }
96 
97 
98 /*
99  * Populates dsnvl with one nvlist per bootenv dataset describing the properties
100  * of that dataset that we've declared ourselves to care about.
101  */
102 int
103 be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
104 {
105 	prop_data_t data;
106 
107 	data.lbh = lbh;
108 	data.list = dsnvl;
109 	data.single_object = false;
110 	return (be_proplist_update(&data));
111 }
112 
113 int
114 be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
115 {
116 	zfs_handle_t *snap_hdl;
117 	prop_data_t data;
118 	int ret;
119 
120 	data.lbh = lbh;
121 	data.list = props;
122 	data.single_object = true;
123 	if ((snap_hdl = zfs_open(lbh->lzh, name,
124 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL)
125 		return (BE_ERR_ZFSOPEN);
126 
127 	ret = prop_list_builder_cb(snap_hdl, &data);
128 	zfs_close(snap_hdl);
129 	return (ret);
130 }
131 
132 int
133 be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props)
134 {
135 	zfs_handle_t *ds_hdl;
136 	prop_data_t data;
137 	int ret;
138 
139 	data.lbh = lbh;
140 	data.list = props;
141 	data.single_object = false;
142 	if ((ds_hdl = zfs_open(lbh->lzh, name,
143 	    ZFS_TYPE_FILESYSTEM)) == NULL)
144 		return (BE_ERR_ZFSOPEN);
145 
146 	ret = snapshot_proplist_update(ds_hdl, &data);
147 	zfs_close(ds_hdl);
148 	return (ret);
149 }
150 
151 /*
152  * Internal callback function used by zfs_iter_filesystems. For each dataset in
153  * the bootenv root, populate an nvlist_t of its relevant properties.
154  */
155 int
156 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
157 {
158 	char buf[512], *mountpoint;
159 	prop_data_t *data;
160 	libbe_handle_t *lbh;
161 	nvlist_t *props;
162 	const char *dataset, *name;
163 	boolean_t mounted;
164 
165 	/*
166 	 * XXX TODO:
167 	 *      some system for defining constants for the nvlist keys
168 	 *      error checking
169 	 */
170 	data = (prop_data_t *)data_p;
171 	lbh = data->lbh;
172 
173 	if (data->single_object)
174 		props = data->list;
175 	else
176 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
177 
178 	dataset = zfs_get_name(zfs_hdl);
179 	nvlist_add_string(props, "dataset", dataset);
180 
181 	name = strrchr(dataset, '/') + 1;
182 	nvlist_add_string(props, "name", name);
183 
184 	mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
185 
186 	if (mounted)
187 		nvlist_add_string(props, "mounted", mountpoint);
188 
189 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512,
190 	    NULL, NULL, 0, 1) == 0)
191 		nvlist_add_string(props, "mountpoint", buf);
192 
193 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
194 	    NULL, NULL, 0, 1) == 0)
195 		nvlist_add_string(props, "origin", buf);
196 
197 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
198 	    NULL, NULL, 0, 1) == 0)
199 		nvlist_add_string(props, "creation", buf);
200 
201 	nvlist_add_boolean_value(props, "active",
202 	    (strcmp(be_active_path(lbh), dataset) == 0));
203 
204 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
205 	    NULL, NULL, 0, 1) == 0)
206 		nvlist_add_string(props, "used", buf);
207 
208 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
209 	    NULL, NULL, 0, 1) == 0)
210 		nvlist_add_string(props, "usedds", buf);
211 
212 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
213 	    NULL, NULL, 0, 1) == 0)
214 		nvlist_add_string(props, "usedsnap", buf);
215 
216 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
217 	    NULL, NULL, 0, 1) == 0)
218 		nvlist_add_string(props, "usedrefreserv", buf);
219 
220 	if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
221 	    NULL, NULL, 0, 1) == 0)
222 		nvlist_add_string(props, "referenced", buf);
223 
224 	nvlist_add_boolean_value(props, "nextboot",
225 	    (strcmp(be_nextboot_path(lbh), dataset) == 0));
226 
227 	if (!data->single_object)
228 		nvlist_add_nvlist(data->list, name, props);
229 
230 	return (0);
231 }
232 
233 
234 /*
235  * Updates the properties of each bootenv in the libbe handle
236  * XXX TODO: ensure that this is always consistent (run after adds, deletes,
237  *       renames,etc
238  */
239 int
240 be_proplist_update(prop_data_t *data)
241 {
242 	zfs_handle_t *root_hdl;
243 
244 	if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
245 	    ZFS_TYPE_FILESYSTEM)) == NULL)
246 		return (BE_ERR_ZFSOPEN);
247 
248 	/* XXX TODO: some error checking here */
249 	zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
250 
251 	zfs_close(root_hdl);
252 
253 	return (0);
254 }
255 
256 static int
257 snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data)
258 {
259 
260 	return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data));
261 }
262 
263 
264 int
265 be_prop_list_alloc(nvlist_t **be_list)
266 {
267 
268 	return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
269 }
270 
271 /*
272  * frees property list and its children
273  */
274 void
275 be_prop_list_free(nvlist_t *be_list)
276 {
277 	nvlist_t *prop_list;
278 	nvpair_t *be_pair;
279 
280 	be_pair = nvlist_next_nvpair(be_list, NULL);
281 	if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
282 		nvlist_free(prop_list);
283 
284 	while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
285 		if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
286 			nvlist_free(prop_list);
287 	}
288 }
289 
290 
291 /*
292  * Usage
293  */
294 int
295 be_exists(libbe_handle_t *lbh, char *be)
296 {
297 	char buf[BE_MAXPATHLEN];
298 
299 	be_root_concat(lbh, be, buf);
300 
301 	if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET))
302 		return (BE_ERR_NOENT);
303 
304 	return (BE_ERR_SUCCESS);
305 }
306