]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libbe/be_info.c
Merge ^/vendor/lldb/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / lib / libbe / be_info.c
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 <sys/zfs_context.h>
34
35 #include "be.h"
36 #include "be_impl.h"
37
38 static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data);
39
40 /*
41  * Returns the name of the active boot environment
42  */
43 const char *
44 be_active_name(libbe_handle_t *lbh)
45 {
46
47         if (*lbh->rootfs != '\0')
48                 return (strrchr(lbh->rootfs, '/') + sizeof(char));
49         else
50                 return (lbh->rootfs);
51 }
52
53
54 /*
55  * Returns full path of the active boot environment
56  */
57 const char *
58 be_active_path(libbe_handle_t *lbh)
59 {
60
61         return (lbh->rootfs);
62 }
63
64 /*
65  * Returns the name of the next active boot environment
66  */
67 const char *
68 be_nextboot_name(libbe_handle_t *lbh)
69 {
70
71         if (*lbh->bootfs != '\0')
72                 return (strrchr(lbh->bootfs, '/') + sizeof(char));
73         else
74                 return (lbh->bootfs);
75 }
76
77
78 /*
79  * Returns full path of the active boot environment
80  */
81 const char *
82 be_nextboot_path(libbe_handle_t *lbh)
83 {
84
85         return (lbh->bootfs);
86 }
87
88
89 /*
90  * Returns the path of the boot environment root dataset
91  */
92 const char *
93 be_root_path(libbe_handle_t *lbh)
94 {
95
96         return (lbh->root);
97 }
98
99
100 /*
101  * Populates dsnvl with one nvlist per bootenv dataset describing the properties
102  * of that dataset that we've declared ourselves to care about.
103  */
104 int
105 be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
106 {
107         prop_data_t data;
108
109         data.lbh = lbh;
110         data.list = dsnvl;
111         data.single_object = false;
112         return (be_proplist_update(&data));
113 }
114
115 int
116 be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props)
117 {
118         zfs_handle_t *snap_hdl;
119         prop_data_t data;
120         int ret;
121
122         data.lbh = lbh;
123         data.list = props;
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);
128
129         ret = prop_list_builder_cb(snap_hdl, &data);
130         zfs_close(snap_hdl);
131         return (ret);
132 }
133
134 int
135 be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props)
136 {
137         zfs_handle_t *ds_hdl;
138         prop_data_t data;
139         int ret;
140
141         data.lbh = lbh;
142         data.list = props;
143         data.single_object = false;
144         if ((ds_hdl = zfs_open(lbh->lzh, name,
145             ZFS_TYPE_FILESYSTEM)) == NULL)
146                 return (BE_ERR_ZFSOPEN);
147
148         ret = snapshot_proplist_update(ds_hdl, &data);
149         zfs_close(ds_hdl);
150         return (ret);
151 }
152
153 /*
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.
156  */
157 int
158 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
159 {
160         char buf[512], *mountpoint;
161         prop_data_t *data;
162         libbe_handle_t *lbh;
163         nvlist_t *props;
164         const char *dataset, *name;
165         boolean_t mounted;
166
167         /*
168          * XXX TODO:
169          *      some system for defining constants for the nvlist keys
170          *      error checking
171          */
172         data = (prop_data_t *)data_p;
173         lbh = data->lbh;
174
175         if (data->single_object)
176                 props = data->list;
177         else
178                 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
179
180         dataset = zfs_get_name(zfs_hdl);
181         nvlist_add_string(props, "dataset", dataset);
182
183         name = strrchr(dataset, '/') + 1;
184         nvlist_add_string(props, "name", name);
185
186         mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
187
188         if (mounted)
189                 nvlist_add_string(props, "mounted", mountpoint);
190
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);
194
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);
198
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);
202
203         nvlist_add_boolean_value(props, "active",
204             (strcmp(be_active_path(lbh), dataset) == 0));
205
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);
209
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);
213
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);
217
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);
221
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);
225
226         nvlist_add_boolean_value(props, "nextboot",
227             (strcmp(be_nextboot_path(lbh), dataset) == 0));
228
229         if (!data->single_object)
230                 nvlist_add_nvlist(data->list, name, props);
231
232         return (0);
233 }
234
235
236 /*
237  * Updates the properties of each bootenv in the libbe handle
238  * XXX TODO: ensure that this is always consistent (run after adds, deletes,
239  *       renames,etc
240  */
241 int
242 be_proplist_update(prop_data_t *data)
243 {
244         zfs_handle_t *root_hdl;
245
246         if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
247             ZFS_TYPE_FILESYSTEM)) == NULL)
248                 return (BE_ERR_ZFSOPEN);
249
250         /* XXX TODO: some error checking here */
251         zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
252
253         zfs_close(root_hdl);
254
255         return (0);
256 }
257
258 static int
259 snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data)
260 {
261
262         return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data,
263             0, 0));
264 }
265
266
267 int
268 be_prop_list_alloc(nvlist_t **be_list)
269 {
270
271         return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
272 }
273
274 /*
275  * frees property list and its children
276  */
277 void
278 be_prop_list_free(nvlist_t *be_list)
279 {
280         nvlist_t *prop_list;
281         nvpair_t *be_pair;
282
283         be_pair = nvlist_next_nvpair(be_list, NULL);
284         if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
285                 nvlist_free(prop_list);
286
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);
290         }
291 }
292
293
294 /*
295  * Usage
296  */
297 int
298 be_exists(libbe_handle_t *lbh, char *be)
299 {
300         char buf[BE_MAXPATHLEN];
301
302         be_root_concat(lbh, be, buf);
303
304         if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET))
305                 return (BE_ERR_NOENT);
306
307         return (BE_ERR_SUCCESS);
308 }