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 #include <libzfsbootenv.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 data.bootonce = NULL; 113 return (be_proplist_update(&data)); 114 } 115 116 int 117 be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props) 118 { 119 zfs_handle_t *snap_hdl; 120 prop_data_t data; 121 int ret; 122 123 data.lbh = lbh; 124 data.list = props; 125 data.single_object = true; 126 data.bootonce = NULL; 127 if ((snap_hdl = zfs_open(lbh->lzh, name, 128 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) 129 return (BE_ERR_ZFSOPEN); 130 131 ret = prop_list_builder_cb(snap_hdl, &data); 132 zfs_close(snap_hdl); 133 return (ret); 134 } 135 136 int 137 be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props) 138 { 139 zfs_handle_t *ds_hdl; 140 prop_data_t data; 141 int ret; 142 143 data.lbh = lbh; 144 data.list = props; 145 data.single_object = false; 146 data.bootonce = NULL; 147 if ((ds_hdl = zfs_open(lbh->lzh, name, 148 ZFS_TYPE_FILESYSTEM)) == NULL) 149 return (BE_ERR_ZFSOPEN); 150 151 ret = snapshot_proplist_update(ds_hdl, &data); 152 zfs_close(ds_hdl); 153 return (ret); 154 } 155 156 /* 157 * Internal callback function used by zfs_iter_filesystems. For each dataset in 158 * the bootenv root, populate an nvlist_t of its relevant properties. 159 */ 160 int 161 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p) 162 { 163 char buf[512], *mountpoint; 164 prop_data_t *data; 165 libbe_handle_t *lbh; 166 nvlist_t *props; 167 const char *dataset, *name; 168 boolean_t mounted; 169 170 /* 171 * XXX TODO: 172 * some system for defining constants for the nvlist keys 173 * error checking 174 */ 175 data = (prop_data_t *)data_p; 176 lbh = data->lbh; 177 178 if (data->single_object) 179 props = data->list; 180 else 181 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 182 183 dataset = zfs_get_name(zfs_hdl); 184 nvlist_add_string(props, "dataset", dataset); 185 186 if (data->bootonce != NULL && 187 strcmp(dataset, data->bootonce) == 0) 188 nvlist_add_boolean_value(props, "bootonce", true); 189 190 name = strrchr(dataset, '/') + 1; 191 nvlist_add_string(props, "name", name); 192 193 mounted = zfs_is_mounted(zfs_hdl, &mountpoint); 194 195 if (mounted) 196 nvlist_add_string(props, "mounted", mountpoint); 197 198 if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512, 199 NULL, NULL, 0, 1) == 0) 200 nvlist_add_string(props, "mountpoint", buf); 201 202 if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512, 203 NULL, NULL, 0, 1) == 0) 204 nvlist_add_string(props, "origin", buf); 205 206 if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512, 207 NULL, NULL, 0, 1) == 0) 208 nvlist_add_string(props, "creation", buf); 209 210 nvlist_add_boolean_value(props, "active", 211 (strcmp(be_active_path(lbh), dataset) == 0)); 212 213 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512, 214 NULL, NULL, 0, 1) == 0) 215 nvlist_add_string(props, "used", buf); 216 217 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512, 218 NULL, NULL, 0, 1) == 0) 219 nvlist_add_string(props, "usedds", buf); 220 221 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512, 222 NULL, NULL, 0, 1) == 0) 223 nvlist_add_string(props, "usedsnap", buf); 224 225 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512, 226 NULL, NULL, 0, 1) == 0) 227 nvlist_add_string(props, "usedrefreserv", buf); 228 229 if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512, 230 NULL, NULL, 0, 1) == 0) 231 nvlist_add_string(props, "referenced", buf); 232 233 nvlist_add_boolean_value(props, "nextboot", 234 (strcmp(be_nextboot_path(lbh), dataset) == 0)); 235 236 if (!data->single_object) 237 nvlist_add_nvlist(data->list, name, props); 238 239 return (0); 240 } 241 242 243 /* 244 * Updates the properties of each bootenv in the libbe handle 245 * XXX TODO: ensure that this is always consistent (run after adds, deletes, 246 * renames,etc 247 */ 248 int 249 be_proplist_update(prop_data_t *data) 250 { 251 zfs_handle_t *root_hdl; 252 253 if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root, 254 ZFS_TYPE_FILESYSTEM)) == NULL) 255 return (BE_ERR_ZFSOPEN); 256 257 (void) lzbe_get_boot_device(zpool_get_name(data->lbh->active_phandle), 258 &data->bootonce); 259 260 /* XXX TODO: some error checking here */ 261 zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data); 262 263 zfs_close(root_hdl); 264 265 return (0); 266 } 267 268 static int 269 snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data) 270 { 271 272 return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data, 273 0, 0)); 274 } 275 276 277 int 278 be_prop_list_alloc(nvlist_t **be_list) 279 { 280 281 return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP)); 282 } 283 284 /* 285 * frees property list and its children 286 */ 287 void 288 be_prop_list_free(nvlist_t *be_list) 289 { 290 nvlist_t *prop_list; 291 nvpair_t *be_pair; 292 293 be_pair = nvlist_next_nvpair(be_list, NULL); 294 if (nvpair_value_nvlist(be_pair, &prop_list) == 0) 295 nvlist_free(prop_list); 296 297 while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) { 298 if (nvpair_value_nvlist(be_pair, &prop_list) == 0) 299 nvlist_free(prop_list); 300 } 301 } 302 303 304 /* 305 * Usage 306 */ 307 int 308 be_exists(libbe_handle_t *lbh, const char *be) 309 { 310 char buf[BE_MAXPATHLEN]; 311 312 be_root_concat(lbh, be, buf); 313 314 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET)) 315 return (BE_ERR_NOENT); 316 317 return (BE_ERR_SUCCESS); 318 } 319