| 1 |
/* |
| 2 |
* Low-level libblkid probing API |
| 3 |
* |
| 4 |
* Copyright (C) 2008-2009 Karel Zak <[email protected]> |
| 5 |
* |
| 6 |
* This file may be redistributed under the terms of the |
| 7 |
* GNU Lesser General Public License. |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* SECTION: lowprobe |
| 12 |
* @title: Low-level probing |
| 13 |
* @short_description: low-level prober initialization |
| 14 |
* |
| 15 |
* The low-level probing routines always and directly read information from |
| 16 |
* the selected (see blkid_probe_set_device()) device. |
| 17 |
* |
| 18 |
* The probing routines are grouped together into separate chains. Currently, |
| 19 |
* the library provides superblocks, partitions and topology chains. |
| 20 |
* |
| 21 |
* The probing routines is possible to filter (enable/disable) by type (e.g. |
| 22 |
* fstype "vfat" or partype "gpt") or by usage flags (e.g. BLKID_USAGE_RAID). |
| 23 |
* These filters are per-chain. Note that always when you touch the chain |
| 24 |
* filter the current probing position is reset and probing starts from |
| 25 |
* scratch. It means that the chain filter should not be modified during |
| 26 |
* probing, for example in loop where you call blkid_do_probe(). |
| 27 |
* |
| 28 |
* For more details see the chain specific documentation. |
| 29 |
* |
| 30 |
* The low-level API provides two ways how access to probing results. |
| 31 |
* |
| 32 |
* 1. The NAME=value (tag) interface. This interface is older and returns all data |
| 33 |
* as strings. This interface is generic for all chains. |
| 34 |
* |
| 35 |
* 2. The binary interfaces. These interfaces return data in the native formats. |
| 36 |
* The interface is always specific to the probing chain. |
| 37 |
* |
| 38 |
* Note that the previous probing result (binary or NAME=value) is always |
| 39 |
* zeroized when a chain probing function is called. For example: |
| 40 |
* |
| 41 |
* <informalexample> |
| 42 |
* <programlisting> |
| 43 |
* blkid_probe_enable_partitions(pr, TRUE); |
| 44 |
* blkid_probe_enable_superblocks(pr, FALSE); |
| 45 |
* |
| 46 |
* blkid_do_safeprobe(pr); |
| 47 |
* </programlisting> |
| 48 |
* </informalexample> |
| 49 |
* |
| 50 |
* overwrites the previous probing result for the partitions chain, the superblocks |
| 51 |
* result is not modified. |
| 52 |
*/ |
| 53 |
|
| 54 |
/** |
| 55 |
* SECTION: lowprobe-tags |
| 56 |
* @title: Low-level tags |
| 57 |
* @short_description: generic NAME=value interface. |
| 58 |
* |
| 59 |
* The probing routines inside the chain are mutually exclusive by default -- |
| 60 |
* only few probing routines are marked as "tolerant". The "tolerant" probing |
| 61 |
* routines are used for filesystem which can share the same device with any |
| 62 |
* other filesystem. The blkid_do_safeprobe() checks for the "tolerant" flag. |
| 63 |
* |
| 64 |
* The SUPERBLOCKS chain is enabled by default. The all others chains is |
| 65 |
* necessary to enable by blkid_probe_enable_'CHAINNAME'(). See chains specific |
| 66 |
* documentation. |
| 67 |
* |
| 68 |
* The blkid_do_probe() function returns a result from only one probing |
| 69 |
* routine, and the next call from the next probing routine. It means you need |
| 70 |
* to call the function in loop, for example: |
| 71 |
* |
| 72 |
* <informalexample> |
| 73 |
* <programlisting> |
| 74 |
* while((blkid_do_probe(pr) == BLKID_PROBE_OK) |
| 75 |
* ... use result ... |
| 76 |
* </programlisting> |
| 77 |
* </informalexample> |
| 78 |
* |
| 79 |
* The blkid_do_safeprobe() is the same as blkid_do_probe(), but returns only |
| 80 |
* first probing result for every enabled chain. This function checks for |
| 81 |
* ambivalent results (e.g. more "intolerant" filesystems superblocks on the |
| 82 |
* device). |
| 83 |
* |
| 84 |
* The probing result is set of NAME=value pairs (the NAME is always unique). |
| 85 |
*/ |
| 86 |
|
| 87 |
#include <stdio.h> |
| 88 |
#include <string.h> |
| 89 |
#include <stdlib.h> |
| 90 |
#include <unistd.h> |
| 91 |
#include <fcntl.h> |
| 92 |
#include <ctype.h> |
| 93 |
#include <sys/mman.h> |
| 94 |
#include <sys/types.h> |
| 95 |
#ifdef HAVE_LINUX_CDROM_H |
| 96 |
#include <linux/cdrom.h> |
| 97 |
#endif |
| 98 |
#ifdef HAVE_LINUX_BLKZONED_H |
| 99 |
#include <linux/blkzoned.h> |
| 100 |
#endif |
| 101 |
#ifdef HAVE_SYS_STAT_H |
| 102 |
#include <sys/stat.h> |
| 103 |
#endif |
| 104 |
#ifdef HAVE_ERRNO_H |
| 105 |
#include <errno.h> |
| 106 |
#endif |
| 107 |
#ifdef HAVE_LINUX_FD_H |
| 108 |
#include <linux/fd.h> |
| 109 |
#endif |
| 110 |
#include <inttypes.h> |
| 111 |
#include <stdint.h> |
| 112 |
#include <stdarg.h> |
| 113 |
#include <limits.h> |
| 114 |
#ifdef HAVE_OPAL_GET_STATUS |
| 115 |
#include <linux/sed-opal.h> |
| 116 |
#endif |
| 117 |
|
| 118 |
#include "blkidP.h" |
| 119 |
#include "all-io.h" |
| 120 |
#include "sysfs.h" |
| 121 |
#include "strutils.h" |
| 122 |
#include "list.h" |
| 123 |
#include "fileutils.h" |
| 124 |
|
| 125 |
/* |
| 126 |
* All supported chains |
| 127 |
*/ |
| 128 |
static const struct blkid_chaindrv *const chains_drvs[] = { |
| 129 |
[BLKID_CHAIN_SUBLKS] = &superblocks_drv, |
| 130 |
[BLKID_CHAIN_TOPLGY] = &topology_drv, |
| 131 |
[BLKID_CHAIN_PARTS] = &partitions_drv |
| 132 |
}; |
| 133 |
|
| 134 |
static void blkid_probe_reset_values(blkid_probe pr); |
| 135 |
|
| 136 |
/** |
| 137 |
* blkid_new_probe: |
| 138 |
* |
| 139 |
* Returns: a pointer to the newly allocated probe struct or NULL in case of error. |
| 140 |
*/ |
| 141 |
blkid_probe blkid_new_probe(void) |
| 142 |
{ |
| 143 |
int i; |
| 144 |
blkid_probe pr; |
| 145 |
|
| 146 |
pr = calloc(1, sizeof(struct blkid_struct_probe)); |
| 147 |
if (!pr) |
| 148 |
return NULL; |
| 149 |
|
| 150 |
DBG(LOWPROBE, ul_debug("allocate a new probe")); |
| 151 |
|
| 152 |
/* initialize chains */ |
| 153 |
for (i = 0; i < BLKID_NCHAINS; i++) { |
| 154 |
pr->chains[i].driver = chains_drvs[i]; |
| 155 |
pr->chains[i].flags = chains_drvs[i]->dflt_flags; |
| 156 |
pr->chains[i].enabled = chains_drvs[i]->dflt_enabled; |
| 157 |
} |
| 158 |
INIT_LIST_HEAD(&pr->buffers); |
| 159 |
INIT_LIST_HEAD(&pr->prunable_buffers); |
| 160 |
INIT_LIST_HEAD(&pr->values); |
| 161 |
INIT_LIST_HEAD(&pr->hints); |
| 162 |
return pr; |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* Clone @parent, the new clone shares all, but except: |
| 167 |
* |
| 168 |
* - probing result |
| 169 |
* - buffers if another device (or offset) is set to the prober |
| 170 |
*/ |
| 171 |
blkid_probe blkid_clone_probe(blkid_probe parent) |
| 172 |
{ |
| 173 |
blkid_probe pr; |
| 174 |
|
| 175 |
if (!parent) |
| 176 |
return NULL; |
| 177 |
|
| 178 |
DBG(LOWPROBE, ul_debug("allocate a probe clone")); |
| 179 |
|
| 180 |
pr = blkid_new_probe(); |
| 181 |
if (!pr) |
| 182 |
return NULL; |
| 183 |
|
| 184 |
pr->fd = parent->fd; |
| 185 |
pr->off = parent->off; |
| 186 |
pr->size = parent->size; |
| 187 |
pr->io_size = parent->io_size; |
| 188 |
pr->devno = parent->devno; |
| 189 |
pr->disk_devno = parent->disk_devno; |
| 190 |
pr->blkssz = parent->blkssz; |
| 191 |
pr->flags = parent->flags; |
| 192 |
pr->zone_size = parent->zone_size; |
| 193 |
pr->parent = parent; |
| 194 |
|
| 195 |
pr->flags &= ~BLKID_FL_PRIVATE_FD; |
| 196 |
|
| 197 |
return pr; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* blkid_new_probe_from_filename: |
| 204 |
* @filename: device or regular file |
| 205 |
* |
| 206 |
* This function is same as call open(filename), blkid_new_probe() and |
| 207 |
* blkid_probe_set_device(pr, fd, 0, 0). |
| 208 |
* |
| 209 |
* The @filename is closed by blkid_free_probe() or by the |
| 210 |
* blkid_probe_set_device() call. |
| 211 |
* |
| 212 |
* Returns: a pointer to the newly allocated probe struct or NULL in case of |
| 213 |
* error. |
| 214 |
*/ |
| 215 |
blkid_probe blkid_new_probe_from_filename(const char *filename) |
| 216 |
{ |
| 217 |
int fd; |
| 218 |
blkid_probe pr = NULL; |
| 219 |
struct stat sb; |
| 220 |
|
| 221 |
/* |
| 222 |
* Check for hidden device-mapper devices (LVM internals, etc.) |
| 223 |
* before open() to avoid bumping the kernel open count. A racing |
| 224 |
* DM_DEVICE_REMOVE would otherwise see EBUSY. |
| 225 |
* |
| 226 |
* Use sysfs_devno_is_dm_hidden() rather than _dm_private() so that |
| 227 |
* Stratis devices remain accessible to tools like mkfs.xfs that |
| 228 |
* need to probe device geometry. |
| 229 |
*/ |
| 230 |
if (stat(filename, &sb) == 0 && S_ISBLK(sb.st_mode) && |
| 231 |
sysfs_devno_is_dm_hidden(sb.st_rdev, NULL)) { |
| 232 |
DBG(LOWPROBE, ul_debug("ignore hidden device mapper device")); |
| 233 |
errno = EINVAL; |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
|
| 237 |
fd = open(filename, O_RDONLY | O_CLOEXEC | O_NONBLOCK); |
| 238 |
if (fd < 0) |
| 239 |
return NULL; |
| 240 |
|
| 241 |
pr = blkid_new_probe(); |
| 242 |
if (!pr) |
| 243 |
goto err; |
| 244 |
|
| 245 |
if (blkid_probe_set_device(pr, fd, 0, 0)) |
| 246 |
goto err; |
| 247 |
|
| 248 |
pr->flags |= BLKID_FL_PRIVATE_FD; |
| 249 |
return pr; |
| 250 |
err: |
| 251 |
close(fd); |
| 252 |
blkid_free_probe(pr); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* blkid_free_probe: |
| 258 |
* @pr: probe |
| 259 |
* |
| 260 |
* Deallocates the probe struct, buffers and all allocated |
| 261 |
* data that are associated with this probing control struct. |
| 262 |
*/ |
| 263 |
void blkid_free_probe(blkid_probe pr) |
| 264 |
{ |
| 265 |
int i; |
| 266 |
|
| 267 |
if (!pr) |
| 268 |
return; |
| 269 |
|
| 270 |
for (i = 0; i < BLKID_NCHAINS; i++) { |
| 271 |
struct blkid_chain *ch = &pr->chains[i]; |
| 272 |
|
| 273 |
if (ch->driver->free_data) |
| 274 |
ch->driver->free_data(pr, ch->data); |
| 275 |
free(ch->fltr); |
| 276 |
ch->fltr = NULL; |
| 277 |
} |
| 278 |
|
| 279 |
if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0) |
| 280 |
close(pr->fd); |
| 281 |
blkid_probe_reset_buffers(pr); |
| 282 |
blkid_probe_reset_values(pr); |
| 283 |
blkid_probe_reset_hints(pr); |
| 284 |
blkid_free_probe(pr->disk_probe); |
| 285 |
|
| 286 |
DBG(LOWPROBE, ul_debug("free probe")); |
| 287 |
free(pr); |
| 288 |
} |
| 289 |
|
| 290 |
void blkid_probe_free_value(struct blkid_prval *v) |
| 291 |
{ |
| 292 |
if (!v) |
| 293 |
return; |
| 294 |
|
| 295 |
list_del(&v->prvals); |
| 296 |
free(v->data); |
| 297 |
|
| 298 |
DBG(LOWPROBE, ul_debug(" free value %s", v->name)); |
| 299 |
free(v); |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* Removes chain values from probing result. |
| 304 |
*/ |
| 305 |
void blkid_probe_chain_reset_values(blkid_probe pr, struct blkid_chain *chn) |
| 306 |
{ |
| 307 |
|
| 308 |
struct list_head *p, *pnext; |
| 309 |
|
| 310 |
if (list_empty(&pr->values)) |
| 311 |
return; |
| 312 |
|
| 313 |
DBG(LOWPROBE, ul_debug("Resetting %s values", chn->driver->name)); |
| 314 |
|
| 315 |
list_for_each_safe(p, pnext, &pr->values) { |
| 316 |
struct blkid_prval *v = list_entry(p, |
| 317 |
struct blkid_prval, prvals); |
| 318 |
|
| 319 |
if (v->chain == chn) |
| 320 |
blkid_probe_free_value(v); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
static void blkid_probe_chain_reset_position(struct blkid_chain *chn) |
| 325 |
{ |
| 326 |
chn->idx = -1; |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* Move chain values from probing result to @vals |
| 331 |
*/ |
| 332 |
int blkid_probe_chain_save_values(blkid_probe pr, struct blkid_chain *chn, |
| 333 |
struct list_head *vals) |
| 334 |
{ |
| 335 |
struct list_head *p, *pnext; |
| 336 |
struct blkid_prval *v; |
| 337 |
|
| 338 |
DBG(LOWPROBE, ul_debug("saving %s values", chn->driver->name)); |
| 339 |
|
| 340 |
list_for_each_safe(p, pnext, &pr->values) { |
| 341 |
|
| 342 |
v = list_entry(p, struct blkid_prval, prvals); |
| 343 |
if (v->chain != chn) |
| 344 |
continue; |
| 345 |
|
| 346 |
list_del_init(&v->prvals); |
| 347 |
list_add_tail(&v->prvals, vals); |
| 348 |
} |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
* Appends values from @vals to the probing result |
| 354 |
*/ |
| 355 |
void blkid_probe_append_values_list(blkid_probe pr, struct list_head *vals) |
| 356 |
{ |
| 357 |
DBG(LOWPROBE, ul_debug("appending values")); |
| 358 |
|
| 359 |
list_splice(vals, &pr->values); |
| 360 |
INIT_LIST_HEAD(vals); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
void blkid_probe_free_values_list(struct list_head *vals) |
| 365 |
{ |
| 366 |
if (!vals) |
| 367 |
return; |
| 368 |
|
| 369 |
DBG(LOWPROBE, ul_debug("freeing values list")); |
| 370 |
|
| 371 |
while (!list_empty(vals)) { |
| 372 |
struct blkid_prval *v = list_entry(vals->next, struct blkid_prval, prvals); |
| 373 |
blkid_probe_free_value(v); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
struct blkid_chain *blkid_probe_get_chain(blkid_probe pr) |
| 378 |
{ |
| 379 |
return pr->cur_chain; |
| 380 |
} |
| 381 |
|
| 382 |
static const char *blkid_probe_get_probername(blkid_probe pr) |
| 383 |
{ |
| 384 |
struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 385 |
|
| 386 |
if (chn && chn->idx >= 0 && (unsigned)chn->idx < chn->driver->nidinfos) |
| 387 |
return chn->driver->idinfos[chn->idx]->name; |
| 388 |
|
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
void *blkid_probe_get_binary_data(blkid_probe pr, struct blkid_chain *chn) |
| 393 |
{ |
| 394 |
int rc, org_prob_flags; |
| 395 |
struct blkid_chain *org_chn; |
| 396 |
|
| 397 |
/* save the current setting -- the binary API has to be completely |
| 398 |
* independent on the current probing status |
| 399 |
*/ |
| 400 |
org_chn = pr->cur_chain; |
| 401 |
org_prob_flags = pr->prob_flags; |
| 402 |
|
| 403 |
pr->cur_chain = chn; |
| 404 |
pr->prob_flags = 0; |
| 405 |
chn->binary = TRUE; |
| 406 |
blkid_probe_chain_reset_position(chn); |
| 407 |
|
| 408 |
rc = chn->driver->probe(pr, chn); |
| 409 |
|
| 410 |
chn->binary = FALSE; |
| 411 |
blkid_probe_chain_reset_position(chn); |
| 412 |
|
| 413 |
/* restore the original setting |
| 414 |
*/ |
| 415 |
pr->cur_chain = org_chn; |
| 416 |
pr->prob_flags = org_prob_flags; |
| 417 |
|
| 418 |
if (rc != 0) |
| 419 |
return NULL; |
| 420 |
|
| 421 |
DBG(LOWPROBE, ul_debug("returning %s binary data", chn->driver->name)); |
| 422 |
return chn->data; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
/** |
| 427 |
* blkid_reset_probe: |
| 428 |
* @pr: probe |
| 429 |
* |
| 430 |
* Zeroize probing results and resets the current probing (this has impact to |
| 431 |
* blkid_do_probe() only). This function does not touch probing filters and |
| 432 |
* keeps assigned device. |
| 433 |
*/ |
| 434 |
void blkid_reset_probe(blkid_probe pr) |
| 435 |
{ |
| 436 |
int i; |
| 437 |
|
| 438 |
blkid_probe_reset_values(pr); |
| 439 |
blkid_probe_set_wiper(pr, 0, 0); |
| 440 |
|
| 441 |
pr->cur_chain = NULL; |
| 442 |
|
| 443 |
for (i = 0; i < BLKID_NCHAINS; i++) |
| 444 |
blkid_probe_chain_reset_position(&pr->chains[i]); |
| 445 |
} |
| 446 |
|
| 447 |
/*** |
| 448 |
static int blkid_probe_dump_filter(blkid_probe pr, int chain) |
| 449 |
{ |
| 450 |
struct blkid_chain *chn; |
| 451 |
int i; |
| 452 |
|
| 453 |
if (!pr || chain < 0 || chain >= BLKID_NCHAINS) |
| 454 |
return -1; |
| 455 |
|
| 456 |
chn = &pr->chains[chain]; |
| 457 |
|
| 458 |
if (!chn->fltr) |
| 459 |
return -1; |
| 460 |
|
| 461 |
for (i = 0; i < chn->driver->nidinfos; i++) { |
| 462 |
const struct blkid_idinfo *id = chn->driver->idinfos[i]; |
| 463 |
|
| 464 |
DBG(LOWPROBE, ul_debug("%d: %s: %s", |
| 465 |
i, |
| 466 |
id->name, |
| 467 |
blkid_bmp_get_item(chn->fltr, i) |
| 468 |
? "disabled" : "enabled <--")); |
| 469 |
} |
| 470 |
return 0; |
| 471 |
} |
| 472 |
***/ |
| 473 |
|
| 474 |
/* |
| 475 |
* Returns properly initialized chain filter |
| 476 |
*/ |
| 477 |
unsigned long *blkid_probe_get_filter(blkid_probe pr, int chain, int create) |
| 478 |
{ |
| 479 |
struct blkid_chain *chn; |
| 480 |
|
| 481 |
if (chain < 0 || chain >= BLKID_NCHAINS) |
| 482 |
return NULL; |
| 483 |
|
| 484 |
chn = &pr->chains[chain]; |
| 485 |
|
| 486 |
/* always when you touch the chain filter all indexes are reset and |
| 487 |
* probing starts from scratch |
| 488 |
*/ |
| 489 |
blkid_probe_chain_reset_position(chn); |
| 490 |
pr->cur_chain = NULL; |
| 491 |
|
| 492 |
if (!chn->driver->has_fltr || (!chn->fltr && !create)) |
| 493 |
return NULL; |
| 494 |
|
| 495 |
if (!chn->fltr) |
| 496 |
chn->fltr = calloc(1, blkid_bmp_nbytes(chn->driver->nidinfos)); |
| 497 |
else |
| 498 |
memset(chn->fltr, 0, blkid_bmp_nbytes(chn->driver->nidinfos)); |
| 499 |
|
| 500 |
/* blkid_probe_dump_filter(pr, chain); */ |
| 501 |
return chn->fltr; |
| 502 |
} |
| 503 |
|
| 504 |
/* |
| 505 |
* Generic private functions for filter setting |
| 506 |
*/ |
| 507 |
int __blkid_probe_invert_filter(blkid_probe pr, int chain) |
| 508 |
{ |
| 509 |
size_t i; |
| 510 |
struct blkid_chain *chn; |
| 511 |
|
| 512 |
chn = &pr->chains[chain]; |
| 513 |
|
| 514 |
if (!chn->driver->has_fltr || !chn->fltr) |
| 515 |
return -1; |
| 516 |
|
| 517 |
for (i = 0; i < blkid_bmp_nwords(chn->driver->nidinfos); i++) |
| 518 |
chn->fltr[i] = ~chn->fltr[i]; |
| 519 |
|
| 520 |
DBG(LOWPROBE, ul_debug("probing filter inverted")); |
| 521 |
/* blkid_probe_dump_filter(pr, chain); */ |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
int __blkid_probe_reset_filter(blkid_probe pr, int chain) |
| 526 |
{ |
| 527 |
return blkid_probe_get_filter(pr, chain, FALSE) ? 0 : -1; |
| 528 |
} |
| 529 |
|
| 530 |
int __blkid_probe_filter_types(blkid_probe pr, int chain, int flag, char *names[]) |
| 531 |
{ |
| 532 |
unsigned long *fltr; |
| 533 |
struct blkid_chain *chn; |
| 534 |
size_t i; |
| 535 |
|
| 536 |
fltr = blkid_probe_get_filter(pr, chain, TRUE); |
| 537 |
if (!fltr) |
| 538 |
return -1; |
| 539 |
|
| 540 |
chn = &pr->chains[chain]; |
| 541 |
|
| 542 |
for (i = 0; i < chn->driver->nidinfos; i++) { |
| 543 |
int has = 0; |
| 544 |
const struct blkid_idinfo *id = chn->driver->idinfos[i]; |
| 545 |
char **n; |
| 546 |
|
| 547 |
for (n = names; *n; n++) { |
| 548 |
if (!strcmp(id->name, *n)) { |
| 549 |
has = 1; |
| 550 |
break; |
| 551 |
} |
| 552 |
} |
| 553 |
if (has) { |
| 554 |
if (flag & BLKID_FLTR_NOTIN) |
| 555 |
blkid_bmp_set_item(fltr, i); |
| 556 |
} else if (flag & BLKID_FLTR_ONLYIN) |
| 557 |
blkid_bmp_set_item(fltr, i); |
| 558 |
} |
| 559 |
|
| 560 |
DBG(LOWPROBE, ul_debug("%s: a new probing type-filter initialized", |
| 561 |
chn->driver->name)); |
| 562 |
/* blkid_probe_dump_filter(pr, chain); */ |
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
static void remove_buffer(struct blkid_bufinfo *bf) |
| 567 |
{ |
| 568 |
list_del(&bf->bufs); |
| 569 |
|
| 570 |
DBG(BUFFER, ul_debug(" remove buffer: [off=%"PRIu64", len=%"PRIu64"]", |
| 571 |
bf->off, bf->len)); |
| 572 |
munmap(bf->data, bf->len); |
| 573 |
free(bf); |
| 574 |
} |
| 575 |
|
| 576 |
static struct blkid_bufinfo *read_buffer(blkid_probe pr, uint64_t real_off, uint64_t len) |
| 577 |
{ |
| 578 |
ssize_t ret; |
| 579 |
struct blkid_bufinfo *bf = NULL; |
| 580 |
|
| 581 |
if (lseek(pr->fd, real_off, SEEK_SET) == (off_t) -1) { |
| 582 |
errno = 0; |
| 583 |
return NULL; |
| 584 |
} |
| 585 |
|
| 586 |
/* someone trying to overflow some buffers? */ |
| 587 |
if (len > ULONG_MAX - sizeof(struct blkid_bufinfo)) { |
| 588 |
errno = ENOMEM; |
| 589 |
return NULL; |
| 590 |
} |
| 591 |
|
| 592 |
/* allocate info and space for data by one malloc call */ |
| 593 |
bf = calloc(1, sizeof(struct blkid_bufinfo)); |
| 594 |
if (!bf) { |
| 595 |
errno = ENOMEM; |
| 596 |
return NULL; |
| 597 |
} |
| 598 |
|
| 599 |
bf->data = mmap(NULL, len, PROT_READ | PROT_WRITE, |
| 600 |
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 601 |
if (bf->data == MAP_FAILED) { |
| 602 |
free(bf); |
| 603 |
errno = ENOMEM; |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
|
| 607 |
bf->len = len; |
| 608 |
bf->off = real_off; |
| 609 |
INIT_LIST_HEAD(&bf->bufs); |
| 610 |
|
| 611 |
DBG(LOWPROBE, ul_debug("\tread: off=%"PRIu64" len=%"PRIu64"", |
| 612 |
real_off, len)); |
| 613 |
|
| 614 |
ret = read(pr->fd, bf->data, len); |
| 615 |
if (ret != (ssize_t) len) { |
| 616 |
DBG(LOWPROBE, ul_debug("\tread failed: %m")); |
| 617 |
remove_buffer(bf); |
| 618 |
|
| 619 |
/* I/O errors on CDROMs are non-fatal to work with hybrid |
| 620 |
* audio+data disks */ |
| 621 |
if (ret >= 0 || blkid_probe_is_cdrom(pr) || blkdid_probe_is_opal_locked(pr)) |
| 622 |
errno = 0; |
| 623 |
|
| 624 |
return NULL; |
| 625 |
} |
| 626 |
|
| 627 |
if (mprotect(bf->data, len, PROT_READ)) |
| 628 |
DBG(LOWPROBE, ul_debug("\tmprotect failed: %m")); |
| 629 |
|
| 630 |
return bf; |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* Search in buffers we already have in memory |
| 635 |
*/ |
| 636 |
static struct blkid_bufinfo *get_cached_buffer(blkid_probe pr, uint64_t off, uint64_t len) |
| 637 |
{ |
| 638 |
uint64_t real_off = pr->off + off; |
| 639 |
struct list_head *p; |
| 640 |
|
| 641 |
list_for_each(p, &pr->buffers) { |
| 642 |
struct blkid_bufinfo *x = |
| 643 |
list_entry(p, struct blkid_bufinfo, bufs); |
| 644 |
|
| 645 |
if (real_off >= x->off && real_off + len <= x->off + x->len) { |
| 646 |
DBG(BUFFER, ul_debug("\treuse: off=%"PRIu64" len=%"PRIu64" (for off=%"PRIu64" len=%"PRIu64")", |
| 647 |
x->off, x->len, real_off, len)); |
| 648 |
return x; |
| 649 |
} |
| 650 |
} |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* Mark smaller buffers that can be satisfied by bf as prunable |
| 656 |
*/ |
| 657 |
static void mark_prunable_buffers(blkid_probe pr, const struct blkid_bufinfo *bf) |
| 658 |
{ |
| 659 |
struct list_head *p, *next; |
| 660 |
|
| 661 |
list_for_each_safe(p, next, &pr->buffers) { |
| 662 |
struct blkid_bufinfo *x = |
| 663 |
list_entry(p, struct blkid_bufinfo, bufs); |
| 664 |
|
| 665 |
if (bf->off <= x->off && bf->off + bf->len >= x->off + x->len) { |
| 666 |
list_del(&x->bufs); |
| 667 |
list_add(&x->bufs, &pr->prunable_buffers); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Remove buffers that are marked as prunable |
| 674 |
*/ |
| 675 |
void blkid_probe_prune_buffers(blkid_probe pr) |
| 676 |
{ |
| 677 |
struct list_head *p, *next; |
| 678 |
|
| 679 |
list_for_each_safe(p, next, &pr->prunable_buffers) { |
| 680 |
struct blkid_bufinfo *x = |
| 681 |
list_entry(p, struct blkid_bufinfo, bufs); |
| 682 |
|
| 683 |
remove_buffer(x); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
/* |
| 688 |
* Zeroize in-memory data in already read buffer. The next blkid_probe_get_buffer() |
| 689 |
* will return modified buffer. This is usable when you want to call the same probing |
| 690 |
* function more than once and hide previously detected magic strings. |
| 691 |
* |
| 692 |
* See blkid_probe_hide_range(). |
| 693 |
*/ |
| 694 |
static int hide_buffer(blkid_probe pr, uint64_t off, uint64_t len) |
| 695 |
{ |
| 696 |
uint64_t real_off = pr->off + off; |
| 697 |
struct list_head *p; |
| 698 |
int ct = 0; |
| 699 |
|
| 700 |
if (UINT64_MAX - len < off) { |
| 701 |
DBG(BUFFER, ul_debug("\t hide-buffer overflow (ignore)")); |
| 702 |
return -EINVAL; |
| 703 |
} |
| 704 |
|
| 705 |
list_for_each(p, &pr->buffers) { |
| 706 |
struct blkid_bufinfo *x = |
| 707 |
list_entry(p, struct blkid_bufinfo, bufs); |
| 708 |
unsigned char *data; |
| 709 |
|
| 710 |
if (real_off >= x->off && real_off + len <= x->off + x->len) { |
| 711 |
|
| 712 |
assert(x->off <= real_off); |
| 713 |
assert(x->off + x->len >= real_off + len); |
| 714 |
|
| 715 |
data = real_off ? x->data + (real_off - x->off) : x->data; |
| 716 |
|
| 717 |
DBG(BUFFER, ul_debug("\thiding: off=%"PRIu64" len=%"PRIu64, |
| 718 |
off, len)); |
| 719 |
mprotect(x->data, x->len, PROT_READ | PROT_WRITE); |
| 720 |
memset(data, 0, len); |
| 721 |
mprotect(x->data, x->len, PROT_READ); |
| 722 |
ct++; |
| 723 |
} |
| 724 |
} |
| 725 |
return ct == 0 ? -EINVAL : 0; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
/* |
| 730 |
* Note that @off is offset within probing area, the probing area is defined by |
| 731 |
* pr->off and pr->size. |
| 732 |
*/ |
| 733 |
const unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len) |
| 734 |
{ |
| 735 |
struct blkid_bufinfo *bf = NULL; |
| 736 |
uint64_t real_off, bias, len_align; |
| 737 |
|
| 738 |
bias = off % pr->io_size; |
| 739 |
off -= bias; |
| 740 |
len += bias; |
| 741 |
|
| 742 |
if (len % pr->io_size) { |
| 743 |
len_align = pr->io_size - (len % pr->io_size); |
| 744 |
|
| 745 |
if (pr->off + off + len + len_align <= pr->size) |
| 746 |
len += len_align; |
| 747 |
} |
| 748 |
|
| 749 |
real_off = pr->off + off; |
| 750 |
|
| 751 |
/* |
| 752 |
DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju", |
| 753 |
off, real_off, pr->off, pr->off + pr->size, len)); |
| 754 |
*/ |
| 755 |
if (pr->size == 0 || pr->io_size == 0) { |
| 756 |
errno = EINVAL; |
| 757 |
return NULL; |
| 758 |
} |
| 759 |
|
| 760 |
if (UINT64_MAX - len < off || UINT64_MAX - len < real_off) { |
| 761 |
DBG(BUFFER, ul_debug("\t read-buffer overflow (ignore)")); |
| 762 |
return NULL; |
| 763 |
} |
| 764 |
|
| 765 |
if (len > 8388608 /* 8 Mib */ ) { |
| 766 |
DBG(BUFFER, ul_debug("\t too large read request (ignore)")); |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
|
| 770 |
if (len == 0 |
| 771 |
|| (!S_ISCHR(pr->mode) && (pr->size < off || pr->size < len)) |
| 772 |
|| (!S_ISCHR(pr->mode) && (pr->off + pr->size < real_off + len))) { |
| 773 |
DBG(BUFFER, ul_debug("\t read-buffer out of probing area (ignore)")); |
| 774 |
errno = 0; |
| 775 |
return NULL; |
| 776 |
} |
| 777 |
|
| 778 |
if (pr->parent && |
| 779 |
pr->parent->devno == pr->devno && |
| 780 |
pr->parent->off <= pr->off && |
| 781 |
pr->parent->off + pr->parent->size >= pr->off + pr->size) { |
| 782 |
/* |
| 783 |
* This is a cloned prober and points to the same area as |
| 784 |
* parent. Let's use parent's buffers. |
| 785 |
* |
| 786 |
* Note that pr->off (and pr->parent->off) is always from the |
| 787 |
* begin of the device. |
| 788 |
*/ |
| 789 |
return blkid_probe_get_buffer(pr->parent, |
| 790 |
pr->off + off + bias - pr->parent->off, len); |
| 791 |
} |
| 792 |
|
| 793 |
/* try buffers we already have in memory or read from device */ |
| 794 |
bf = get_cached_buffer(pr, off, len); |
| 795 |
if (!bf) { |
| 796 |
bf = read_buffer(pr, real_off, len); |
| 797 |
if (!bf) |
| 798 |
return NULL; |
| 799 |
|
| 800 |
mark_prunable_buffers(pr, bf); |
| 801 |
list_add_tail(&bf->bufs, &pr->buffers); |
| 802 |
} |
| 803 |
|
| 804 |
assert(bf->off <= real_off); |
| 805 |
assert(bf->off + bf->len >= real_off + len); |
| 806 |
|
| 807 |
errno = 0; |
| 808 |
return real_off ? bf->data + (real_off - bf->off + bias) : bf->data + bias; |
| 809 |
} |
| 810 |
|
| 811 |
#ifdef O_DIRECT |
| 812 |
/* |
| 813 |
* This is blkid_probe_get_buffer with the read done as an O_DIRECT operation. |
| 814 |
* Note that @off is offset within probing area, the probing area is defined by |
| 815 |
* pr->off and pr->size. |
| 816 |
*/ |
| 817 |
const unsigned char *blkid_probe_get_buffer_direct(blkid_probe pr, uint64_t off, uint64_t len) |
| 818 |
{ |
| 819 |
const unsigned char *ret = NULL; |
| 820 |
int flags, rc, olderrno; |
| 821 |
|
| 822 |
flags = fcntl(pr->fd, F_GETFL); |
| 823 |
rc = fcntl(pr->fd, F_SETFL, flags | O_DIRECT); |
| 824 |
if (rc) { |
| 825 |
DBG(LOWPROBE, ul_debug("fcntl F_SETFL failed to set O_DIRECT")); |
| 826 |
errno = 0; |
| 827 |
return NULL; |
| 828 |
} |
| 829 |
ret = blkid_probe_get_buffer(pr, off, len); |
| 830 |
olderrno = errno; |
| 831 |
rc = fcntl(pr->fd, F_SETFL, flags); |
| 832 |
if (rc) { |
| 833 |
DBG(LOWPROBE, ul_debug("fcntl F_SETFL failed to clear O_DIRECT")); |
| 834 |
errno = olderrno; |
| 835 |
} |
| 836 |
return ret; |
| 837 |
} |
| 838 |
#endif |
| 839 |
|
| 840 |
/** |
| 841 |
* blkid_probe_reset_buffers: |
| 842 |
* @pr: prober |
| 843 |
* |
| 844 |
* libblkid reuse all already read buffers from the device. The buffers may be |
| 845 |
* modified by blkid_probe_hide_range(). This function reset and free all |
| 846 |
* cached buffers. The next blkid_do_probe() will read all data from the |
| 847 |
* device. |
| 848 |
* |
| 849 |
* Since: 2.31 |
| 850 |
* |
| 851 |
* Returns: <0 in case of failure, or 0 on success. |
| 852 |
*/ |
| 853 |
int blkid_probe_reset_buffers(blkid_probe pr) |
| 854 |
{ |
| 855 |
uint64_t ct = 0, len = 0; |
| 856 |
|
| 857 |
pr->flags &= ~BLKID_FL_MODIF_BUFF; |
| 858 |
|
| 859 |
blkid_probe_prune_buffers(pr); |
| 860 |
|
| 861 |
if (list_empty(&pr->buffers)) |
| 862 |
return 0; |
| 863 |
|
| 864 |
DBG(BUFFER, ul_debug("Resetting probing buffers")); |
| 865 |
|
| 866 |
while (!list_empty(&pr->buffers)) { |
| 867 |
struct blkid_bufinfo *bf = list_entry(pr->buffers.next, |
| 868 |
struct blkid_bufinfo, bufs); |
| 869 |
ct++; |
| 870 |
len += bf->len; |
| 871 |
|
| 872 |
remove_buffer(bf); |
| 873 |
} |
| 874 |
|
| 875 |
DBG(LOWPROBE, ul_debug(" buffers summary: %"PRIu64" bytes by %"PRIu64" read() calls", |
| 876 |
len, ct)); |
| 877 |
|
| 878 |
INIT_LIST_HEAD(&pr->buffers); |
| 879 |
|
| 880 |
return 0; |
| 881 |
} |
| 882 |
|
| 883 |
/** |
| 884 |
* blkid_probe_hide_range: |
| 885 |
* @pr: prober |
| 886 |
* @off: start of the range |
| 887 |
* @len: size of the range |
| 888 |
* |
| 889 |
* This function modifies in-memory cached data from the device. The specified |
| 890 |
* range is zeroized. This is usable together with blkid_probe_step_back(). |
| 891 |
* The next blkid_do_probe() will not see specified area. |
| 892 |
* |
| 893 |
* Note that this is usable for already (by library) read data, and this |
| 894 |
* function is not a way how to hide any large areas on your device. |
| 895 |
* |
| 896 |
* The function blkid_probe_reset_buffers() reverts all. |
| 897 |
* |
| 898 |
* Since: 2.31 |
| 899 |
* |
| 900 |
* Returns: <0 in case of failure, or 0 on success. |
| 901 |
*/ |
| 902 |
int blkid_probe_hide_range(blkid_probe pr, uint64_t off, uint64_t len) |
| 903 |
{ |
| 904 |
int rc = hide_buffer(pr, off, len); |
| 905 |
|
| 906 |
if (rc == 0) |
| 907 |
pr->flags |= BLKID_FL_MODIF_BUFF; |
| 908 |
return rc; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
static void blkid_probe_reset_values(blkid_probe pr) |
| 913 |
{ |
| 914 |
if (list_empty(&pr->values)) |
| 915 |
return; |
| 916 |
|
| 917 |
DBG(LOWPROBE, ul_debug("resetting results")); |
| 918 |
|
| 919 |
while (!list_empty(&pr->values)) { |
| 920 |
struct blkid_prval *v = list_entry(pr->values.next, |
| 921 |
struct blkid_prval, prvals); |
| 922 |
blkid_probe_free_value(v); |
| 923 |
} |
| 924 |
|
| 925 |
INIT_LIST_HEAD(&pr->values); |
| 926 |
} |
| 927 |
|
| 928 |
/* |
| 929 |
* Small devices need a special care. |
| 930 |
*/ |
| 931 |
int blkid_probe_is_tiny(blkid_probe pr) |
| 932 |
{ |
| 933 |
return (pr->flags & BLKID_FL_TINY_DEV); |
| 934 |
} |
| 935 |
|
| 936 |
/* |
| 937 |
* CDROMs may fail when probed for RAID (last sector problem) |
| 938 |
*/ |
| 939 |
int blkid_probe_is_cdrom(blkid_probe pr) |
| 940 |
{ |
| 941 |
return (pr->flags & BLKID_FL_CDROM_DEV); |
| 942 |
} |
| 943 |
|
| 944 |
int blkdid_probe_is_opal_locked(blkid_probe pr) |
| 945 |
{ |
| 946 |
if (!(pr->flags & BLKID_FL_OPAL_CHECKED)) { |
| 947 |
pr->flags |= BLKID_FL_OPAL_CHECKED; |
| 948 |
|
| 949 |
#ifdef HAVE_OPAL_GET_STATUS |
| 950 |
ssize_t ret; |
| 951 |
struct opal_status st = { }; |
| 952 |
int errsv = errno; |
| 953 |
|
| 954 |
/* If the device is locked with OPAL, we'll fail to read with I/O |
| 955 |
* errors when probing deep into the block device. */ |
| 956 |
ret = ioctl(pr->fd, IOC_OPAL_GET_STATUS, &st); |
| 957 |
if (ret == 0 && (st.flags & OPAL_FL_LOCKED)) { |
| 958 |
pr->flags |= BLKID_FL_OPAL_LOCKED; |
| 959 |
} |
| 960 |
|
| 961 |
errno = errsv; |
| 962 |
#endif |
| 963 |
} |
| 964 |
|
| 965 |
return (pr->flags & BLKID_FL_OPAL_LOCKED); |
| 966 |
} |
| 967 |
|
| 968 |
#ifdef CDROM_GET_CAPABILITY |
| 969 |
|
| 970 |
static int is_sector_readable(int fd, uint64_t sector) |
| 971 |
{ |
| 972 |
char buf[512]; |
| 973 |
ssize_t sz; |
| 974 |
|
| 975 |
if (lseek(fd, sector * 512, SEEK_SET) == (off_t) -1) |
| 976 |
goto failed; |
| 977 |
|
| 978 |
sz = read(fd, buf, sizeof(buf)); |
| 979 |
if (sz != (ssize_t) sizeof(buf)) |
| 980 |
goto failed; |
| 981 |
|
| 982 |
return 1; |
| 983 |
failed: |
| 984 |
DBG(LOWPROBE, ul_debug("CDROM: read sector %"PRIu64" failed %m", sector)); |
| 985 |
errno = 0; |
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
/* |
| 990 |
* Linux kernel reports (BLKGETSIZE) cdrom device size greater than area |
| 991 |
* readable by read(2). We have to reduce the probing area to avoid unwanted |
| 992 |
* I/O errors in probing functions. It seems that unreadable are always last 2 |
| 993 |
* or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte |
| 994 |
* sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last |
| 995 |
* written block, so we will reduce size based on it too. |
| 996 |
*/ |
| 997 |
static void cdrom_size_correction(blkid_probe pr, uint64_t last_written) |
| 998 |
{ |
| 999 |
uint64_t n, nsectors = pr->size >> 9; |
| 1000 |
|
| 1001 |
if (last_written && nsectors > ((last_written+1) << 2)) |
| 1002 |
nsectors = (last_written+1) << 2; |
| 1003 |
|
| 1004 |
for (n = nsectors - 12; n < nsectors; n++) { |
| 1005 |
if (!is_sector_readable(pr->fd, n)) |
| 1006 |
goto failed; |
| 1007 |
} |
| 1008 |
|
| 1009 |
DBG(LOWPROBE, ul_debug("CDROM: full size available")); |
| 1010 |
return; |
| 1011 |
failed: |
| 1012 |
/* 'n' is the failed sector, reduce device size to n-1; */ |
| 1013 |
DBG(LOWPROBE, ul_debug("CDROM: reduce size from %ju to %ju.", |
| 1014 |
(uintmax_t) pr->size, |
| 1015 |
(uintmax_t) n << 9)); |
| 1016 |
pr->size = n << 9; |
| 1017 |
} |
| 1018 |
|
| 1019 |
#endif |
| 1020 |
|
| 1021 |
static uint64_t blkid_get_io_size(int fd) |
| 1022 |
{ |
| 1023 |
static const int ioctls[] = { |
| 1024 |
#ifdef BLKIOOPT |
| 1025 |
BLKIOOPT, |
| 1026 |
#endif |
| 1027 |
#ifdef BLKIOMIN |
| 1028 |
BLKIOMIN, |
| 1029 |
#endif |
| 1030 |
#ifdef BLKBSZGET |
| 1031 |
BLKBSZGET, |
| 1032 |
#endif |
| 1033 |
}; |
| 1034 |
unsigned int s; |
| 1035 |
size_t i; |
| 1036 |
int r; |
| 1037 |
|
| 1038 |
for (i = 0; i < ARRAY_SIZE(ioctls); i++) { |
| 1039 |
r = ioctl(fd, ioctls[i], &s); |
| 1040 |
if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE) |
| 1041 |
return min(s, 1U << 16); |
| 1042 |
} |
| 1043 |
|
| 1044 |
return DEFAULT_SECTOR_SIZE; |
| 1045 |
} |
| 1046 |
|
| 1047 |
/** |
| 1048 |
* blkid_probe_set_device: |
| 1049 |
* @pr: probe |
| 1050 |
* @fd: device file descriptor |
| 1051 |
* @off: begin of probing area |
| 1052 |
* @size: size of probing area (zero means whole device/file) |
| 1053 |
* |
| 1054 |
* Assigns the device to probe control struct, resets internal buffers, resets |
| 1055 |
* the current probing, and close previously associated device (if open by |
| 1056 |
* libblkid). |
| 1057 |
* |
| 1058 |
* If @fd is < 0 than only resets the prober and returns 1. Note that |
| 1059 |
* blkid_reset_probe() keeps the device associated with the prober, but |
| 1060 |
* blkid_probe_set_device() does complete reset. |
| 1061 |
* |
| 1062 |
* Returns: -1 in case of failure, 0 on success and 1 on reset. |
| 1063 |
*/ |
| 1064 |
int blkid_probe_set_device(blkid_probe pr, int fd, |
| 1065 |
blkid_loff_t off, blkid_loff_t size) |
| 1066 |
{ |
| 1067 |
struct stat sb; |
| 1068 |
uint64_t devsiz = 0; |
| 1069 |
char *dm_uuid = NULL; |
| 1070 |
int is_floppy = 0; |
| 1071 |
|
| 1072 |
blkid_reset_probe(pr); |
| 1073 |
blkid_probe_reset_buffers(pr); |
| 1074 |
|
| 1075 |
if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0) |
| 1076 |
close(pr->fd); |
| 1077 |
|
| 1078 |
if (pr->disk_probe) { |
| 1079 |
blkid_free_probe(pr->disk_probe); |
| 1080 |
pr->disk_probe = NULL; |
| 1081 |
} |
| 1082 |
|
| 1083 |
pr->flags &= ~BLKID_FL_PRIVATE_FD; |
| 1084 |
pr->flags &= ~BLKID_FL_TINY_DEV; |
| 1085 |
pr->flags &= ~BLKID_FL_CDROM_DEV; |
| 1086 |
pr->prob_flags = 0; |
| 1087 |
pr->fd = fd; |
| 1088 |
pr->off = (uint64_t) off; |
| 1089 |
pr->size = 0; |
| 1090 |
pr->io_size = DEFAULT_SECTOR_SIZE; |
| 1091 |
pr->devno = 0; |
| 1092 |
pr->disk_devno = 0; |
| 1093 |
pr->mode = 0; |
| 1094 |
pr->blkssz = 0; |
| 1095 |
pr->wipe_off = 0; |
| 1096 |
pr->wipe_size = 0; |
| 1097 |
pr->wipe_chain = NULL; |
| 1098 |
pr->zone_size = 0; |
| 1099 |
|
| 1100 |
if (fd < 0) |
| 1101 |
return 1; |
| 1102 |
|
| 1103 |
|
| 1104 |
#if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE) |
| 1105 |
/* Disable read-ahead */ |
| 1106 |
posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM); |
| 1107 |
#endif |
| 1108 |
if (fstat(fd, &sb)) |
| 1109 |
goto err; |
| 1110 |
|
| 1111 |
if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode)) { |
| 1112 |
errno = EINVAL; |
| 1113 |
goto err; |
| 1114 |
} |
| 1115 |
|
| 1116 |
pr->mode = sb.st_mode; |
| 1117 |
if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) |
| 1118 |
pr->devno = sb.st_rdev; |
| 1119 |
|
| 1120 |
if (S_ISBLK(sb.st_mode)) { |
| 1121 |
if (blkdev_get_size(fd, (unsigned long long *) &devsiz)) { |
| 1122 |
DBG(LOWPROBE, ul_debug("failed to get device size")); |
| 1123 |
goto err; |
| 1124 |
} |
| 1125 |
} else if (S_ISCHR(sb.st_mode)) { |
| 1126 |
char buf[PATH_MAX]; |
| 1127 |
|
| 1128 |
if (!sysfs_chrdev_devno_to_devname(sb.st_rdev, buf, sizeof(buf)) |
| 1129 |
|| strncmp(buf, "ubi", 3) != 0) { |
| 1130 |
DBG(LOWPROBE, ul_debug("no UBI char device")); |
| 1131 |
errno = EINVAL; |
| 1132 |
goto err; |
| 1133 |
} |
| 1134 |
devsiz = 1; /* UBI devices are char... */ |
| 1135 |
} else if (S_ISREG(sb.st_mode)) |
| 1136 |
devsiz = sb.st_size; /* regular file */ |
| 1137 |
|
| 1138 |
pr->size = size ? (uint64_t)size : devsiz; |
| 1139 |
|
| 1140 |
if (off && size == 0) |
| 1141 |
/* only offset without size specified */ |
| 1142 |
pr->size -= (uint64_t) off; |
| 1143 |
|
| 1144 |
if (pr->off + pr->size > devsiz) { |
| 1145 |
DBG(LOWPROBE, ul_debug("area specified by offset and size is bigger than device")); |
| 1146 |
errno = EINVAL; |
| 1147 |
goto err; |
| 1148 |
} |
| 1149 |
|
| 1150 |
if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode)) |
| 1151 |
pr->flags |= BLKID_FL_TINY_DEV; |
| 1152 |
|
| 1153 |
#ifdef FDGETFDCSTAT |
| 1154 |
if (S_ISBLK(sb.st_mode)) { |
| 1155 |
/* |
| 1156 |
* Re-open without O_NONBLOCK for floppy device. |
| 1157 |
* |
| 1158 |
* Since kernel commit c7e9d0020361f4308a70cdfd6d5335e273eb8717 |
| 1159 |
* floppy drive works bad when opened with O_NONBLOCK. |
| 1160 |
*/ |
| 1161 |
struct floppy_fdc_state flst; |
| 1162 |
|
| 1163 |
if (ioctl(fd, FDGETFDCSTAT, &flst) >= 0) { |
| 1164 |
int flags = fcntl(fd, F_GETFL, 0); |
| 1165 |
|
| 1166 |
if (flags < 0) |
| 1167 |
goto err; |
| 1168 |
if (flags & O_NONBLOCK) { |
| 1169 |
flags &= ~O_NONBLOCK; |
| 1170 |
|
| 1171 |
fd = ul_reopen(fd, flags | O_CLOEXEC); |
| 1172 |
if (fd < 0) |
| 1173 |
goto err; |
| 1174 |
|
| 1175 |
pr->flags |= BLKID_FL_PRIVATE_FD; |
| 1176 |
pr->fd = fd; |
| 1177 |
} |
| 1178 |
is_floppy = 1; |
| 1179 |
} |
| 1180 |
errno = 0; |
| 1181 |
} |
| 1182 |
#endif |
| 1183 |
if (S_ISBLK(sb.st_mode) && |
| 1184 |
!is_floppy && |
| 1185 |
sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) { |
| 1186 |
DBG(LOWPROBE, ul_debug("ignore private device mapper device")); |
| 1187 |
pr->flags |= BLKID_FL_NOSCAN_DEV; |
| 1188 |
} |
| 1189 |
|
| 1190 |
#ifdef CDROM_GET_CAPABILITY |
| 1191 |
else if (S_ISBLK(sb.st_mode) && |
| 1192 |
!blkid_probe_is_tiny(pr) && |
| 1193 |
!dm_uuid && |
| 1194 |
!is_floppy && |
| 1195 |
blkid_probe_is_wholedisk(pr)) { |
| 1196 |
|
| 1197 |
long last_written = 0; |
| 1198 |
|
| 1199 |
/* |
| 1200 |
* pktcdvd.ko accepts only these ioctls: |
| 1201 |
* CDROMEJECT CDROMMULTISESSION CDROMREADTOCENTRY |
| 1202 |
* CDROM_LAST_WRITTEN CDROM_SEND_PACKET SCSI_IOCTL_SEND_COMMAND |
| 1203 |
* So CDROM_GET_CAPABILITY cannot be used for detecting pktcdvd |
| 1204 |
* devices. But CDROM_GET_CAPABILITY and CDROM_DRIVE_STATUS are |
| 1205 |
* fast so use them for detecting if medium is present. In any |
| 1206 |
* case use last written block form CDROM_LAST_WRITTEN. |
| 1207 |
*/ |
| 1208 |
|
| 1209 |
if (ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0) { |
| 1210 |
# ifdef CDROM_DRIVE_STATUS |
| 1211 |
switch (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) { |
| 1212 |
case CDS_TRAY_OPEN: |
| 1213 |
case CDS_NO_DISC: |
| 1214 |
errno = ENOMEDIUM; |
| 1215 |
goto err; |
| 1216 |
} |
| 1217 |
# endif |
| 1218 |
pr->flags |= BLKID_FL_CDROM_DEV; |
| 1219 |
} |
| 1220 |
|
| 1221 |
# ifdef CDROM_LAST_WRITTEN |
| 1222 |
if (ioctl(fd, CDROM_LAST_WRITTEN, &last_written) == 0) { |
| 1223 |
pr->flags |= BLKID_FL_CDROM_DEV; |
| 1224 |
} else { |
| 1225 |
if (errno == ENOMEDIUM) |
| 1226 |
goto err; |
| 1227 |
} |
| 1228 |
# endif |
| 1229 |
|
| 1230 |
if (pr->flags & BLKID_FL_CDROM_DEV) { |
| 1231 |
cdrom_size_correction(pr, last_written); |
| 1232 |
|
| 1233 |
# ifdef CDROMMULTISESSION |
| 1234 |
if (!pr->off && blkid_probe_get_hint(pr, "session_offset", NULL) < 0) { |
| 1235 |
struct cdrom_multisession multisession = { .addr_format = CDROM_LBA }; |
| 1236 |
if (ioctl(fd, CDROMMULTISESSION, &multisession) == 0 && multisession.xa_flag) |
| 1237 |
blkid_probe_set_hint(pr, "session_offset", (multisession.addr.lba << 11)); |
| 1238 |
} |
| 1239 |
# endif |
| 1240 |
} |
| 1241 |
} |
| 1242 |
#endif |
| 1243 |
free(dm_uuid); |
| 1244 |
|
| 1245 |
# ifdef BLKGETZONESZ |
| 1246 |
if (S_ISBLK(sb.st_mode) && !is_floppy) { |
| 1247 |
uint32_t zone_size_sector; |
| 1248 |
|
| 1249 |
if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector)) |
| 1250 |
pr->zone_size = zone_size_sector << 9; |
| 1251 |
} |
| 1252 |
# endif |
| 1253 |
|
| 1254 |
if (S_ISBLK(sb.st_mode) && !is_floppy && !blkid_probe_is_tiny(pr)) |
| 1255 |
pr->io_size = blkid_get_io_size(fd); |
| 1256 |
|
| 1257 |
DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64", zonesize=%"PRIu64", iosize=%"PRIu64, |
| 1258 |
pr->off, pr->size, pr->zone_size, pr->io_size)); |
| 1259 |
DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s", |
| 1260 |
blkid_probe_is_wholedisk(pr) ?"YES" : "NO", |
| 1261 |
S_ISREG(pr->mode) ? "YES" : "NO")); |
| 1262 |
|
| 1263 |
return 0; |
| 1264 |
err: |
| 1265 |
DBG(LOWPROBE, ul_debug("failed to prepare a device for low-probing")); |
| 1266 |
return -1; |
| 1267 |
|
| 1268 |
} |
| 1269 |
|
| 1270 |
int blkid_probe_get_dimension(blkid_probe pr, uint64_t *off, uint64_t *size) |
| 1271 |
{ |
| 1272 |
*off = pr->off; |
| 1273 |
*size = pr->size; |
| 1274 |
return 0; |
| 1275 |
} |
| 1276 |
|
| 1277 |
int blkid_probe_set_dimension(blkid_probe pr, uint64_t off, uint64_t size) |
| 1278 |
{ |
| 1279 |
DBG(LOWPROBE, ul_debug( |
| 1280 |
"changing probing area: size=%"PRIu64", off=%"PRIu64" " |
| 1281 |
"-to-> size=%"PRIu64", off=%"PRIu64"", |
| 1282 |
pr->size, pr->off, size, off)); |
| 1283 |
|
| 1284 |
pr->off = off; |
| 1285 |
pr->size = size; |
| 1286 |
pr->flags &= ~BLKID_FL_TINY_DEV; |
| 1287 |
|
| 1288 |
if (pr->size <= 1440ULL * 1024ULL && !S_ISCHR(pr->mode)) |
| 1289 |
pr->flags |= BLKID_FL_TINY_DEV; |
| 1290 |
|
| 1291 |
blkid_probe_reset_buffers(pr); |
| 1292 |
|
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
const unsigned char *blkid_probe_get_sb_buffer(blkid_probe pr, const struct blkid_idmag *mag, size_t size) |
| 1297 |
{ |
| 1298 |
uint64_t hint_offset, off; |
| 1299 |
|
| 1300 |
if (mag->kboff >= 0) { |
| 1301 |
if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0) |
| 1302 |
hint_offset = 0; |
| 1303 |
|
| 1304 |
off = hint_offset + (mag->kboff << 10); |
| 1305 |
} else { |
| 1306 |
off = pr->size - (-mag->kboff << 10); |
| 1307 |
} |
| 1308 |
|
| 1309 |
return blkid_probe_get_buffer(pr, off, size); |
| 1310 |
} |
| 1311 |
|
| 1312 |
uint64_t blkid_probe_get_idmag_off(blkid_probe pr, const struct blkid_idmag *mag) |
| 1313 |
{ |
| 1314 |
if (mag->kboff >= 0) |
| 1315 |
return mag->kboff << 10; |
| 1316 |
else |
| 1317 |
return pr->size - (-mag->kboff << 10); |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* |
| 1321 |
* Check for matching magic value. |
| 1322 |
* Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found |
| 1323 |
* or no magic present, or negative value on error. |
| 1324 |
*/ |
| 1325 |
int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id, |
| 1326 |
uint64_t *offset, const struct blkid_idmag **res) |
| 1327 |
{ |
| 1328 |
const struct blkid_idmag *mag = NULL; |
| 1329 |
uint64_t off = 0; |
| 1330 |
|
| 1331 |
if (id) |
| 1332 |
mag = &id->magics[0]; |
| 1333 |
if (res) |
| 1334 |
*res = NULL; |
| 1335 |
|
| 1336 |
/* try to detect by magic string */ |
| 1337 |
while(mag && mag->magic) { |
| 1338 |
const unsigned char *buf; |
| 1339 |
long kboff; |
| 1340 |
uint64_t hint_offset; |
| 1341 |
|
| 1342 |
if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0) |
| 1343 |
hint_offset = 0; |
| 1344 |
|
| 1345 |
/* If the magic is for zoned device, skip non-zoned device */ |
| 1346 |
if (mag->is_zoned && !pr->zone_size) { |
| 1347 |
mag++; |
| 1348 |
continue; |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (!mag->is_zoned) |
| 1352 |
kboff = mag->kboff; |
| 1353 |
else |
| 1354 |
kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone; |
| 1355 |
|
| 1356 |
if (kboff >= 0) |
| 1357 |
off = hint_offset + (kboff << 10) + mag->sboff; |
| 1358 |
else |
| 1359 |
off = pr->size - (-kboff << 10) + mag->sboff; |
| 1360 |
buf = blkid_probe_get_buffer(pr, off, mag->len); |
| 1361 |
|
| 1362 |
if (!buf && errno) |
| 1363 |
return -errno; |
| 1364 |
|
| 1365 |
if (buf && !memcmp(mag->magic, buf, mag->len)) { |
| 1366 |
DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld", |
| 1367 |
mag->sboff, kboff)); |
| 1368 |
if (offset) |
| 1369 |
*offset = off; |
| 1370 |
if (res) |
| 1371 |
*res = mag; |
| 1372 |
return BLKID_PROBE_OK; |
| 1373 |
} |
| 1374 |
mag++; |
| 1375 |
} |
| 1376 |
|
| 1377 |
if (id && id->magics[0].magic) |
| 1378 |
/* magic string(s) defined, but not found */ |
| 1379 |
return BLKID_PROBE_NONE; |
| 1380 |
|
| 1381 |
return BLKID_PROBE_OK; |
| 1382 |
} |
| 1383 |
|
| 1384 |
static inline void blkid_probe_start(blkid_probe pr) |
| 1385 |
{ |
| 1386 |
DBG(LOWPROBE, ul_debug("start probe")); |
| 1387 |
pr->cur_chain = NULL; |
| 1388 |
pr->prob_flags = 0; |
| 1389 |
blkid_probe_set_wiper(pr, 0, 0); |
| 1390 |
} |
| 1391 |
|
| 1392 |
static inline void blkid_probe_end(blkid_probe pr) |
| 1393 |
{ |
| 1394 |
DBG(LOWPROBE, ul_debug("end probe")); |
| 1395 |
pr->cur_chain = NULL; |
| 1396 |
pr->prob_flags = 0; |
| 1397 |
blkid_probe_set_wiper(pr, 0, 0); |
| 1398 |
} |
| 1399 |
|
| 1400 |
/** |
| 1401 |
* blkid_do_probe: |
| 1402 |
* @pr: prober |
| 1403 |
* |
| 1404 |
* Calls probing functions in all enabled chains. The superblocks chain is |
| 1405 |
* enabled by default. The blkid_do_probe() stores result from only one |
| 1406 |
* probing function. It's necessary to call this routine in a loop to get |
| 1407 |
* results from all probing functions in all chains. The probing is reset |
| 1408 |
* by blkid_reset_probe() or by filter functions. |
| 1409 |
* |
| 1410 |
* This is string-based NAME=value interface only. |
| 1411 |
* |
| 1412 |
* <example> |
| 1413 |
* <title>basic case - use the first result only</title> |
| 1414 |
* <programlisting> |
| 1415 |
* if (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
| 1416 |
* int nvals = blkid_probe_numof_values(pr); |
| 1417 |
* for (n = 0; n < nvals; n++) { |
| 1418 |
* if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0) |
| 1419 |
* printf("%s = %s\n", name, data); |
| 1420 |
* } |
| 1421 |
* } |
| 1422 |
* </programlisting> |
| 1423 |
* </example> |
| 1424 |
* |
| 1425 |
* <example> |
| 1426 |
* <title>advanced case - probe for all signatures</title> |
| 1427 |
* <programlisting> |
| 1428 |
* while (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
| 1429 |
* int nvals = blkid_probe_numof_values(pr); |
| 1430 |
* ... |
| 1431 |
* } |
| 1432 |
* </programlisting> |
| 1433 |
* </example> |
| 1434 |
* |
| 1435 |
* See also blkid_reset_probe(). |
| 1436 |
* |
| 1437 |
* Returns: 0 on success, 1 when probing is done and -1 in case of error. |
| 1438 |
*/ |
| 1439 |
int blkid_do_probe(blkid_probe pr) |
| 1440 |
{ |
| 1441 |
int rc = 1; |
| 1442 |
|
| 1443 |
if (pr->flags & BLKID_FL_NOSCAN_DEV) |
| 1444 |
return BLKID_PROBE_NONE; |
| 1445 |
|
| 1446 |
do { |
| 1447 |
struct blkid_chain *chn = pr->cur_chain; |
| 1448 |
|
| 1449 |
if (!chn) { |
| 1450 |
blkid_probe_start(pr); |
| 1451 |
chn = pr->cur_chain = &pr->chains[0]; |
| 1452 |
} |
| 1453 |
/* we go to the next chain only when the previous probing |
| 1454 |
* result was nothing (rc == 1) and when the current chain is |
| 1455 |
* disabled or we are at end of the current chain (chain->idx + |
| 1456 |
* 1 == sizeof chain) or the current chain bailed out right at |
| 1457 |
* the start (chain->idx == -1) |
| 1458 |
*/ |
| 1459 |
else if (rc == 1 && (chn->enabled == FALSE || |
| 1460 |
chn->idx + 1 == (int) chn->driver->nidinfos || |
| 1461 |
chn->idx == -1)) { |
| 1462 |
|
| 1463 |
size_t idx = chn->driver->id + 1; |
| 1464 |
|
| 1465 |
if (idx < BLKID_NCHAINS) |
| 1466 |
chn = pr->cur_chain = &pr->chains[idx]; |
| 1467 |
else { |
| 1468 |
blkid_probe_end(pr); |
| 1469 |
return BLKID_PROBE_NONE; /* all chains already probed */ |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
chn->binary = FALSE; /* for sure... */ |
| 1474 |
|
| 1475 |
DBG(LOWPROBE, ul_debug("chain probe %s %s (idx=%d)", |
| 1476 |
chn->driver->name, |
| 1477 |
chn->enabled? "ENABLED" : "DISABLED", |
| 1478 |
chn->idx)); |
| 1479 |
|
| 1480 |
if (!chn->enabled) |
| 1481 |
continue; |
| 1482 |
|
| 1483 |
/* rc: -1 = error, 0 = success, 1 = no result */ |
| 1484 |
rc = chn->driver->probe(pr, chn); |
| 1485 |
|
| 1486 |
} while (rc == BLKID_PROBE_NONE); |
| 1487 |
|
| 1488 |
if (rc < 0) |
| 1489 |
return BLKID_PROBE_ERROR; |
| 1490 |
|
| 1491 |
return rc; |
| 1492 |
} |
| 1493 |
|
| 1494 |
#ifdef HAVE_LINUX_BLKZONED_H |
| 1495 |
static int is_conventional(blkid_probe pr, uint64_t offset) |
| 1496 |
{ |
| 1497 |
struct blk_zone_report *rep = NULL; |
| 1498 |
int ret; |
| 1499 |
uint64_t zone_mask; |
| 1500 |
|
| 1501 |
if (!pr->zone_size) |
| 1502 |
return 1; |
| 1503 |
|
| 1504 |
zone_mask = ~(pr->zone_size - 1); |
| 1505 |
rep = blkdev_get_zonereport(blkid_probe_get_fd(pr), |
| 1506 |
(offset & zone_mask) >> 9, 1); |
| 1507 |
if (!rep) |
| 1508 |
return -1; |
| 1509 |
|
| 1510 |
if (rep->zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 1511 |
ret = 1; |
| 1512 |
else |
| 1513 |
ret = 0; |
| 1514 |
|
| 1515 |
free(rep); |
| 1516 |
|
| 1517 |
return ret; |
| 1518 |
} |
| 1519 |
#else |
| 1520 |
static inline int is_conventional(blkid_probe pr __attribute__((__unused__)), |
| 1521 |
uint64_t offset __attribute__((__unused__))) |
| 1522 |
{ |
| 1523 |
return 1; |
| 1524 |
} |
| 1525 |
#endif |
| 1526 |
|
| 1527 |
/** |
| 1528 |
* blkid_do_wipe: |
| 1529 |
* @pr: prober |
| 1530 |
* @dryrun: if TRUE then don't touch the device. |
| 1531 |
* |
| 1532 |
* This function erases the current signature detected by @pr. The @pr has to |
| 1533 |
* be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags |
| 1534 |
* has to be enabled (if you want to erase also superblock with broken check |
| 1535 |
* sums then use BLKID_SUBLKS_BADCSUM too). |
| 1536 |
* |
| 1537 |
* After successful signature removing the @pr prober will be moved one step |
| 1538 |
* back and the next blkid_do_probe() call will again call previously called |
| 1539 |
* probing function. All in-memory cached data from the device are always |
| 1540 |
* reset. |
| 1541 |
* |
| 1542 |
* <example> |
| 1543 |
* <title>wipe all filesystems or raids from the device</title> |
| 1544 |
* <programlisting> |
| 1545 |
* fd = open(devname, O_RDWR|O_CLOEXEC); |
| 1546 |
* blkid_probe_set_device(pr, fd, 0, 0); |
| 1547 |
* |
| 1548 |
* blkid_probe_enable_superblocks(pr, 1); |
| 1549 |
* blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC); |
| 1550 |
* |
| 1551 |
* while (blkid_do_probe(pr) == 0) |
| 1552 |
* blkid_do_wipe(pr, FALSE); |
| 1553 |
* </programlisting> |
| 1554 |
* </example> |
| 1555 |
* |
| 1556 |
* See also blkid_probe_step_back() if you cannot use this built-in wipe |
| 1557 |
* function, but you want to use libblkid probing as a source for wiping. |
| 1558 |
* |
| 1559 |
* See also blkid_wipe_all() which works the same as the example above. |
| 1560 |
* |
| 1561 |
* Returns: 0 on success, and -1 in case of error. |
| 1562 |
*/ |
| 1563 |
int blkid_do_wipe(blkid_probe pr, int dryrun) |
| 1564 |
{ |
| 1565 |
const char *off = NULL; |
| 1566 |
size_t len = 0; |
| 1567 |
uint64_t offset, magoff; |
| 1568 |
int conventional; |
| 1569 |
char buf[BUFSIZ]; |
| 1570 |
int fd, rc = 0; |
| 1571 |
struct blkid_chain *chn; |
| 1572 |
|
| 1573 |
chn = pr->cur_chain; |
| 1574 |
if (!chn) |
| 1575 |
return BLKID_PROBE_ERROR; |
| 1576 |
|
| 1577 |
switch (chn->driver->id) { |
| 1578 |
case BLKID_CHAIN_SUBLKS: |
| 1579 |
rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL); |
| 1580 |
if (!rc) |
| 1581 |
rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len); |
| 1582 |
break; |
| 1583 |
case BLKID_CHAIN_PARTS: |
| 1584 |
rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL); |
| 1585 |
if (!rc) |
| 1586 |
rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len); |
| 1587 |
break; |
| 1588 |
default: |
| 1589 |
return BLKID_PROBE_OK; |
| 1590 |
} |
| 1591 |
|
| 1592 |
if (rc || len == 0 || off == NULL) |
| 1593 |
return BLKID_PROBE_OK; |
| 1594 |
|
| 1595 |
errno = 0; |
| 1596 |
magoff = strtoumax(off, NULL, 10); |
| 1597 |
if (errno) |
| 1598 |
return BLKID_PROBE_OK; |
| 1599 |
|
| 1600 |
offset = magoff + pr->off; |
| 1601 |
fd = blkid_probe_get_fd(pr); |
| 1602 |
if (fd < 0) |
| 1603 |
return BLKID_PROBE_ERROR; |
| 1604 |
|
| 1605 |
if (len > sizeof(buf)) |
| 1606 |
len = sizeof(buf); |
| 1607 |
|
| 1608 |
rc = is_conventional(pr, offset); |
| 1609 |
if (rc < 0) |
| 1610 |
return BLKID_PROBE_ERROR; |
| 1611 |
conventional = rc == 1; |
| 1612 |
|
| 1613 |
DBG(LOWPROBE, ul_debug( |
| 1614 |
"do_wipe [offset=0x%"PRIx64" (%"PRIu64"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n", |
| 1615 |
offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not")); |
| 1616 |
|
| 1617 |
if (lseek(fd, offset, SEEK_SET) == (off_t) -1) |
| 1618 |
return BLKID_PROBE_ERROR; |
| 1619 |
|
| 1620 |
if (!dryrun && len) { |
| 1621 |
if (conventional) { |
| 1622 |
memset(buf, 0, len); |
| 1623 |
|
| 1624 |
/* wipen on device */ |
| 1625 |
if (write_all(fd, buf, len)) |
| 1626 |
return BLKID_PROBE_ERROR; |
| 1627 |
if (fsync(fd) != 0) |
| 1628 |
return BLKID_PROBE_ERROR; |
| 1629 |
} else { |
| 1630 |
#ifdef HAVE_LINUX_BLKZONED_H |
| 1631 |
uint64_t zone_mask = ~(pr->zone_size - 1); |
| 1632 |
struct blk_zone_range range = { |
| 1633 |
.sector = (offset & zone_mask) >> 9, |
| 1634 |
.nr_sectors = pr->zone_size >> 9, |
| 1635 |
}; |
| 1636 |
|
| 1637 |
rc = ioctl(fd, BLKRESETZONE, &range); |
| 1638 |
if (rc < 0) |
| 1639 |
return BLKID_PROBE_ERROR; |
| 1640 |
#else |
| 1641 |
/* Should not reach here */ |
| 1642 |
assert(0); |
| 1643 |
#endif |
| 1644 |
} |
| 1645 |
|
| 1646 |
pr->flags &= ~BLKID_FL_MODIF_BUFF; /* be paranoid */ |
| 1647 |
|
| 1648 |
return blkid_probe_step_back(pr); |
| 1649 |
|
| 1650 |
} |
| 1651 |
|
| 1652 |
if (dryrun) { |
| 1653 |
/* wipe in memory only */ |
| 1654 |
blkid_probe_hide_range(pr, magoff, len); |
| 1655 |
return blkid_probe_step_back(pr); |
| 1656 |
} |
| 1657 |
|
| 1658 |
return BLKID_PROBE_OK; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/** |
| 1662 |
* blkid_wipe_all: |
| 1663 |
* @pr: prober |
| 1664 |
* |
| 1665 |
* This function erases all detectable signatures from &pr. |
| 1666 |
* The @pr has to be open in O_RDWR mode. All other necessary configurations |
| 1667 |
* will be enabled automatically. |
| 1668 |
* |
| 1669 |
* <example> |
| 1670 |
* <title>wipe all filesystems or raids from the device</title> |
| 1671 |
* <programlisting> |
| 1672 |
* fd = open(devname, O_RDWR|O_CLOEXEC); |
| 1673 |
* blkid_probe_set_device(pr, fd, 0, 0); |
| 1674 |
* |
| 1675 |
* blkid_wipe_all(pr); |
| 1676 |
* </programlisting> |
| 1677 |
* </example> |
| 1678 |
* |
| 1679 |
* Returns: 0 on success, and -1 in case of error. |
| 1680 |
*/ |
| 1681 |
int blkid_wipe_all(blkid_probe pr) |
| 1682 |
{ |
| 1683 |
DBG(LOWPROBE, ul_debug("wiping all signatures")); |
| 1684 |
|
| 1685 |
blkid_probe_enable_superblocks(pr, 1); |
| 1686 |
blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | |
| 1687 |
BLKID_SUBLKS_BADCSUM); |
| 1688 |
|
| 1689 |
blkid_probe_enable_partitions(pr, 1); |
| 1690 |
blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC | |
| 1691 |
BLKID_PARTS_FORCE_GPT); |
| 1692 |
|
| 1693 |
while (blkid_do_probe(pr) == 0) { |
| 1694 |
DBG(LOWPROBE, ul_debug("wiping one signature")); |
| 1695 |
blkid_do_wipe(pr, 0); |
| 1696 |
} |
| 1697 |
|
| 1698 |
return BLKID_PROBE_OK; |
| 1699 |
} |
| 1700 |
|
| 1701 |
/** |
| 1702 |
* blkid_probe_step_back: |
| 1703 |
* @pr: prober |
| 1704 |
* |
| 1705 |
* This function move pointer to the probing chain one step back -- it means |
| 1706 |
* that the previously used probing function will be called again in the next |
| 1707 |
* blkid_do_probe() call. |
| 1708 |
* |
| 1709 |
* This is necessary for example if you erase or modify on-disk superblock |
| 1710 |
* according to the current libblkid probing result. |
| 1711 |
* |
| 1712 |
* Note that blkid_probe_hide_range() changes semantic of this function and |
| 1713 |
* cached buffers are not reset, but library uses in-memory modified |
| 1714 |
* buffers to call the next probing function. |
| 1715 |
* |
| 1716 |
* <example> |
| 1717 |
* <title>wipe all superblock, but use libblkid only for probing</title> |
| 1718 |
* <programlisting> |
| 1719 |
* pr = blkid_new_probe_from_filename(devname); |
| 1720 |
* |
| 1721 |
* blkid_probe_enable_superblocks(pr, 1); |
| 1722 |
* blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC); |
| 1723 |
* |
| 1724 |
* blkid_probe_enable_partitions(pr, 1); |
| 1725 |
* blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC); |
| 1726 |
* |
| 1727 |
* while (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
| 1728 |
* const char *ostr = NULL; |
| 1729 |
* size_t len = 0; |
| 1730 |
* |
| 1731 |
* // superblocks |
| 1732 |
* if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0) |
| 1733 |
* blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len); |
| 1734 |
* |
| 1735 |
* // partition tables |
| 1736 |
* if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0) |
| 1737 |
* blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len); |
| 1738 |
* |
| 1739 |
* if (!len || !str) |
| 1740 |
* continue; |
| 1741 |
* |
| 1742 |
* // convert ostr to the real offset by off = strtoll(ostr, NULL, 10); |
| 1743 |
* // use your stuff to erase @len bytes at the @off |
| 1744 |
* .... |
| 1745 |
* |
| 1746 |
* // retry the last probing to check for backup superblocks ..etc. |
| 1747 |
* blkid_probe_step_back(pr); |
| 1748 |
* } |
| 1749 |
* </programlisting> |
| 1750 |
* </example> |
| 1751 |
* |
| 1752 |
* Returns: 0 on success, and -1 in case of error. |
| 1753 |
*/ |
| 1754 |
int blkid_probe_step_back(blkid_probe pr) |
| 1755 |
{ |
| 1756 |
struct blkid_chain *chn; |
| 1757 |
|
| 1758 |
chn = pr->cur_chain; |
| 1759 |
if (!chn) |
| 1760 |
return -1; |
| 1761 |
|
| 1762 |
if (!(pr->flags & BLKID_FL_MODIF_BUFF)) |
| 1763 |
blkid_probe_reset_buffers(pr); |
| 1764 |
|
| 1765 |
if (chn->idx >= 0) { |
| 1766 |
chn->idx--; |
| 1767 |
DBG(LOWPROBE, ul_debug("step back: moving %s chain index to %d", |
| 1768 |
chn->driver->name, |
| 1769 |
chn->idx)); |
| 1770 |
} |
| 1771 |
|
| 1772 |
if (chn->idx == -1) { |
| 1773 |
/* blkid_do_probe() goes to the next chain if the index |
| 1774 |
* of the current chain is -1, so we have to set the |
| 1775 |
* chain pointer to the previous chain. |
| 1776 |
*/ |
| 1777 |
size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0; |
| 1778 |
|
| 1779 |
DBG(LOWPROBE, ul_debug("step back: moving to previous chain")); |
| 1780 |
|
| 1781 |
if (idx > 0) |
| 1782 |
pr->cur_chain = &pr->chains[idx]; |
| 1783 |
else if (idx == 0) |
| 1784 |
pr->cur_chain = NULL; |
| 1785 |
} |
| 1786 |
|
| 1787 |
return 0; |
| 1788 |
} |
| 1789 |
|
| 1790 |
/** |
| 1791 |
* blkid_do_safeprobe: |
| 1792 |
* @pr: prober |
| 1793 |
* |
| 1794 |
* This function gathers probing results from all enabled chains and checks |
| 1795 |
* for ambivalent results (e.g. more filesystems on the device). |
| 1796 |
* |
| 1797 |
* This is string-based NAME=value interface only. |
| 1798 |
* |
| 1799 |
* Note about superblocks chain -- the function does not check for filesystems |
| 1800 |
* when a RAID signature is detected. The function also does not check for |
| 1801 |
* collision between RAIDs. The first detected RAID is returned. The function |
| 1802 |
* checks for collision between partition table and RAID signature -- it's |
| 1803 |
* recommended to enable partitions chain together with superblocks chain. |
| 1804 |
* |
| 1805 |
* Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is |
| 1806 |
* detected and -1 on case of error. |
| 1807 |
*/ |
| 1808 |
int blkid_do_safeprobe(blkid_probe pr) |
| 1809 |
{ |
| 1810 |
int i, count = 0, rc = 0; |
| 1811 |
|
| 1812 |
if (pr->flags & BLKID_FL_NOSCAN_DEV) |
| 1813 |
return BLKID_PROBE_NONE; |
| 1814 |
|
| 1815 |
blkid_probe_start(pr); |
| 1816 |
pr->prob_flags |= BLKID_PROBE_FL_SAFEPROBE; |
| 1817 |
|
| 1818 |
for (i = 0; i < BLKID_NCHAINS; i++) { |
| 1819 |
struct blkid_chain *chn; |
| 1820 |
|
| 1821 |
chn = pr->cur_chain = &pr->chains[i]; |
| 1822 |
chn->binary = FALSE; /* for sure... */ |
| 1823 |
|
| 1824 |
DBG(LOWPROBE, ul_debug("chain safeprobe %s %s", |
| 1825 |
chn->driver->name, |
| 1826 |
chn->enabled? "ENABLED" : "DISABLED")); |
| 1827 |
|
| 1828 |
if (!chn->enabled) |
| 1829 |
continue; |
| 1830 |
|
| 1831 |
blkid_probe_chain_reset_position(chn); |
| 1832 |
|
| 1833 |
rc = chn->driver->safeprobe(pr, chn); |
| 1834 |
|
| 1835 |
blkid_probe_chain_reset_position(chn); |
| 1836 |
|
| 1837 |
/* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */ |
| 1838 |
if (rc < 0) |
| 1839 |
goto done; /* error */ |
| 1840 |
if (rc == 0) |
| 1841 |
count++; /* success */ |
| 1842 |
} |
| 1843 |
|
| 1844 |
done: |
| 1845 |
blkid_probe_end(pr); |
| 1846 |
if (rc < 0) |
| 1847 |
return BLKID_PROBE_ERROR; |
| 1848 |
|
| 1849 |
return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/** |
| 1853 |
* blkid_do_fullprobe: |
| 1854 |
* @pr: prober |
| 1855 |
* |
| 1856 |
* This function gathers probing results from all enabled chains. Same as |
| 1857 |
* blkid_do_safeprobe() but does not check for collision between probing |
| 1858 |
* result. |
| 1859 |
* |
| 1860 |
* This is string-based NAME=value interface only. |
| 1861 |
* |
| 1862 |
* Returns: 0 on success, 1 if nothing is detected or -1 on case of error. |
| 1863 |
*/ |
| 1864 |
int blkid_do_fullprobe(blkid_probe pr) |
| 1865 |
{ |
| 1866 |
int i, count = 0, rc = 0; |
| 1867 |
|
| 1868 |
if (pr->flags & BLKID_FL_NOSCAN_DEV) |
| 1869 |
return BLKID_PROBE_NONE; |
| 1870 |
|
| 1871 |
blkid_probe_start(pr); |
| 1872 |
|
| 1873 |
for (i = 0; i < BLKID_NCHAINS; i++) { |
| 1874 |
struct blkid_chain *chn; |
| 1875 |
|
| 1876 |
chn = pr->cur_chain = &pr->chains[i]; |
| 1877 |
chn->binary = FALSE; /* for sure... */ |
| 1878 |
|
| 1879 |
DBG(LOWPROBE, ul_debug("chain fullprobe %s: %s", |
| 1880 |
chn->driver->name, |
| 1881 |
chn->enabled? "ENABLED" : "DISABLED")); |
| 1882 |
|
| 1883 |
if (!chn->enabled) |
| 1884 |
continue; |
| 1885 |
|
| 1886 |
blkid_probe_chain_reset_position(chn); |
| 1887 |
|
| 1888 |
rc = chn->driver->probe(pr, chn); |
| 1889 |
|
| 1890 |
blkid_probe_chain_reset_position(chn); |
| 1891 |
|
| 1892 |
/* rc: -1 = error, 0 = success, 1 = no result */ |
| 1893 |
if (rc < 0) |
| 1894 |
goto done; /* error */ |
| 1895 |
if (rc == 0) |
| 1896 |
count++; /* success */ |
| 1897 |
} |
| 1898 |
|
| 1899 |
done: |
| 1900 |
blkid_probe_end(pr); |
| 1901 |
if (rc < 0) |
| 1902 |
return BLKID_PROBE_ERROR; |
| 1903 |
|
| 1904 |
return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK; |
| 1905 |
} |
| 1906 |
|
| 1907 |
/* same sa blkid_probe_get_buffer() but works with 512-sectors */ |
| 1908 |
const unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector) |
| 1909 |
{ |
| 1910 |
return blkid_probe_get_buffer(pr, ((uint64_t) sector) << 9, 0x200); |
| 1911 |
} |
| 1912 |
|
| 1913 |
struct blkid_prval *blkid_probe_assign_value(blkid_probe pr, const char *name) |
| 1914 |
{ |
| 1915 |
struct blkid_prval *v; |
| 1916 |
|
| 1917 |
v = calloc(1, sizeof(struct blkid_prval)); |
| 1918 |
if (!v) |
| 1919 |
return NULL; |
| 1920 |
|
| 1921 |
INIT_LIST_HEAD(&v->prvals); |
| 1922 |
v->name = name; |
| 1923 |
v->chain = pr->cur_chain; |
| 1924 |
list_add_tail(&v->prvals, &pr->values); |
| 1925 |
|
| 1926 |
DBG(LOWPROBE, ul_debug("assigning %s [%s]", name, v->chain->driver->name)); |
| 1927 |
return v; |
| 1928 |
} |
| 1929 |
|
| 1930 |
/* Note that value data is always terminated by zero to keep things robust, |
| 1931 |
* this extra zero is not count to the value length. It's caller responsibility |
| 1932 |
* to set proper value length (for strings we count terminator to the length, |
| 1933 |
* for binary data it's without terminator). |
| 1934 |
*/ |
| 1935 |
int blkid_probe_value_set_data(struct blkid_prval *v, |
| 1936 |
const unsigned char *data, size_t len) |
| 1937 |
{ |
| 1938 |
v->data = calloc(1, len + 1); /* always terminate by \0 */ |
| 1939 |
|
| 1940 |
if (!v->data) |
| 1941 |
return -ENOMEM; |
| 1942 |
memcpy(v->data, data, len); |
| 1943 |
v->len = len; |
| 1944 |
return 0; |
| 1945 |
} |
| 1946 |
|
| 1947 |
int blkid_probe_set_value(blkid_probe pr, const char *name, |
| 1948 |
const unsigned char *data, size_t len) |
| 1949 |
{ |
| 1950 |
struct blkid_prval *v; |
| 1951 |
|
| 1952 |
v = blkid_probe_assign_value(pr, name); |
| 1953 |
if (!v) |
| 1954 |
return -1; |
| 1955 |
|
| 1956 |
return blkid_probe_value_set_data(v, data, len); |
| 1957 |
} |
| 1958 |
|
| 1959 |
int blkid_probe_vsprintf_value(blkid_probe pr, const char *name, |
| 1960 |
const char *fmt, va_list ap) |
| 1961 |
{ |
| 1962 |
struct blkid_prval *v; |
| 1963 |
ssize_t len; |
| 1964 |
|
| 1965 |
v = blkid_probe_assign_value(pr, name); |
| 1966 |
if (!v) |
| 1967 |
return -ENOMEM; |
| 1968 |
|
| 1969 |
len = vasprintf((char **) &v->data, fmt, ap); |
| 1970 |
|
| 1971 |
if (len <= 0) { |
| 1972 |
blkid_probe_free_value(v); |
| 1973 |
return len == 0 ? -EINVAL : -ENOMEM; |
| 1974 |
} |
| 1975 |
v->len = len + 1; |
| 1976 |
return 0; |
| 1977 |
} |
| 1978 |
|
| 1979 |
int blkid_probe_sprintf_value(blkid_probe pr, const char *name, |
| 1980 |
const char *fmt, ...) |
| 1981 |
{ |
| 1982 |
int rc; |
| 1983 |
va_list ap; |
| 1984 |
|
| 1985 |
va_start(ap, fmt); |
| 1986 |
rc = blkid_probe_vsprintf_value(pr, name, fmt, ap); |
| 1987 |
va_end(ap); |
| 1988 |
|
| 1989 |
return rc; |
| 1990 |
} |
| 1991 |
|
| 1992 |
int blkid_probe_set_magic(blkid_probe pr, uint64_t offset, |
| 1993 |
size_t len, const unsigned char *magic) |
| 1994 |
{ |
| 1995 |
int rc = 0; |
| 1996 |
struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 1997 |
|
| 1998 |
if (!chn || !len || chn->binary) |
| 1999 |
return 0; |
| 2000 |
|
| 2001 |
switch (chn->driver->id) { |
| 2002 |
case BLKID_CHAIN_SUBLKS: |
| 2003 |
if (!(chn->flags & BLKID_SUBLKS_MAGIC)) |
| 2004 |
return 0; |
| 2005 |
rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len); |
| 2006 |
if (!rc) |
| 2007 |
rc = blkid_probe_sprintf_value(pr, |
| 2008 |
"SBMAGIC_OFFSET", "%llu", (unsigned long long)offset); |
| 2009 |
break; |
| 2010 |
case BLKID_CHAIN_PARTS: |
| 2011 |
if (!(chn->flags & BLKID_PARTS_MAGIC)) |
| 2012 |
return 0; |
| 2013 |
rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len); |
| 2014 |
if (!rc) |
| 2015 |
rc = blkid_probe_sprintf_value(pr, |
| 2016 |
"PTMAGIC_OFFSET", "%llu", (unsigned long long)offset); |
| 2017 |
break; |
| 2018 |
default: |
| 2019 |
break; |
| 2020 |
} |
| 2021 |
|
| 2022 |
return rc; |
| 2023 |
} |
| 2024 |
|
| 2025 |
static void blkid_probe_log_csum_mismatch(blkid_probe pr, size_t n, const void *csum, |
| 2026 |
const void *expected) |
| 2027 |
{ |
| 2028 |
char csum_hex[256]; |
| 2029 |
char expected_hex[sizeof(csum_hex)]; |
| 2030 |
int hex_size = min(sizeof(csum_hex), n * 2); |
| 2031 |
|
| 2032 |
for (int i = 0; i < hex_size; i+=2) { |
| 2033 |
snprintf(&csum_hex[i], sizeof(csum_hex) - i, "%02X", ((const unsigned char *) csum)[i / 2]); |
| 2034 |
snprintf(&expected_hex[i], sizeof(expected_hex) - i, "%02X", ((const unsigned char *) expected)[i / 2]); |
| 2035 |
} |
| 2036 |
|
| 2037 |
DBG(LOWPROBE, ul_debug( |
| 2038 |
"incorrect checksum for type %s," |
| 2039 |
" got %*s, expected %*s", |
| 2040 |
blkid_probe_get_probername(pr), |
| 2041 |
hex_size, csum_hex, hex_size, expected_hex)); |
| 2042 |
} |
| 2043 |
|
| 2044 |
|
| 2045 |
int blkid_probe_verify_csum_buf(blkid_probe pr, size_t n, const void *csum, |
| 2046 |
const void *expected) |
| 2047 |
{ |
| 2048 |
if (memcmp(csum, expected, n) != 0) { |
| 2049 |
struct blkid_chain *chn = blkid_probe_get_chain(pr); |
| 2050 |
|
| 2051 |
ON_DBG(LOWPROBE, blkid_probe_log_csum_mismatch(pr, n, csum, expected)); |
| 2052 |
|
| 2053 |
/* |
| 2054 |
* Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set |
| 2055 |
*/ |
| 2056 |
if (chn && chn->driver->id == BLKID_CHAIN_SUBLKS |
| 2057 |
&& (chn->flags & BLKID_SUBLKS_BADCSUM)) { |
| 2058 |
blkid_probe_set_value(pr, "SBBADCSUM", (unsigned char *) "1", 2); |
| 2059 |
goto accept; |
| 2060 |
} |
| 2061 |
return 0; /* bad checksum */ |
| 2062 |
} |
| 2063 |
|
| 2064 |
accept: |
| 2065 |
return 1; |
| 2066 |
} |
| 2067 |
|
| 2068 |
int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected) |
| 2069 |
{ |
| 2070 |
return blkid_probe_verify_csum_buf(pr, sizeof(csum), &csum, &expected); |
| 2071 |
} |
| 2072 |
|
| 2073 |
/** |
| 2074 |
* blkid_probe_get_devno: |
| 2075 |
* @pr: probe |
| 2076 |
* |
| 2077 |
* Returns: block device number, or 0 for regular files. |
| 2078 |
*/ |
| 2079 |
dev_t blkid_probe_get_devno(blkid_probe pr) |
| 2080 |
{ |
| 2081 |
return pr->devno; |
| 2082 |
} |
| 2083 |
|
| 2084 |
/** |
| 2085 |
* blkid_probe_get_wholedisk_devno: |
| 2086 |
* @pr: probe |
| 2087 |
* |
| 2088 |
* Returns: device number of the wholedisk, or 0 for regular files. |
| 2089 |
*/ |
| 2090 |
dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr) |
| 2091 |
{ |
| 2092 |
if (!pr->disk_devno) { |
| 2093 |
dev_t devno, disk_devno = 0; |
| 2094 |
|
| 2095 |
devno = blkid_probe_get_devno(pr); |
| 2096 |
if (!devno) |
| 2097 |
return 0; |
| 2098 |
|
| 2099 |
if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0) |
| 2100 |
pr->disk_devno = disk_devno; |
| 2101 |
} |
| 2102 |
return pr->disk_devno; |
| 2103 |
} |
| 2104 |
|
| 2105 |
/** |
| 2106 |
* blkid_probe_is_wholedisk: |
| 2107 |
* @pr: probe |
| 2108 |
* |
| 2109 |
* Returns: 1 if the device is whole-disk or 0. |
| 2110 |
*/ |
| 2111 |
int blkid_probe_is_wholedisk(blkid_probe pr) |
| 2112 |
{ |
| 2113 |
dev_t devno, disk_devno; |
| 2114 |
|
| 2115 |
devno = blkid_probe_get_devno(pr); |
| 2116 |
if (!devno) |
| 2117 |
return 0; |
| 2118 |
|
| 2119 |
disk_devno = blkid_probe_get_wholedisk_devno(pr); |
| 2120 |
if (!disk_devno) |
| 2121 |
return 0; |
| 2122 |
|
| 2123 |
return devno == disk_devno; |
| 2124 |
} |
| 2125 |
|
| 2126 |
blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr) |
| 2127 |
{ |
| 2128 |
dev_t disk; |
| 2129 |
|
| 2130 |
if (blkid_probe_is_wholedisk(pr)) |
| 2131 |
return NULL; /* this is not partition */ |
| 2132 |
|
| 2133 |
if (pr->parent) |
| 2134 |
/* this is cloned blkid_probe, use parent's stuff */ |
| 2135 |
return blkid_probe_get_wholedisk_probe(pr->parent); |
| 2136 |
|
| 2137 |
disk = blkid_probe_get_wholedisk_devno(pr); |
| 2138 |
|
| 2139 |
if (pr->disk_probe && pr->disk_probe->devno != disk) { |
| 2140 |
/* we have disk prober, but for another disk... close it */ |
| 2141 |
blkid_free_probe(pr->disk_probe); |
| 2142 |
pr->disk_probe = NULL; |
| 2143 |
} |
| 2144 |
|
| 2145 |
if (!pr->disk_probe) { |
| 2146 |
/* Open a new disk prober */ |
| 2147 |
char *disk_path = blkid_devno_to_devname(disk); |
| 2148 |
int flags; |
| 2149 |
|
| 2150 |
if (!disk_path) |
| 2151 |
return NULL; |
| 2152 |
|
| 2153 |
DBG(LOWPROBE, ul_debug("allocate a wholedisk probe")); |
| 2154 |
|
| 2155 |
pr->disk_probe = blkid_new_probe_from_filename(disk_path); |
| 2156 |
|
| 2157 |
free(disk_path); |
| 2158 |
|
| 2159 |
if (!pr->disk_probe) |
| 2160 |
return NULL; /* ENOMEM? */ |
| 2161 |
|
| 2162 |
flags = blkid_probe_get_partitions_flags(pr); |
| 2163 |
if (flags & BLKID_PARTS_FORCE_GPT) |
| 2164 |
blkid_probe_set_partitions_flags(pr->disk_probe, |
| 2165 |
BLKID_PARTS_FORCE_GPT); |
| 2166 |
} |
| 2167 |
|
| 2168 |
return pr->disk_probe; |
| 2169 |
} |
| 2170 |
|
| 2171 |
/** |
| 2172 |
* blkid_probe_get_size: |
| 2173 |
* @pr: probe |
| 2174 |
* |
| 2175 |
* This function returns size of probing area as defined by blkid_probe_set_device(). |
| 2176 |
* If the size of the probing area is unrestricted then this function returns |
| 2177 |
* the real size of device. See also blkid_get_dev_size(). |
| 2178 |
* |
| 2179 |
* Returns: size in bytes or -1 in case of error. |
| 2180 |
*/ |
| 2181 |
blkid_loff_t blkid_probe_get_size(blkid_probe pr) |
| 2182 |
{ |
| 2183 |
return (blkid_loff_t) pr->size; |
| 2184 |
} |
| 2185 |
|
| 2186 |
/** |
| 2187 |
* blkid_probe_get_offset: |
| 2188 |
* @pr: probe |
| 2189 |
* |
| 2190 |
* This function returns offset of probing area as defined by blkid_probe_set_device(). |
| 2191 |
* |
| 2192 |
* Returns: offset in bytes or -1 in case of error. |
| 2193 |
*/ |
| 2194 |
blkid_loff_t blkid_probe_get_offset(blkid_probe pr) |
| 2195 |
{ |
| 2196 |
return (blkid_loff_t) pr->off; |
| 2197 |
} |
| 2198 |
|
| 2199 |
/** |
| 2200 |
* blkid_probe_get_fd: |
| 2201 |
* @pr: probe |
| 2202 |
* |
| 2203 |
* Returns: file descriptor for assigned device/file or -1 in case of error. |
| 2204 |
*/ |
| 2205 |
int blkid_probe_get_fd(blkid_probe pr) |
| 2206 |
{ |
| 2207 |
return pr->fd; |
| 2208 |
} |
| 2209 |
|
| 2210 |
/** |
| 2211 |
* blkid_probe_get_sectorsize: |
| 2212 |
* @pr: probe or NULL (for NULL returns 512) |
| 2213 |
* |
| 2214 |
* Returns: block device logical sector size (BLKSSZGET ioctl, default 512). |
| 2215 |
*/ |
| 2216 |
unsigned int blkid_probe_get_sectorsize(blkid_probe pr) |
| 2217 |
{ |
| 2218 |
if (pr->blkssz) |
| 2219 |
return pr->blkssz; |
| 2220 |
|
| 2221 |
if (S_ISBLK(pr->mode) && |
| 2222 |
blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0) |
| 2223 |
return pr->blkssz; |
| 2224 |
|
| 2225 |
pr->blkssz = DEFAULT_SECTOR_SIZE; |
| 2226 |
return pr->blkssz; |
| 2227 |
} |
| 2228 |
|
| 2229 |
/** |
| 2230 |
* blkid_probe_set_sectorsize: |
| 2231 |
* @pr: probe |
| 2232 |
* @sz: new size (to overwrite system default) |
| 2233 |
* |
| 2234 |
* Note that blkid_probe_set_device() resets this setting. Use it after |
| 2235 |
* blkid_probe_set_device() and before any probing call. |
| 2236 |
* |
| 2237 |
* Since: 2.30 |
| 2238 |
* |
| 2239 |
* Returns: 0 or <0 in case of error |
| 2240 |
*/ |
| 2241 |
int blkid_probe_set_sectorsize(blkid_probe pr, unsigned int sz) |
| 2242 |
{ |
| 2243 |
pr->blkssz = sz; |
| 2244 |
return 0; |
| 2245 |
} |
| 2246 |
|
| 2247 |
/** |
| 2248 |
* blkid_probe_get_sectors: |
| 2249 |
* @pr: probe |
| 2250 |
* |
| 2251 |
* Returns: 512-byte sector count or -1 in case of error. |
| 2252 |
*/ |
| 2253 |
blkid_loff_t blkid_probe_get_sectors(blkid_probe pr) |
| 2254 |
{ |
| 2255 |
return (blkid_loff_t) (pr->size >> 9); |
| 2256 |
} |
| 2257 |
|
| 2258 |
/** |
| 2259 |
* blkid_probe_numof_values: |
| 2260 |
* @pr: probe |
| 2261 |
* |
| 2262 |
* Returns: number of values in probing result or -1 in case of error. |
| 2263 |
*/ |
| 2264 |
int blkid_probe_numof_values(blkid_probe pr) |
| 2265 |
{ |
| 2266 |
int i = 0; |
| 2267 |
struct list_head *p; |
| 2268 |
|
| 2269 |
list_for_each(p, &pr->values) |
| 2270 |
++i; |
| 2271 |
return i; |
| 2272 |
} |
| 2273 |
|
| 2274 |
/** |
| 2275 |
* blkid_probe_get_value: |
| 2276 |
* @pr: probe |
| 2277 |
* @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1 |
| 2278 |
* @name: pointer to return value name or NULL |
| 2279 |
* @data: pointer to return value data or NULL |
| 2280 |
* @len: pointer to return value length or NULL |
| 2281 |
* |
| 2282 |
* Note, the @len returns length of the @data, including the terminating |
| 2283 |
* '\0' character. |
| 2284 |
* |
| 2285 |
* Returns: 0 on success, or -1 in case of error. |
| 2286 |
*/ |
| 2287 |
int blkid_probe_get_value(blkid_probe pr, int num, const char **name, |
| 2288 |
const char **data, size_t *len) |
| 2289 |
{ |
| 2290 |
struct blkid_prval *v = __blkid_probe_get_value(pr, num); |
| 2291 |
|
| 2292 |
if (!v) |
| 2293 |
return -1; |
| 2294 |
if (name) |
| 2295 |
*name = v->name; |
| 2296 |
if (data) |
| 2297 |
*data = (char *) v->data; |
| 2298 |
if (len) |
| 2299 |
*len = v->len; |
| 2300 |
|
| 2301 |
DBG(LOWPROBE, ul_debug("returning %s value", v->name)); |
| 2302 |
return 0; |
| 2303 |
} |
| 2304 |
|
| 2305 |
/** |
| 2306 |
* blkid_probe_lookup_value: |
| 2307 |
* @pr: probe |
| 2308 |
* @name: name of value |
| 2309 |
* @data: pointer to return value data or NULL |
| 2310 |
* @len: pointer to return value length or NULL |
| 2311 |
* |
| 2312 |
* Note, the @len returns length of the @data, including the terminating |
| 2313 |
* '\0' character. |
| 2314 |
* |
| 2315 |
* Returns: 0 on success, or -1 in case of error. |
| 2316 |
*/ |
| 2317 |
int blkid_probe_lookup_value(blkid_probe pr, const char *name, |
| 2318 |
const char **data, size_t *len) |
| 2319 |
{ |
| 2320 |
struct blkid_prval *v = __blkid_probe_lookup_value(pr, name); |
| 2321 |
|
| 2322 |
if (!v) |
| 2323 |
return -1; |
| 2324 |
if (data) |
| 2325 |
*data = (char *) v->data; |
| 2326 |
if (len) |
| 2327 |
*len = v->len; |
| 2328 |
return 0; |
| 2329 |
} |
| 2330 |
|
| 2331 |
/** |
| 2332 |
* blkid_probe_has_value: |
| 2333 |
* @pr: probe |
| 2334 |
* @name: name of value |
| 2335 |
* |
| 2336 |
* Returns: 1 if value exist in probing result, otherwise 0. |
| 2337 |
*/ |
| 2338 |
int blkid_probe_has_value(blkid_probe pr, const char *name) |
| 2339 |
{ |
| 2340 |
if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0) |
| 2341 |
return 1; |
| 2342 |
return 0; |
| 2343 |
} |
| 2344 |
|
| 2345 |
struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num) |
| 2346 |
{ |
| 2347 |
int i = 0; |
| 2348 |
struct list_head *p; |
| 2349 |
|
| 2350 |
if (num < 0) |
| 2351 |
return NULL; |
| 2352 |
|
| 2353 |
list_for_each(p, &pr->values) { |
| 2354 |
if (i++ != num) |
| 2355 |
continue; |
| 2356 |
return list_entry(p, struct blkid_prval, prvals); |
| 2357 |
} |
| 2358 |
return NULL; |
| 2359 |
} |
| 2360 |
|
| 2361 |
struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name) |
| 2362 |
{ |
| 2363 |
struct list_head *p; |
| 2364 |
|
| 2365 |
if (list_empty(&pr->values)) |
| 2366 |
return NULL; |
| 2367 |
|
| 2368 |
list_for_each(p, &pr->values) { |
| 2369 |
struct blkid_prval *v = list_entry(p, struct blkid_prval, |
| 2370 |
prvals); |
| 2371 |
|
| 2372 |
if (v->name && strcmp(name, v->name) == 0) { |
| 2373 |
DBG(LOWPROBE, ul_debug("returning %s value", v->name)); |
| 2374 |
return v; |
| 2375 |
} |
| 2376 |
} |
| 2377 |
return NULL; |
| 2378 |
} |
| 2379 |
|
| 2380 |
|
| 2381 |
/* converts DCE UUID (uuid[16]) to human readable string |
| 2382 |
* - the @len should be always 37 */ |
| 2383 |
void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len) |
| 2384 |
{ |
| 2385 |
snprintf(str, len, |
| 2386 |
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 2387 |
uuid[0], uuid[1], uuid[2], uuid[3], |
| 2388 |
uuid[4], uuid[5], |
| 2389 |
uuid[6], uuid[7], |
| 2390 |
uuid[8], uuid[9], |
| 2391 |
uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]); |
| 2392 |
} |
| 2393 |
|
| 2394 |
/* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */ |
| 2395 |
int blkid_uuid_is_empty(const unsigned char *buf, size_t len) |
| 2396 |
{ |
| 2397 |
size_t i; |
| 2398 |
|
| 2399 |
for (i = 0; i < len; i++) |
| 2400 |
if (buf[i]) |
| 2401 |
return 0; |
| 2402 |
return 1; |
| 2403 |
} |
| 2404 |
|
| 2405 |
/* Removes whitespace from the right-hand side of a string (trailing |
| 2406 |
* whitespace). |
| 2407 |
* |
| 2408 |
* Returns size of the new string (without \0). |
| 2409 |
*/ |
| 2410 |
size_t blkid_rtrim_whitespace(unsigned char *str) |
| 2411 |
{ |
| 2412 |
return rtrim_whitespace(str); |
| 2413 |
} |
| 2414 |
|
| 2415 |
/* Removes whitespace from the left-hand side of a string. |
| 2416 |
* |
| 2417 |
* Returns size of the new string (without \0). |
| 2418 |
*/ |
| 2419 |
size_t blkid_ltrim_whitespace(unsigned char *str) |
| 2420 |
{ |
| 2421 |
return ltrim_whitespace(str); |
| 2422 |
} |
| 2423 |
|
| 2424 |
/* |
| 2425 |
* Some mkfs-like utils wipe some parts (usually begin) of the device. |
| 2426 |
* For example LVM (pvcreate) or mkswap(8). This information could be used |
| 2427 |
* for later resolution to conflicts between superblocks. |
| 2428 |
* |
| 2429 |
* For example we found valid LVM superblock, LVM wipes 8KiB at the begin of |
| 2430 |
* the device. If we found another signature (for example MBR) within the |
| 2431 |
* wiped area then the signature has been added later and LVM superblock |
| 2432 |
* should be ignore. |
| 2433 |
* |
| 2434 |
* Note that this heuristic is not 100% reliable, for example "pvcreate --zero n" |
| 2435 |
* can be used to keep the begin of the device unmodified. It's probably better |
| 2436 |
* to use this heuristic for conflicts between superblocks and partition tables |
| 2437 |
* than for conflicts between filesystem superblocks -- existence of unwanted |
| 2438 |
* partition table is very unusual, because PT is pretty visible (parsed and |
| 2439 |
* interpreted by kernel). |
| 2440 |
* |
| 2441 |
* Note that we usually expect only one signature on the device, it means that |
| 2442 |
* we have to remember only one wiped area from previously successfully |
| 2443 |
* detected signature. |
| 2444 |
* |
| 2445 |
* blkid_probe_set_wiper() -- defines wiped area (e.g. LVM) |
| 2446 |
* blkid_probe_use_wiper() -- try to use area (e.g. MBR) |
| 2447 |
* |
| 2448 |
* Note that there is not relation between _wiper and blkid_to_wipe(). |
| 2449 |
* |
| 2450 |
*/ |
| 2451 |
void blkid_probe_set_wiper(blkid_probe pr, uint64_t off, uint64_t size) |
| 2452 |
{ |
| 2453 |
struct blkid_chain *chn; |
| 2454 |
|
| 2455 |
if (!size) { |
| 2456 |
DBG(LOWPROBE, ul_debug("zeroize wiper")); |
| 2457 |
pr->wipe_size = pr->wipe_off = 0; |
| 2458 |
pr->wipe_chain = NULL; |
| 2459 |
return; |
| 2460 |
} |
| 2461 |
|
| 2462 |
chn = pr->cur_chain; |
| 2463 |
|
| 2464 |
if (!chn || !chn->driver || |
| 2465 |
chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos) |
| 2466 |
return; |
| 2467 |
|
| 2468 |
pr->wipe_size = size; |
| 2469 |
pr->wipe_off = off; |
| 2470 |
pr->wipe_chain = chn; |
| 2471 |
|
| 2472 |
DBG(LOWPROBE, |
| 2473 |
ul_debug("wiper set to %s::%s off=%"PRIu64" size=%"PRIu64"", |
| 2474 |
chn->driver->name, |
| 2475 |
chn->driver->idinfos[chn->idx]->name, |
| 2476 |
pr->wipe_off, pr->wipe_size)); |
| 2477 |
} |
| 2478 |
|
| 2479 |
/* |
| 2480 |
* Returns 1 if the <@off,@size> area was wiped |
| 2481 |
*/ |
| 2482 |
int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn, uint64_t off, uint64_t size) |
| 2483 |
{ |
| 2484 |
if (!size) |
| 2485 |
return 0; |
| 2486 |
|
| 2487 |
if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) { |
| 2488 |
*chn = pr->wipe_chain; |
| 2489 |
return 1; |
| 2490 |
} |
| 2491 |
return 0; |
| 2492 |
} |
| 2493 |
|
| 2494 |
/* |
| 2495 |
* Try to use any area -- if the area has been previously wiped then the |
| 2496 |
* previous probing result should be ignored (reset). |
| 2497 |
*/ |
| 2498 |
void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size) |
| 2499 |
{ |
| 2500 |
struct blkid_chain *chn = NULL; |
| 2501 |
|
| 2502 |
if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) { |
| 2503 |
DBG(LOWPROBE, ul_debug("previously wiped area modified " |
| 2504 |
" -- ignore previous results")); |
| 2505 |
blkid_probe_set_wiper(pr, 0, 0); |
| 2506 |
blkid_probe_chain_reset_values(pr, chn); |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
static struct blkid_hint *get_hint(blkid_probe pr, const char *name) |
| 2511 |
{ |
| 2512 |
struct list_head *p; |
| 2513 |
|
| 2514 |
if (list_empty(&pr->hints)) |
| 2515 |
return NULL; |
| 2516 |
|
| 2517 |
list_for_each(p, &pr->hints) { |
| 2518 |
struct blkid_hint *h = list_entry(p, struct blkid_hint, hints); |
| 2519 |
|
| 2520 |
if (h->name && strcmp(name, h->name) == 0) |
| 2521 |
return h; |
| 2522 |
} |
| 2523 |
return NULL; |
| 2524 |
} |
| 2525 |
|
| 2526 |
/** |
| 2527 |
* blkid_probe_set_hint: |
| 2528 |
* @pr: probe |
| 2529 |
* @name: hint name or NAME=value |
| 2530 |
* @value: offset or another number |
| 2531 |
* |
| 2532 |
* Sets extra hint for low-level prober. If the hint is set by NAME=value |
| 2533 |
* notation than @value is ignored. The functions blkid_probe_set_device() |
| 2534 |
* and blkid_reset_probe() resets all hints. |
| 2535 |
* |
| 2536 |
* The hints are optional way how to force libblkid probing functions to check |
| 2537 |
* for example another location. |
| 2538 |
* |
| 2539 |
* Returns: 0 on success, or -1 in case of error. |
| 2540 |
*/ |
| 2541 |
int blkid_probe_set_hint(blkid_probe pr, const char *name, uint64_t value) |
| 2542 |
{ |
| 2543 |
struct blkid_hint *hint = NULL; |
| 2544 |
char *n = NULL, *v = NULL; |
| 2545 |
|
| 2546 |
if (strchr(name, '=')) { |
| 2547 |
char *end = NULL; |
| 2548 |
|
| 2549 |
if (blkid_parse_tag_string(name, &n, &v) != 0) |
| 2550 |
goto done; |
| 2551 |
|
| 2552 |
errno = 0; |
| 2553 |
value = strtoumax(v, &end, 10); |
| 2554 |
|
| 2555 |
if (errno || v == end || (end && *end)) |
| 2556 |
goto done; |
| 2557 |
} |
| 2558 |
|
| 2559 |
hint = get_hint(pr, n ? n : name); |
| 2560 |
if (hint) { |
| 2561 |
/* alter old hint */ |
| 2562 |
hint->value = value; |
| 2563 |
DBG(LOWPROBE, |
| 2564 |
ul_debug("updated hint '%s' to %"PRIu64"", hint->name, hint->value)); |
| 2565 |
} else { |
| 2566 |
/* add a new hint */ |
| 2567 |
if (!n) { |
| 2568 |
n = strdup(name); |
| 2569 |
if (!n) |
| 2570 |
goto done; |
| 2571 |
} |
| 2572 |
hint = malloc(sizeof(*hint)); |
| 2573 |
if (!hint) |
| 2574 |
goto done; |
| 2575 |
|
| 2576 |
hint->name = n; |
| 2577 |
hint->value = value; |
| 2578 |
|
| 2579 |
INIT_LIST_HEAD(&hint->hints); |
| 2580 |
list_add_tail(&hint->hints, &pr->hints); |
| 2581 |
|
| 2582 |
DBG(LOWPROBE, |
| 2583 |
ul_debug("new hint '%s' is %"PRIu64"", hint->name, hint->value)); |
| 2584 |
n = NULL; |
| 2585 |
} |
| 2586 |
done: |
| 2587 |
free(n); |
| 2588 |
free(v); |
| 2589 |
|
| 2590 |
if (!hint) |
| 2591 |
return errno ? -errno : -EINVAL; |
| 2592 |
return 0; |
| 2593 |
} |
| 2594 |
|
| 2595 |
int blkid_probe_get_hint(blkid_probe pr, const char *name, uint64_t *value) |
| 2596 |
{ |
| 2597 |
struct blkid_hint *h = get_hint(pr, name); |
| 2598 |
|
| 2599 |
if (!h) |
| 2600 |
return -EINVAL; |
| 2601 |
if (value) |
| 2602 |
*value = h->value; |
| 2603 |
return 0; |
| 2604 |
} |
| 2605 |
|
| 2606 |
/** |
| 2607 |
* blkid_probe_reset_hints: |
| 2608 |
* @pr: probe |
| 2609 |
* |
| 2610 |
* Removes all previously defined probinig hints. See also blkid_probe_set_hint(). |
| 2611 |
*/ |
| 2612 |
void blkid_probe_reset_hints(blkid_probe pr) |
| 2613 |
{ |
| 2614 |
if (list_empty(&pr->hints)) |
| 2615 |
return; |
| 2616 |
|
| 2617 |
DBG(LOWPROBE, ul_debug("resetting hints")); |
| 2618 |
|
| 2619 |
while (!list_empty(&pr->hints)) { |
| 2620 |
struct blkid_hint *h = list_entry(pr->hints.next, |
| 2621 |
struct blkid_hint, hints); |
| 2622 |
list_del(&h->hints); |
| 2623 |
free(h->name); |
| 2624 |
free(h); |
| 2625 |
} |
| 2626 |
|
| 2627 |
INIT_LIST_HEAD(&pr->hints); |
| 2628 |
} |