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