| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vojtech Trefny <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <blockdev/utils.h> |
| 22 |
#include <ndctl/libndctl.h> |
| 23 |
#include <uuid.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "nvdimm.h" |
| 27 |
#include "check_deps.h" |
| 28 |
|
| 29 |
/** |
| 30 |
* SECTION: nvdimm |
| 31 |
* @short_description: DEPRECATED plugin for operations with nvdimm space |
| 32 |
* @title: NVDIMM |
| 33 |
* @include: nvdimm.h |
| 34 |
* |
| 35 |
* A plugin for operations with NVDIMM devices. |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* bd_nvdimm_error_quark: (skip) |
| 40 |
*/ |
| 41 |
GQuark bd_nvdimm_error_quark (void) { |
| 42 |
return g_quark_from_static_string ("g-bd-nvdimm-error-quark"); |
| 43 |
} |
| 44 |
|
| 45 |
void bd_nvdimm_namespace_info_free (BDNVDIMMNamespaceInfo *info) { |
| 46 |
if (info == NULL) |
| 47 |
return; |
| 48 |
|
| 49 |
g_free (info->dev); |
| 50 |
g_free (info->uuid); |
| 51 |
g_free (info->blockdev); |
| 52 |
g_free (info); |
| 53 |
} |
| 54 |
|
| 55 |
BDNVDIMMNamespaceInfo* bd_nvdimm_namespace_info_copy (BDNVDIMMNamespaceInfo *info) { |
| 56 |
if (info == NULL) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
BDNVDIMMNamespaceInfo *new_info = g_new0 (BDNVDIMMNamespaceInfo, 1); |
| 60 |
|
| 61 |
new_info->dev = g_strdup (info->dev); |
| 62 |
new_info->mode = info->mode; |
| 63 |
new_info->size = info->size; |
| 64 |
new_info->uuid = g_strdup (info->uuid); |
| 65 |
new_info->sector_size = info->sector_size; |
| 66 |
new_info->blockdev = g_strdup (info->blockdev); |
| 67 |
new_info->enabled = info->enabled; |
| 68 |
|
| 69 |
return new_info; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static const gchar * const mode_str[BD_NVDIMM_NAMESPACE_MODE_UNKNOWN+1] = {"raw", "sector", "memory", "dax", "fsdax", "devdax", "unknown"}; |
| 74 |
|
| 75 |
static volatile guint avail_deps = 0; |
| 76 |
static GMutex deps_check_lock; |
| 77 |
|
| 78 |
#define DEPS_NDCTL 0 |
| 79 |
#define DEPS_NDCTL_MASK (1 << DEPS_NDCTL) |
| 80 |
#define DEPS_LAST 1 |
| 81 |
|
| 82 |
static const UtilDep deps[DEPS_LAST] = { |
| 83 |
{"ndctl", NULL, NULL, NULL}, |
| 84 |
}; |
| 85 |
|
| 86 |
/** |
| 87 |
* bd_nvdimm_init: |
| 88 |
* |
| 89 |
* Initializes the plugin. **This function is called automatically by the |
| 90 |
* library's initialization functions.** |
| 91 |
* |
| 92 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 93 |
*/ |
| 94 |
gboolean bd_nvdimm_init (void) { |
| 95 |
/* nothing to do here */ |
| 96 |
return TRUE; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* bd_nvdimm_close: |
| 101 |
* |
| 102 |
* Cleans up after the plugin. **This function is called automatically by the |
| 103 |
* library's functions that unload it.** |
| 104 |
* |
| 105 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 106 |
*/ |
| 107 |
void bd_nvdimm_close (void) { |
| 108 |
g_atomic_int_set (&avail_deps, 0); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* bd_nvdimm_is_tech_avail: |
| 114 |
* @tech: the queried tech |
| 115 |
* @mode: a bit mask of queried modes of operation (#BDNVDIMMTechMode) for @tech |
| 116 |
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available) |
| 117 |
* |
| 118 |
* Returns: whether the @tech-@mode combination is available -- supported by the |
| 119 |
* plugin implementation and having all the runtime dependencies available |
| 120 |
* |
| 121 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 122 |
*/ |
| 123 |
gboolean bd_nvdimm_is_tech_avail (BDNVDIMMTech tech, guint64 mode, GError **error) { |
| 124 |
/* all tech-mode combinations are supported by this implementation of the |
| 125 |
plugin, namespace reconfigure requires the 'ndctl' utility */ |
| 126 |
|
| 127 |
if (tech == BD_NVDIMM_TECH_NAMESPACE) { |
| 128 |
if (mode & BD_NVDIMM_TECH_MODE_RECONFIGURE) |
| 129 |
return check_deps (&avail_deps, DEPS_NDCTL_MASK, deps, DEPS_LAST, &deps_check_lock, error); |
| 130 |
else |
| 131 |
return TRUE; |
| 132 |
} else { |
| 133 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_TECH_UNAVAIL, "Unknown technology"); |
| 134 |
return FALSE; |
| 135 |
} |
| 136 |
|
| 137 |
return TRUE; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* bd_nvdimm_namespace_get_mode_from_str: |
| 142 |
* @mode_str: string representation of mode |
| 143 |
* @error: (out) (optional): place to store error (if any) |
| 144 |
* |
| 145 |
* Returns: mode matching the @mode_str given or %BD_NVDIMM_NAMESPACE_MODE_UNKNOWN in case of no match |
| 146 |
* |
| 147 |
* Tech category: always available |
| 148 |
* |
| 149 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 150 |
*/ |
| 151 |
BDNVDIMMNamespaceMode bd_nvdimm_namespace_get_mode_from_str (const gchar *mode_str, GError **error) { |
| 152 |
if (g_strcmp0 (mode_str, "raw") == 0) |
| 153 |
return BD_NVDIMM_NAMESPACE_MODE_RAW; |
| 154 |
else if (g_strcmp0 (mode_str, "sector") == 0) |
| 155 |
return BD_NVDIMM_NAMESPACE_MODE_SECTOR; |
| 156 |
else if (g_strcmp0 (mode_str, "memory") == 0) |
| 157 |
return BD_NVDIMM_NAMESPACE_MODE_MEMORY; |
| 158 |
else if (g_strcmp0 (mode_str, "dax") == 0) |
| 159 |
return BD_NVDIMM_NAMESPACE_MODE_DAX; |
| 160 |
else if (g_strcmp0 (mode_str, "fsdax") == 0) |
| 161 |
return BD_NVDIMM_NAMESPACE_MODE_FSDAX; |
| 162 |
else if (g_strcmp0 (mode_str, "devdax") == 0) |
| 163 |
return BD_NVDIMM_NAMESPACE_MODE_DEVDAX; |
| 164 |
else { |
| 165 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_MODE_INVAL, |
| 166 |
"Invalid mode given: '%s'", mode_str); |
| 167 |
return BD_NVDIMM_NAMESPACE_MODE_UNKNOWN; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* bd_nvdimm_namespace_get_mode_str: |
| 173 |
* @mode: mode to get string representation of |
| 174 |
* @error: (out) (optional): place to store error (if any) |
| 175 |
* |
| 176 |
* Returns: (transfer none): string representation of @mode or %NULL in case of error |
| 177 |
* |
| 178 |
* Tech category: always available |
| 179 |
* |
| 180 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 181 |
*/ |
| 182 |
const gchar* bd_nvdimm_namespace_get_mode_str (BDNVDIMMNamespaceMode mode, GError **error) { |
| 183 |
if (mode <= BD_NVDIMM_NAMESPACE_MODE_UNKNOWN) |
| 184 |
return mode_str[mode]; |
| 185 |
else { |
| 186 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_MODE_INVAL, |
| 187 |
"Invalid mode given: %d", mode); |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
static struct ndctl_namespace* get_namespace_by_name (const gchar *namespace, struct ndctl_ctx *ctx) { |
| 193 |
struct ndctl_namespace *ndns = NULL; |
| 194 |
struct ndctl_region *region = NULL; |
| 195 |
struct ndctl_bus *bus = NULL; |
| 196 |
|
| 197 |
ndctl_bus_foreach (ctx, bus) { |
| 198 |
ndctl_region_foreach (bus, region) { |
| 199 |
ndctl_namespace_foreach (region, ndns) { |
| 200 |
if (g_strcmp0 (namespace, ndctl_namespace_get_devname (ndns)) == 0) |
| 201 |
return ndns; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* bd_nvdimm_namespace_get_devname: |
| 211 |
* @device: name or path of a block device (e.g. "/dev/pmem0") |
| 212 |
* @error: (out) (optional): place to store error (if any) |
| 213 |
* |
| 214 |
* Returns: (transfer full): namespace device name (e.g. "namespaceX.Y") for @device |
| 215 |
* or %NULL if @device is not a NVDIMM namespace |
| 216 |
* (@error may be set to indicate error) |
| 217 |
* |
| 218 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_QUERY |
| 219 |
* |
| 220 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 221 |
*/ |
| 222 |
gchar* bd_nvdimm_namespace_get_devname (const gchar *device, GError **error) { |
| 223 |
struct ndctl_ctx *ctx = NULL; |
| 224 |
struct ndctl_namespace *ndns = NULL; |
| 225 |
struct ndctl_region *region = NULL; |
| 226 |
struct ndctl_bus *bus = NULL; |
| 227 |
gint success = 0; |
| 228 |
gchar *ret = NULL; |
| 229 |
|
| 230 |
/* get rid of the "/dev/" prefix (if any) */ |
| 231 |
if (g_str_has_prefix (device, "/dev/")) |
| 232 |
device = device + 5; |
| 233 |
|
| 234 |
success = ndctl_new (&ctx); |
| 235 |
if (success != 0) { |
| 236 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 237 |
"Failed to create ndctl context"); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
ndctl_bus_foreach (ctx, bus) { |
| 242 |
ndctl_region_foreach (bus, region) { |
| 243 |
ndctl_namespace_foreach (region, ndns) { |
| 244 |
if (!ndctl_namespace_is_active (ndns)) |
| 245 |
continue; |
| 246 |
|
| 247 |
struct ndctl_btt *btt = ndctl_namespace_get_btt (ndns); |
| 248 |
struct ndctl_dax *dax = ndctl_namespace_get_dax (ndns); |
| 249 |
struct ndctl_pfn *pfn = ndctl_namespace_get_pfn (ndns); |
| 250 |
const gchar *blockdev = NULL; |
| 251 |
|
| 252 |
if (dax) |
| 253 |
continue; |
| 254 |
else if (btt) |
| 255 |
blockdev = ndctl_btt_get_block_device (btt); |
| 256 |
else if (pfn) |
| 257 |
blockdev = ndctl_pfn_get_block_device (pfn); |
| 258 |
else |
| 259 |
blockdev = ndctl_namespace_get_block_device (ndns); |
| 260 |
|
| 261 |
if (g_strcmp0 (blockdev, device) == 0) { |
| 262 |
ret = g_strdup (ndctl_namespace_get_devname (ndns)); |
| 263 |
ndctl_unref (ctx); |
| 264 |
return ret; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
ndctl_unref (ctx); |
| 271 |
return NULL; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* bd_nvdimm_namespace_enable: |
| 276 |
* @namespace: name of the namespace to enable |
| 277 |
* @extra: (nullable) (array zero-terminated=1): extra options (currently unused) |
| 278 |
* @error: (out) (optional): place to store error (if any) |
| 279 |
* |
| 280 |
* Returns: whether the @namespace was successfully enabled or not |
| 281 |
* |
| 282 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_ACTIVATE_DEACTIVATE |
| 283 |
* |
| 284 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 285 |
*/ |
| 286 |
gboolean bd_nvdimm_namespace_enable (const gchar *namespace, const BDExtraArg **extra G_GNUC_UNUSED, GError **error) { |
| 287 |
struct ndctl_ctx *ctx = NULL; |
| 288 |
struct ndctl_namespace *ndns = NULL; |
| 289 |
gint ret = 0; |
| 290 |
|
| 291 |
ret = ndctl_new (&ctx); |
| 292 |
if (ret != 0) { |
| 293 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 294 |
"Failed to create ndctl context"); |
| 295 |
return FALSE; |
| 296 |
} |
| 297 |
|
| 298 |
ndns = get_namespace_by_name (namespace, ctx); |
| 299 |
if (!ndns) { |
| 300 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_NOEXIST, |
| 301 |
"Failed to enable namespace: namespace '%s' not found.", namespace); |
| 302 |
ndctl_unref (ctx); |
| 303 |
return FALSE; |
| 304 |
} |
| 305 |
|
| 306 |
ret = ndctl_namespace_enable (ndns); |
| 307 |
if (ret < 0) { |
| 308 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 309 |
"Failed to enable namespace: %s", strerror (-ret)); |
| 310 |
ndctl_unref (ctx); |
| 311 |
return FALSE; |
| 312 |
} |
| 313 |
|
| 314 |
ndctl_unref (ctx); |
| 315 |
return TRUE; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* bd_nvdimm_namespace_disable: |
| 320 |
* @namespace: name of the namespace to disable |
| 321 |
* @extra: (nullable) (array zero-terminated=1): extra options (currently unused) |
| 322 |
* @error: (out) (optional): place to store error (if any) |
| 323 |
* |
| 324 |
* Returns: whether the @namespace was successfully disabled or not |
| 325 |
* |
| 326 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_ACTIVATE_DEACTIVATE |
| 327 |
* |
| 328 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 329 |
*/ |
| 330 |
gboolean bd_nvdimm_namespace_disable (const gchar *namespace, const BDExtraArg **extra G_GNUC_UNUSED, GError **error) { |
| 331 |
struct ndctl_ctx *ctx = NULL; |
| 332 |
struct ndctl_namespace *ndns = NULL; |
| 333 |
gint ret = 0; |
| 334 |
|
| 335 |
ret = ndctl_new (&ctx); |
| 336 |
if (ret != 0) { |
| 337 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 338 |
"Failed to create ndctl context"); |
| 339 |
return FALSE; |
| 340 |
} |
| 341 |
|
| 342 |
ndns = get_namespace_by_name (namespace, ctx); |
| 343 |
if (!ndns) { |
| 344 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_NOEXIST, |
| 345 |
"Failed to disable namespace: namespace '%s' not found.", namespace); |
| 346 |
ndctl_unref (ctx); |
| 347 |
return FALSE; |
| 348 |
} |
| 349 |
|
| 350 |
ret = ndctl_namespace_disable_safe (ndns); |
| 351 |
if (ret != 0) { |
| 352 |
g_set_error (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 353 |
"Failed to disable namespace: %s", strerror (-ret)); |
| 354 |
ndctl_unref (ctx); |
| 355 |
return FALSE; |
| 356 |
} |
| 357 |
|
| 358 |
ndctl_unref (ctx); |
| 359 |
return TRUE; |
| 360 |
} |
| 361 |
|
| 362 |
static BDNVDIMMNamespaceInfo* get_nvdimm_namespace_info (struct ndctl_namespace *ndns, GError **error) { |
| 363 |
struct ndctl_btt *btt; |
| 364 |
struct ndctl_pfn *pfn; |
| 365 |
struct ndctl_dax *dax; |
| 366 |
enum ndctl_namespace_mode mode; |
| 367 |
gchar uuid_buf[37] = {0}; |
| 368 |
uuid_t uuid; |
| 369 |
|
| 370 |
btt = ndctl_namespace_get_btt (ndns); |
| 371 |
dax = ndctl_namespace_get_dax (ndns); |
| 372 |
pfn = ndctl_namespace_get_pfn (ndns); |
| 373 |
mode = ndctl_namespace_get_mode (ndns); |
| 374 |
|
| 375 |
BDNVDIMMNamespaceInfo *info = g_new0 (BDNVDIMMNamespaceInfo, 1); |
| 376 |
|
| 377 |
info->dev = g_strdup (ndctl_namespace_get_devname (ndns)); |
| 378 |
|
| 379 |
switch (mode) { |
| 380 |
case NDCTL_NS_MODE_MEMORY: |
| 381 |
if (pfn) |
| 382 |
info->size = ndctl_pfn_get_size (pfn); |
| 383 |
else |
| 384 |
info->size = ndctl_namespace_get_size (ndns); |
| 385 |
#ifndef LIBNDCTL_NEW_MODES |
| 386 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_MEMORY; |
| 387 |
#else |
| 388 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_FSDAX; |
| 389 |
#endif |
| 390 |
break; |
| 391 |
case NDCTL_NS_MODE_DAX: |
| 392 |
if (!dax) { |
| 393 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 394 |
"Failed to get information about namespaces: DAX mode " |
| 395 |
"detected but no DAX device found."); |
| 396 |
bd_nvdimm_namespace_info_free (info); |
| 397 |
return NULL; |
| 398 |
} |
| 399 |
info->size = ndctl_dax_get_size (dax); |
| 400 |
#ifndef LIBNDCTL_NEW_MODES |
| 401 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_DAX; |
| 402 |
#else |
| 403 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_DEVDAX; |
| 404 |
#endif |
| 405 |
break; |
| 406 |
case NDCTL_NS_MODE_SAFE: |
| 407 |
if (!btt) { |
| 408 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 409 |
"Failed to get information about namespaces: Sector mode " |
| 410 |
"detected but no BTT device found."); |
| 411 |
bd_nvdimm_namespace_info_free (info); |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
info->size = ndctl_btt_get_size (btt); |
| 415 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_SECTOR; |
| 416 |
break; |
| 417 |
case NDCTL_NS_MODE_RAW: |
| 418 |
info->size = ndctl_namespace_get_size (ndns); |
| 419 |
info->mode = BD_NVDIMM_NAMESPACE_MODE_RAW; |
| 420 |
break; |
| 421 |
default: |
| 422 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 423 |
"Failed to get information about namespaces: Unknown mode."); |
| 424 |
bd_nvdimm_namespace_info_free (info); |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
|
| 428 |
if (btt) { |
| 429 |
ndctl_btt_get_uuid (btt, uuid); |
| 430 |
uuid_unparse (uuid, uuid_buf); |
| 431 |
info->uuid = g_strdup (uuid_buf); |
| 432 |
|
| 433 |
info->blockdev = g_strdup (ndctl_btt_get_block_device (btt)); |
| 434 |
} else if (pfn) { |
| 435 |
ndctl_pfn_get_uuid (pfn, uuid); |
| 436 |
uuid_unparse (uuid, uuid_buf); |
| 437 |
info->uuid = g_strdup (uuid_buf); |
| 438 |
|
| 439 |
info->blockdev = g_strdup (ndctl_pfn_get_block_device (pfn)); |
| 440 |
} else if (dax) { |
| 441 |
ndctl_dax_get_uuid (dax, uuid); |
| 442 |
uuid_unparse (uuid, uuid_buf); |
| 443 |
info->uuid = g_strdup (uuid_buf); |
| 444 |
|
| 445 |
/* no blockdev for dax mode */ |
| 446 |
info->blockdev = NULL; |
| 447 |
} else { |
| 448 |
ndctl_namespace_get_uuid (ndns, uuid); |
| 449 |
|
| 450 |
if (uuid_is_null (uuid)) |
| 451 |
info->uuid = NULL; |
| 452 |
else { |
| 453 |
uuid_unparse (uuid, uuid_buf); |
| 454 |
info->uuid = g_strdup (uuid_buf); |
| 455 |
} |
| 456 |
|
| 457 |
info->blockdev = g_strdup (ndctl_namespace_get_block_device (ndns)); |
| 458 |
} |
| 459 |
|
| 460 |
if (btt) |
| 461 |
info->sector_size = ndctl_btt_get_sector_size (btt); |
| 462 |
else if (dax) |
| 463 |
/* no sector size for dax mode */ |
| 464 |
info->sector_size = 0; |
| 465 |
else { |
| 466 |
info->sector_size = ndctl_namespace_get_sector_size (ndns); |
| 467 |
|
| 468 |
/* apparently the default value for sector size is 512 |
| 469 |
on non DAX namespaces even if libndctl says it's 0 |
| 470 |
https://github.com/pmem/ndctl/commit/a7320456f1bca5edf15352ce977e757fdf78ed58 |
| 471 |
*/ |
| 472 |
|
| 473 |
if (info->sector_size == 0) |
| 474 |
info->sector_size = 512; |
| 475 |
} |
| 476 |
|
| 477 |
info->enabled = ndctl_namespace_is_active (ndns); |
| 478 |
|
| 479 |
return info; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* bd_nvdimm_namespace_info: |
| 484 |
* @namespace: namespace to get information about |
| 485 |
* @extra: (nullable) (array zero-terminated=1): extra options (currently unused) |
| 486 |
* @error: (out) (optional): place to store error (if any) |
| 487 |
* |
| 488 |
* Returns: (transfer full): information about given namespace or %NULL if no such |
| 489 |
* namespace was found (@error may be set to indicate error) |
| 490 |
* |
| 491 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_QUERY |
| 492 |
* |
| 493 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 494 |
*/ |
| 495 |
BDNVDIMMNamespaceInfo* bd_nvdimm_namespace_info (const gchar *namespace, const BDExtraArg **extra G_GNUC_UNUSED, GError **error) { |
| 496 |
struct ndctl_ctx *ctx = NULL; |
| 497 |
struct ndctl_namespace *ndns = NULL; |
| 498 |
BDNVDIMMNamespaceInfo *info = NULL; |
| 499 |
gint ret = 0; |
| 500 |
|
| 501 |
ret = ndctl_new (&ctx); |
| 502 |
if (ret != 0) { |
| 503 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 504 |
"Failed to create ndctl context"); |
| 505 |
return NULL; |
| 506 |
} |
| 507 |
|
| 508 |
ndns = get_namespace_by_name (namespace, ctx); |
| 509 |
if (ndns) { |
| 510 |
info = get_nvdimm_namespace_info (ndns, error); |
| 511 |
ndctl_unref (ctx); |
| 512 |
return info; |
| 513 |
} |
| 514 |
|
| 515 |
ndctl_unref (ctx); |
| 516 |
return NULL; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* bd_nvdimm_list_namespaces: |
| 521 |
* @bus: (nullable): return only namespaces on given bus (specified by name), |
| 522 |
* %NULL may be specified to return namespaces from all buses |
| 523 |
* @region: (nullable): return only namespaces on given region (specified by 'regionX' name), |
| 524 |
* %NULL may be specified to return namespaces from all regions |
| 525 |
* @idle: whether to list idle (not enabled) namespaces too |
| 526 |
* @extra: (nullable) (array zero-terminated=1): extra options (currently unused) |
| 527 |
* @error: (out) (optional): place to store error (if any) |
| 528 |
* |
| 529 |
* Returns: (array zero-terminated=1): information about the namespaces on @bus and @region or |
| 530 |
* %NULL if no namespaces were found (@error may be set to indicate error) |
| 531 |
* |
| 532 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_QUERY |
| 533 |
* |
| 534 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 535 |
*/ |
| 536 |
BDNVDIMMNamespaceInfo** bd_nvdimm_list_namespaces (const gchar *bus_name, const gchar *region_name, gboolean idle, |
| 537 |
const BDExtraArg **extra G_GNUC_UNUSED, GError **error) { |
| 538 |
struct ndctl_ctx *ctx = NULL; |
| 539 |
struct ndctl_namespace *ndns = NULL; |
| 540 |
struct ndctl_region *region = NULL; |
| 541 |
struct ndctl_bus *bus = NULL; |
| 542 |
gint ret = 0; |
| 543 |
BDNVDIMMNamespaceInfo **info = NULL; |
| 544 |
|
| 545 |
GPtrArray *namespaces = g_ptr_array_new (); |
| 546 |
|
| 547 |
ret = ndctl_new (&ctx); |
| 548 |
if (ret != 0) { |
| 549 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_FAIL, |
| 550 |
"Failed to create ndctl context"); |
| 551 |
return NULL; |
| 552 |
} |
| 553 |
|
| 554 |
ndctl_bus_foreach (ctx, bus) { |
| 555 |
if (bus_name && g_strcmp0 (bus_name, ndctl_bus_get_devname (bus)) != 0) |
| 556 |
continue; |
| 557 |
|
| 558 |
ndctl_region_foreach (bus, region) { |
| 559 |
if (region_name && g_strcmp0 (region_name, ndctl_region_get_devname (region)) != 0) |
| 560 |
continue; |
| 561 |
|
| 562 |
ndctl_namespace_foreach (region, ndns) { |
| 563 |
if (!idle && !ndctl_namespace_is_active (ndns)) |
| 564 |
continue; |
| 565 |
|
| 566 |
BDNVDIMMNamespaceInfo *info = get_nvdimm_namespace_info (ndns, error); |
| 567 |
if (!info) { |
| 568 |
g_ptr_array_foreach (namespaces, (GFunc) (void *) bd_nvdimm_namespace_info_free, NULL); |
| 569 |
g_ptr_array_free (namespaces, FALSE); |
| 570 |
ndctl_unref (ctx); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
g_ptr_array_add (namespaces, info); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (namespaces->len == 0) { |
| 580 |
g_ptr_array_free (namespaces, TRUE); |
| 581 |
ndctl_unref (ctx); |
| 582 |
return NULL; |
| 583 |
} |
| 584 |
|
| 585 |
g_ptr_array_add (namespaces, NULL); |
| 586 |
|
| 587 |
info = (BDNVDIMMNamespaceInfo **) g_ptr_array_free (namespaces, FALSE); |
| 588 |
ndctl_unref (ctx); |
| 589 |
|
| 590 |
return info; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* bd_nvdimm_namespace_reconfigure: |
| 595 |
* @namespace: name of the namespace to reconfigure |
| 596 |
* @mode: mode type to set (raw/sector/memory/dax/fsdax/devdax) |
| 597 |
* @force: whether to use force to reconfigure an active namespace |
| 598 |
* @error: (out) (optional): place to store error if any |
| 599 |
* @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now |
| 600 |
* passed to the 'ndctl' utility) |
| 601 |
* |
| 602 |
* Returns: whether @namespace was successfully reconfigured or not |
| 603 |
* |
| 604 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_RECONFIGURE |
| 605 |
* |
| 606 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 607 |
*/ |
| 608 |
gboolean bd_nvdimm_namespace_reconfigure (const gchar* namespace, BDNVDIMMNamespaceMode mode, gboolean force, const BDExtraArg **extra, GError** error) { |
| 609 |
const gchar *args[8] = {"ndctl", "create-namespace", "-e", namespace, "-m", NULL, NULL, NULL}; |
| 610 |
gboolean ret = FALSE; |
| 611 |
const gchar *mode_str = NULL; |
| 612 |
|
| 613 |
if (!check_deps (&avail_deps, DEPS_NDCTL_MASK, deps, DEPS_LAST, &deps_check_lock, error)) |
| 614 |
return FALSE; |
| 615 |
|
| 616 |
mode_str = bd_nvdimm_namespace_get_mode_str (mode, error); |
| 617 |
if (!mode_str) |
| 618 |
/* error is already populated */ |
| 619 |
return FALSE; |
| 620 |
|
| 621 |
args[5] = g_strdup (mode_str); |
| 622 |
|
| 623 |
if (force) |
| 624 |
args[6] = "-f"; |
| 625 |
|
| 626 |
ret = bd_utils_exec_and_report_error (args, extra, error); |
| 627 |
|
| 628 |
g_free ((gchar *) args[5]); |
| 629 |
return ret; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
static guint64 blk_sector_sizes[] = { 512, 520, 528, 4096, 4104, 4160, 4224, 0 }; |
| 634 |
static guint64 pmem_sector_sizes[] = { 512, 4096, 0 }; |
| 635 |
static guint64 io_sector_sizes[] = { 0 }; |
| 636 |
|
| 637 |
/** |
| 638 |
* bd_nvdimm_namespace_get_supported_sector_sizes: |
| 639 |
* @mode: namespace mode |
| 640 |
* @error: (out) (optional): place to store error if any |
| 641 |
* |
| 642 |
* Returns: (transfer none) (array zero-terminated=1): list of supported sector sizes for @mode |
| 643 |
* |
| 644 |
* Tech category: %BD_NVDIMM_TECH_NAMESPACE-%BD_NVDIMM_TECH_MODE_QUERY |
| 645 |
* |
| 646 |
* Deprecated: 3.1: NVDIMM plugin will be removed in the next major release |
| 647 |
*/ |
| 648 |
const guint64 *bd_nvdimm_namespace_get_supported_sector_sizes (BDNVDIMMNamespaceMode mode, GError **error) { |
| 649 |
switch (mode) { |
| 650 |
case BD_NVDIMM_NAMESPACE_MODE_RAW: |
| 651 |
case BD_NVDIMM_NAMESPACE_MODE_MEMORY: |
| 652 |
case BD_NVDIMM_NAMESPACE_MODE_FSDAX: |
| 653 |
return pmem_sector_sizes; |
| 654 |
|
| 655 |
case BD_NVDIMM_NAMESPACE_MODE_DAX: |
| 656 |
case BD_NVDIMM_NAMESPACE_MODE_DEVDAX: |
| 657 |
return io_sector_sizes; |
| 658 |
|
| 659 |
case BD_NVDIMM_NAMESPACE_MODE_SECTOR: |
| 660 |
return blk_sector_sizes; |
| 661 |
|
| 662 |
default: |
| 663 |
g_set_error_literal (error, BD_NVDIMM_ERROR, BD_NVDIMM_ERROR_NAMESPACE_MODE_INVAL, |
| 664 |
"Invalid/unknown mode specified."); |
| 665 |
return NULL; |
| 666 |
} |
| 667 |
} |