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