]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libbe/be_info.c
libbe(3): Fix checking of zfs_prop_get's return value
[FreeBSD/FreeBSD.git] / lib / libbe / be_info.c
1 /*
2  * be_info.c
3  *
4  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5  * All rights reserved.
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 "be.h"
30 #include "be_impl.h"
31
32 /*
33  * Returns the name of the active boot environment
34  */
35 const char *
36 be_active_name(libbe_handle_t *lbh)
37 {
38
39         return (strrchr(lbh->rootfs, '/') + sizeof(char));
40 }
41
42
43 /*
44  * Returns full path of the active boot environment
45  */
46 const char *
47 be_active_path(libbe_handle_t *lbh)
48 {
49
50         return (lbh->rootfs);
51 }
52
53 /*
54  * Returns the name of the next active boot environment
55  */
56 const char *
57 be_nextboot_name(libbe_handle_t *lbh)
58 {
59
60         return (strrchr(lbh->bootfs, '/') + sizeof(char));
61 }
62
63
64 /*
65  * Returns full path of the active boot environment
66  */
67 const char *
68 be_nextboot_path(libbe_handle_t *lbh)
69 {
70
71         return (lbh->bootfs);
72 }
73
74
75 /*
76  * Returns the path of the boot environment root dataset
77  */
78 const char *
79 be_root_path(libbe_handle_t *lbh)
80 {
81
82         return (lbh->root);
83 }
84
85
86 /*
87  * Populates dsnvl with one nvlist per bootenv dataset describing the properties
88  * of that dataset that we've declared ourselves to care about.
89  */
90 int
91 be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
92 {
93         prop_data_t data;
94
95         data.lbh = lbh;
96         data.list = dsnvl;
97         return (prop_list_builder(&data));
98 }
99
100
101 /*
102  * Internal callback function used by zfs_iter_filesystems. For each dataset in
103  * the bootenv root, populate an nvlist_t of its relevant properties.
104  * TODO: should any other properties be included?
105  */
106 int
107 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
108 {
109         char buf[512], *mountpoint;
110         prop_data_t *data;
111         libbe_handle_t *lbh;
112         nvlist_t *props;
113         const char *dataset, *name;
114         boolean_t mounted;
115
116         /*
117          * XXX TODO:
118          *      some system for defining constants for the nvlist keys
119          *      error checking
120          */
121         data = (prop_data_t *)data_p;
122         lbh = data->lbh;
123
124         nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
125
126         dataset = zfs_get_name(zfs_hdl);
127         nvlist_add_string(props, "dataset", dataset);
128
129         name = strrchr(dataset, '/') + 1;
130         nvlist_add_string(props, "name", name);
131
132         mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
133         nvlist_add_boolean_value(props, "mounted", mounted);
134
135         if (mounted)
136                 nvlist_add_string(props, "mountpoint", mountpoint);
137
138         if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
139             NULL, NULL, 0, 1) == 0)
140                 nvlist_add_string(props, "origin", buf);
141
142         if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
143             NULL, NULL, 0, 1) == 0)
144                 nvlist_add_string(props, "creation", buf);
145
146         nvlist_add_boolean_value(props, "active",
147             (strcmp(be_active_path(lbh), dataset) == 0));
148
149         if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
150             NULL, NULL, 0, 1) == 0)
151                 nvlist_add_string(props, "used", buf);
152
153         if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
154             NULL, NULL, 0, 1) == 0)
155                 nvlist_add_string(props, "usedds", buf);
156
157         if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
158             NULL, NULL, 0, 1) == 0)
159                 nvlist_add_string(props, "usedsnap", buf);
160
161         if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
162             NULL, NULL, 0, 1) == 0)
163                 nvlist_add_string(props, "usedrefreserv", buf);
164
165         if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
166             NULL, NULL, 0, 1) == 0)
167                 nvlist_add_string(props, "referenced", buf);
168
169         nvlist_add_boolean_value(props, "nextboot",
170             (strcmp(be_nextboot_path(lbh), dataset) == 0));
171
172         nvlist_add_nvlist(data->list, name, props);
173
174         return (0);
175 }
176
177
178 /*
179  * Updates the properties of each bootenv in the libbe handle
180  * XXX TODO: rename to be_proplist_update
181  * XXX TODO: ensure that this is always consistent (run after adds, deletes,
182  *       renames,etc
183  */
184 int
185 prop_list_builder(prop_data_t *data)
186 {
187         zfs_handle_t *root_hdl;
188
189         if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
190             ZFS_TYPE_FILESYSTEM)) == NULL)
191                 return (BE_ERR_ZFSOPEN);
192
193         /* XXX TODO: some error checking here */
194         zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
195
196         zfs_close(root_hdl);
197
198         return (0);
199 }
200
201
202 int
203 be_prop_list_alloc(nvlist_t **be_list)
204 {
205
206         return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
207 }
208
209 /*
210  * frees property list and its children
211  */
212 void
213 be_prop_list_free(nvlist_t *be_list)
214 {
215         nvlist_t *prop_list;
216         nvpair_t *be_pair;
217
218         be_pair = nvlist_next_nvpair(be_list, NULL);
219         if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
220                 nvlist_free(prop_list);
221
222         while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
223                 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
224                         nvlist_free(prop_list);
225         }
226 }
227
228
229 /*
230  * Usage
231  */
232 bool
233 be_exists(libbe_handle_t *lbh, char *be)
234 {
235         char buf[BE_MAXPATHLEN];
236
237         be_root_concat(lbh, be, buf);
238
239         /*
240          * XXX TODO: check mountpoint prop and see if its /, AND that result
241          * with below expression.
242          */
243         return (zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET));
244 }