]>
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; |
025b1146 | 733 | uint64_t real_off, bias, len_align; |
8a534253 TW |
734 | |
735 | bias = off % pr->io_size; | |
736 | off -= bias; | |
737 | len += bias; | |
025b1146 TW |
738 | |
739 | if (len % pr->io_size) { | |
740 | len_align = pr->io_size - (len % pr->io_size); | |
741 | ||
742 | if (pr->off + off + len + len_align <= pr->size) | |
743 | len += len_align; | |
744 | } | |
8a534253 TW |
745 | |
746 | real_off = pr->off + off; | |
a674a0ab KZ |
747 | |
748 | /* | |
749 | DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju", | |
750 | off, real_off, pr->off, pr->off + pr->size, len)); | |
751 | */ | |
8a534253 | 752 | if (pr->size == 0 || pr->io_size == 0) { |
00749bc3 | 753 | errno = EINVAL; |
88923b08 | 754 | return NULL; |
00749bc3 | 755 | } |
88923b08 | 756 | |
106de261 KZ |
757 | if (UINT64_MAX - len < off || UINT64_MAX - len < real_off) { |
758 | DBG(BUFFER, ul_debug("\t read-buffer overflow (ignore)")); | |
759 | return NULL; | |
760 | } | |
761 | ||
65b1f3de TW |
762 | if (len > 8388608 /* 8 Mib */ ) { |
763 | DBG(BUFFER, ul_debug("\t too large read request (ignore)")); | |
764 | return NULL; | |
765 | } | |
766 | ||
106de261 KZ |
767 | if (len == 0 |
768 | || (!S_ISCHR(pr->mode) && (pr->size < off || pr->size < len)) | |
769 | || (!S_ISCHR(pr->mode) && (pr->off + pr->size < real_off + len))) { | |
770 | DBG(BUFFER, ul_debug("\t read-buffer out of probing area (ignore)")); | |
a674a0ab KZ |
771 | errno = 0; |
772 | return NULL; | |
773 | } | |
774 | ||
fd9f45e1 KZ |
775 | if (pr->parent && |
776 | pr->parent->devno == pr->devno && | |
ac8874ca KZ |
777 | pr->parent->off <= pr->off && |
778 | pr->parent->off + pr->parent->size >= pr->off + pr->size) { | |
fd9f45e1 KZ |
779 | /* |
780 | * This is a cloned prober and points to the same area as | |
ac8874ca KZ |
781 | * parent. Let's use parent's buffers. |
782 | * | |
783 | * Note that pr->off (and pr->parent->off) is always from the | |
a674a0ab | 784 | * begin of the device. |
fd9f45e1 | 785 | */ |
ac8874ca KZ |
786 | return blkid_probe_get_buffer(pr->parent, |
787 | pr->off + off - pr->parent->off, len); | |
788 | } | |
fd9f45e1 | 789 | |
d2b0c658 KZ |
790 | /* try buffers we already have in memory or read from device */ |
791 | bf = get_cached_buffer(pr, off, len); | |
a674a0ab | 792 | if (!bf) { |
e04f3860 | 793 | bf = read_buffer(pr, real_off, len); |
a674a0ab | 794 | if (!bf) |
1ca17f91 | 795 | return NULL; |
1ca17f91 | 796 | |
77b85278 | 797 | mark_prunable_buffers(pr, bf); |
15a8fb42 | 798 | list_add_tail(&bf->bufs, &pr->buffers); |
1ca17f91 | 799 | } |
15a8fb42 | 800 | |
a674a0ab KZ |
801 | assert(bf->off <= real_off); |
802 | assert(bf->off + bf->len >= real_off + len); | |
803 | ||
00749bc3 | 804 | errno = 0; |
8a534253 | 805 | return real_off ? bf->data + (real_off - bf->off + bias) : bf->data + bias; |
1ca17f91 KZ |
806 | } |
807 | ||
d2b0c658 KZ |
808 | /** |
809 | * blkid_probe_reset_buffers: | |
810 | * @pr: prober | |
811 | * | |
311e33af | 812 | * libblkid reuse all already read buffers from the device. The buffers may be |
d2b0c658 | 813 | * modified by blkid_probe_hide_range(). This function reset and free all |
311e33af | 814 | * cached buffers. The next blkid_do_probe() will read all data from the |
d2b0c658 KZ |
815 | * device. |
816 | * | |
95fbd2db KZ |
817 | * Since: 2.31 |
818 | * | |
d2b0c658 KZ |
819 | * Returns: <0 in case of failure, or 0 on success. |
820 | */ | |
821 | int blkid_probe_reset_buffers(blkid_probe pr) | |
a0948ffe | 822 | { |
2355dd6a | 823 | uint64_t ct = 0, len = 0; |
15a8fb42 | 824 | |
d2b0c658 KZ |
825 | pr->flags &= ~BLKID_FL_MODIF_BUFF; |
826 | ||
77b85278 TW |
827 | blkid_probe_prune_buffers(pr); |
828 | ||
7f787ced | 829 | if (list_empty(&pr->buffers)) |
d2b0c658 | 830 | return 0; |
15a8fb42 | 831 | |
63c9c05d | 832 | DBG(BUFFER, ul_debug("Resetting probing buffers")); |
15a8fb42 KZ |
833 | |
834 | while (!list_empty(&pr->buffers)) { | |
835 | struct blkid_bufinfo *bf = list_entry(pr->buffers.next, | |
836 | struct blkid_bufinfo, bufs); | |
2355dd6a KZ |
837 | ct++; |
838 | len += bf->len; | |
a674a0ab | 839 | |
2dc39ee6 | 840 | remove_buffer(bf); |
4884729a | 841 | } |
15a8fb42 | 842 | |
e04f3860 | 843 | DBG(LOWPROBE, ul_debug(" buffers summary: %"PRIu64" bytes by %"PRIu64" read() calls", |
2355dd6a | 844 | len, ct)); |
15a8fb42 KZ |
845 | |
846 | INIT_LIST_HEAD(&pr->buffers); | |
d2b0c658 KZ |
847 | |
848 | return 0; | |
849 | } | |
850 | ||
851 | /** | |
852 | * blkid_probe_hide_range: | |
853 | * @pr: prober | |
854 | * @off: start of the range | |
855 | * @len: size of the range | |
856 | * | |
857 | * This function modifies in-memory cached data from the device. The specified | |
858 | * range is zeroized. This is usable together with blkid_probe_step_back(). | |
859 | * The next blkid_do_probe() will not see specified area. | |
860 | * | |
861 | * Note that this is usable for already (by library) read data, and this | |
862 | * function is not a way how to hide any large areas on your device. | |
863 | * | |
864 | * The function blkid_probe_reset_buffers() reverts all. | |
865 | * | |
95fbd2db KZ |
866 | * Since: 2.31 |
867 | * | |
d2b0c658 KZ |
868 | * Returns: <0 in case of failure, or 0 on success. |
869 | */ | |
870 | int blkid_probe_hide_range(blkid_probe pr, uint64_t off, uint64_t len) | |
871 | { | |
872 | int rc = hide_buffer(pr, off, len); | |
873 | ||
874 | if (rc == 0) | |
875 | pr->flags |= BLKID_FL_MODIF_BUFF; | |
876 | return rc; | |
a0948ffe KZ |
877 | } |
878 | ||
67719fbb | 879 | |
af17d349 | 880 | static void blkid_probe_reset_values(blkid_probe pr) |
6c4a7811 | 881 | { |
7f787ced | 882 | if (list_empty(&pr->values)) |
6c4a7811 OO |
883 | return; |
884 | ||
63c9c05d | 885 | DBG(LOWPROBE, ul_debug("resetting results")); |
6c4a7811 | 886 | |
af17d349 KZ |
887 | while (!list_empty(&pr->values)) { |
888 | struct blkid_prval *v = list_entry(pr->values.next, | |
6c4a7811 | 889 | struct blkid_prval, prvals); |
af17d349 | 890 | blkid_probe_free_value(v); |
6c4a7811 OO |
891 | } |
892 | ||
af17d349 | 893 | INIT_LIST_HEAD(&pr->values); |
6c4a7811 OO |
894 | } |
895 | ||
108013b4 KZ |
896 | /* |
897 | * Small devices need a special care. | |
898 | */ | |
899 | int blkid_probe_is_tiny(blkid_probe pr) | |
900 | { | |
7f787ced | 901 | return (pr->flags & BLKID_FL_TINY_DEV); |
108013b4 KZ |
902 | } |
903 | ||
55113b15 C |
904 | /* |
905 | * CDROMs may fail when probed for RAID (last sector problem) | |
906 | */ | |
907 | int blkid_probe_is_cdrom(blkid_probe pr) | |
908 | { | |
7f787ced | 909 | return (pr->flags & BLKID_FL_CDROM_DEV); |
55113b15 C |
910 | } |
911 | ||
a0cf9105 LB |
912 | int blkdid_probe_is_opal_locked(blkid_probe pr) |
913 | { | |
914 | return (pr->flags & BLKID_FL_OPAL_LOCKED); | |
915 | } | |
916 | ||
332123f2 RM |
917 | #ifdef CDROM_GET_CAPABILITY |
918 | ||
bfebe74e KZ |
919 | static int is_sector_readable(int fd, uint64_t sector) |
920 | { | |
921 | char buf[512]; | |
922 | ssize_t sz; | |
923 | ||
a732e4a1 | 924 | if (lseek(fd, sector * 512, SEEK_SET) == (off_t) -1) |
bfebe74e KZ |
925 | goto failed; |
926 | ||
927 | sz = read(fd, buf, sizeof(buf)); | |
928 | if (sz != (ssize_t) sizeof(buf)) | |
929 | goto failed; | |
930 | ||
931 | return 1; | |
932 | failed: | |
eaaf0e7e | 933 | DBG(LOWPROBE, ul_debug("CDROM: read sector %"PRIu64" failed %m", sector)); |
bfebe74e KZ |
934 | errno = 0; |
935 | return 0; | |
936 | } | |
937 | ||
938 | /* | |
6548ac6a KZ |
939 | * Linux kernel reports (BLKGETSIZE) cdrom device size greater than area |
940 | * readable by read(2). We have to reduce the probing area to avoid unwanted | |
941 | * I/O errors in probing functions. It seems that unreadable are always last 2 | |
942 | * or 3 CD blocks (CD block size is 2048 bytes, it means 12 in 512-byte | |
bfd4da56 PR |
943 | * sectors). Linux kernel reports (CDROM_LAST_WRITTEN) also location of last |
944 | * written block, so we will reduce size based on it too. | |
bfebe74e | 945 | */ |
bfd4da56 | 946 | static void cdrom_size_correction(blkid_probe pr, uint64_t last_written) |
bfebe74e | 947 | { |
6548ac6a | 948 | uint64_t n, nsectors = pr->size >> 9; |
bfebe74e | 949 | |
bfd4da56 PR |
950 | if (last_written && nsectors > ((last_written+1) << 2)) |
951 | nsectors = (last_written+1) << 2; | |
952 | ||
bfebe74e KZ |
953 | for (n = nsectors - 12; n < nsectors; n++) { |
954 | if (!is_sector_readable(pr->fd, n)) | |
955 | goto failed; | |
956 | } | |
957 | ||
958 | DBG(LOWPROBE, ul_debug("CDROM: full size available")); | |
959 | return; | |
960 | failed: | |
961 | /* 'n' is the failed sector, reduce device size to n-1; */ | |
962 | DBG(LOWPROBE, ul_debug("CDROM: reduce size from %ju to %ju.", | |
963 | (uintmax_t) pr->size, | |
1bd62f72 | 964 | (uintmax_t) n << 9)); |
bfebe74e KZ |
965 | pr->size = n << 9; |
966 | } | |
967 | ||
332123f2 RM |
968 | #endif |
969 | ||
224f2998 | 970 | #ifdef BLKIOOPT |
8a534253 TW |
971 | static uint64_t blkid_get_io_size(int fd) |
972 | { | |
973 | static const int ioctls[] = { BLKIOOPT, BLKIOMIN, BLKBSZGET }; | |
974 | unsigned int s; | |
975 | size_t i; | |
976 | int r; | |
977 | ||
978 | for (i = 0; i < ARRAY_SIZE(ioctls); i++) { | |
979 | r = ioctl(fd, ioctls[i], &s); | |
980 | if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE) | |
981 | return min(s, 1U << 16); | |
982 | } | |
983 | ||
984 | return DEFAULT_SECTOR_SIZE; | |
985 | } | |
224f2998 | 986 | #endif |
8a534253 | 987 | |
52448df8 KZ |
988 | /** |
989 | * blkid_probe_set_device: | |
990 | * @pr: probe | |
991 | * @fd: device file descriptor | |
992 | * @off: begin of probing area | |
f38db0cf | 993 | * @size: size of probing area (zero means whole device/file) |
52448df8 | 994 | * |
c4d6d1c5 KZ |
995 | * Assigns the device to probe control struct, resets internal buffers, resets |
996 | * the current probing, and close previously associated device (if open by | |
997 | * libblkid). | |
51410fc6 | 998 | * |
c4d6d1c5 KZ |
999 | * If @fd is < 0 than only resets the prober and returns 1. Note that |
1000 | * blkid_reset_probe() keeps the device associated with the prober, but | |
1001 | * blkid_probe_set_device() does complete reset. | |
1002 | * | |
1003 | * Returns: -1 in case of failure, 0 on success and 1 on reset. | |
51410fc6 KZ |
1004 | */ |
1005 | int blkid_probe_set_device(blkid_probe pr, int fd, | |
1006 | blkid_loff_t off, blkid_loff_t size) | |
a0948ffe | 1007 | { |
90ec8d9c | 1008 | struct stat sb; |
f12cd8d1 | 1009 | uint64_t devsiz = 0; |
884659b3 | 1010 | char *dm_uuid = NULL; |
f5ea9eee | 1011 | int is_floppy = 0; |
90ec8d9c | 1012 | |
51410fc6 | 1013 | blkid_reset_probe(pr); |
d2b0c658 | 1014 | blkid_probe_reset_buffers(pr); |
a0948ffe | 1015 | |
a9eef56c | 1016 | if ((pr->flags & BLKID_FL_PRIVATE_FD) && pr->fd >= 0) |
f38db0cf KZ |
1017 | close(pr->fd); |
1018 | ||
c4d6d1c5 KZ |
1019 | if (pr->disk_probe) { |
1020 | blkid_free_probe(pr->disk_probe); | |
1021 | pr->disk_probe = NULL; | |
1022 | } | |
1023 | ||
a9eef56c KZ |
1024 | pr->flags &= ~BLKID_FL_PRIVATE_FD; |
1025 | pr->flags &= ~BLKID_FL_TINY_DEV; | |
1026 | pr->flags &= ~BLKID_FL_CDROM_DEV; | |
ccdf9fda | 1027 | pr->prob_flags = 0; |
51410fc6 | 1028 | pr->fd = fd; |
f12cd8d1 | 1029 | pr->off = (uint64_t) off; |
bb6c6673 | 1030 | pr->size = 0; |
8a534253 | 1031 | pr->io_size = DEFAULT_SECTOR_SIZE; |
52448df8 | 1032 | pr->devno = 0; |
ccdf9fda | 1033 | pr->disk_devno = 0; |
52448df8 KZ |
1034 | pr->mode = 0; |
1035 | pr->blkssz = 0; | |
ccdf9fda KZ |
1036 | pr->wipe_off = 0; |
1037 | pr->wipe_size = 0; | |
1038 | pr->wipe_chain = NULL; | |
b250fbbf | 1039 | pr->zone_size = 0; |
dc61d909 | 1040 | |
c4d6d1c5 KZ |
1041 | if (fd < 0) |
1042 | return 1; | |
1043 | ||
2d24f963 | 1044 | |
a67bb3bf LT |
1045 | #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE) |
1046 | /* Disable read-ahead */ | |
1047 | posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM); | |
1048 | #endif | |
90ec8d9c KZ |
1049 | if (fstat(fd, &sb)) |
1050 | goto err; | |
1051 | ||
a674a0ab | 1052 | if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode) && !S_ISREG(sb.st_mode)) { |
2355dd6a | 1053 | errno = EINVAL; |
26eb5a59 | 1054 | goto err; |
a674a0ab | 1055 | } |
20e1c3dc | 1056 | |
90ec8d9c KZ |
1057 | pr->mode = sb.st_mode; |
1058 | if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) | |
1059 | pr->devno = sb.st_rdev; | |
1060 | ||
a674a0ab KZ |
1061 | if (S_ISBLK(sb.st_mode)) { |
1062 | if (blkdev_get_size(fd, (unsigned long long *) &devsiz)) { | |
1063 | DBG(LOWPROBE, ul_debug("failed to get device size")); | |
108013b4 | 1064 | goto err; |
a674a0ab | 1065 | } |
7eb6d9ce KZ |
1066 | } else if (S_ISCHR(sb.st_mode)) { |
1067 | char buf[PATH_MAX]; | |
1068 | ||
1069 | if (!sysfs_chrdev_devno_to_devname(sb.st_rdev, buf, sizeof(buf)) | |
1070 | || strncmp(buf, "ubi", 3) != 0) { | |
1071 | DBG(LOWPROBE, ul_debug("no UBI char device")); | |
1072 | errno = EINVAL; | |
1073 | goto err; | |
1074 | } | |
a674a0ab | 1075 | devsiz = 1; /* UBI devices are char... */ |
7eb6d9ce | 1076 | } else if (S_ISREG(sb.st_mode)) |
a674a0ab KZ |
1077 | devsiz = sb.st_size; /* regular file */ |
1078 | ||
b9710f1f | 1079 | pr->size = size ? (uint64_t)size : devsiz; |
108013b4 | 1080 | |
a674a0ab KZ |
1081 | if (off && size == 0) |
1082 | /* only offset without size specified */ | |
f12cd8d1 | 1083 | pr->size -= (uint64_t) off; |
a674a0ab KZ |
1084 | |
1085 | if (pr->off + pr->size > devsiz) { | |
1086 | DBG(LOWPROBE, ul_debug("area specified by offset and size is bigger than device")); | |
1087 | errno = EINVAL; | |
1088 | goto err; | |
bb6c6673 | 1089 | } |
c1ba7962 | 1090 | |
90ec8d9c | 1091 | if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode)) |
a9eef56c | 1092 | pr->flags |= BLKID_FL_TINY_DEV; |
d0465c3c | 1093 | |
f5ea9eee KZ |
1094 | #ifdef FDGETFDCSTAT |
1095 | if (S_ISBLK(sb.st_mode)) { | |
1096 | /* | |
1097 | * Re-open without O_NONBLOCK for floppy device. | |
1098 | * | |
1099 | * Since kernel commit c7e9d0020361f4308a70cdfd6d5335e273eb8717 | |
1100 | * floppy drive works bad when opened with O_NONBLOCK. | |
1101 | */ | |
1102 | struct floppy_fdc_state flst; | |
1103 | ||
1104 | if (ioctl(fd, FDGETFDCSTAT, &flst) >= 0) { | |
1105 | int flags = fcntl(fd, F_GETFL, 0); | |
1106 | ||
1107 | if (flags < 0) | |
1108 | goto err; | |
1109 | if (flags & O_NONBLOCK) { | |
1110 | flags &= ~O_NONBLOCK; | |
1111 | ||
1112 | fd = ul_reopen(fd, flags | O_CLOEXEC); | |
1113 | if (fd < 0) | |
1114 | goto err; | |
1115 | ||
1116 | pr->flags |= BLKID_FL_PRIVATE_FD; | |
1117 | pr->fd = fd; | |
1118 | } | |
1119 | is_floppy = 1; | |
1120 | } | |
1121 | errno = 0; | |
1122 | } | |
1123 | #endif | |
884659b3 | 1124 | if (S_ISBLK(sb.st_mode) && |
f5ea9eee | 1125 | !is_floppy && |
80ec018c TA |
1126 | sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) { |
1127 | DBG(LOWPROBE, ul_debug("ignore private device mapper device")); | |
20e1c3dc KZ |
1128 | pr->flags |= BLKID_FL_NOSCAN_DEV; |
1129 | } | |
1130 | ||
55113b15 | 1131 | #ifdef CDROM_GET_CAPABILITY |
20e1c3dc | 1132 | else if (S_ISBLK(sb.st_mode) && |
a3ab71cf | 1133 | !blkid_probe_is_tiny(pr) && |
884659b3 | 1134 | !dm_uuid && |
f5ea9eee | 1135 | !is_floppy && |
6b88a410 PR |
1136 | blkid_probe_is_wholedisk(pr)) { |
1137 | ||
ed1f6f10 KZ |
1138 | long last_written = 0; |
1139 | ||
3d725afa | 1140 | /* |
6b88a410 PR |
1141 | * pktcdvd.ko accepts only these ioctls: |
1142 | * CDROMEJECT CDROMMULTISESSION CDROMREADTOCENTRY | |
1143 | * CDROM_LAST_WRITTEN CDROM_SEND_PACKET SCSI_IOCTL_SEND_COMMAND | |
1144 | * So CDROM_GET_CAPABILITY cannot be used for detecting pktcdvd | |
1145 | * devices. But CDROM_GET_CAPABILITY and CDROM_DRIVE_STATUS are | |
1146 | * fast so use them for detecting if medium is present. In any | |
1147 | * case use last written block form CDROM_LAST_WRITTEN. | |
1148 | */ | |
bfebe74e | 1149 | |
6b88a410 | 1150 | if (ioctl(fd, CDROM_GET_CAPABILITY, NULL) >= 0) { |
dc30fd43 | 1151 | # ifdef CDROM_DRIVE_STATUS |
6b88a410 PR |
1152 | switch (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT)) { |
1153 | case CDS_TRAY_OPEN: | |
1154 | case CDS_NO_DISC: | |
1155 | errno = ENOMEDIUM; | |
1156 | goto err; | |
1157 | } | |
1158 | # endif | |
1159 | pr->flags |= BLKID_FL_CDROM_DEV; | |
dc30fd43 | 1160 | } |
6b88a410 PR |
1161 | |
1162 | # ifdef CDROM_LAST_WRITTEN | |
c80b3f32 | 1163 | if (ioctl(fd, CDROM_LAST_WRITTEN, &last_written) == 0) { |
6b88a410 | 1164 | pr->flags |= BLKID_FL_CDROM_DEV; |
c80b3f32 JP |
1165 | } else { |
1166 | if (errno == ENOMEDIUM) | |
1167 | goto err; | |
1168 | } | |
dc30fd43 | 1169 | # endif |
6b88a410 PR |
1170 | |
1171 | if (pr->flags & BLKID_FL_CDROM_DEV) { | |
bfd4da56 | 1172 | cdrom_size_correction(pr, last_written); |
427ea355 PR |
1173 | |
1174 | # ifdef CDROMMULTISESSION | |
1175 | if (!pr->off && blkid_probe_get_hint(pr, "session_offset", NULL) < 0) { | |
1176 | struct cdrom_multisession multisession = { .addr_format = CDROM_LBA }; | |
1177 | if (ioctl(fd, CDROMMULTISESSION, &multisession) == 0 && multisession.xa_flag) | |
1178 | blkid_probe_set_hint(pr, "session_offset", (multisession.addr.lba << 11)); | |
1179 | } | |
1180 | # endif | |
6b88a410 | 1181 | } |
bfebe74e | 1182 | } |
55113b15 | 1183 | #endif |
884659b3 | 1184 | free(dm_uuid); |
508e438b | 1185 | |
60d1f1a3 | 1186 | # ifdef BLKGETZONESZ |
f5ea9eee | 1187 | if (S_ISBLK(sb.st_mode) && !is_floppy) { |
b250fbbf NA |
1188 | uint32_t zone_size_sector; |
1189 | ||
1190 | if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector)) | |
1191 | pr->zone_size = zone_size_sector << 9; | |
1192 | } | |
1193 | # endif | |
1194 | ||
224f2998 | 1195 | #ifdef BLKIOOPT |
8a534253 TW |
1196 | if (S_ISBLK(sb.st_mode) && !is_floppy && !blkid_probe_is_tiny(pr)) |
1197 | pr->io_size = blkid_get_io_size(fd); | |
224f2998 | 1198 | #endif |
8a534253 TW |
1199 | |
1200 | DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64", zonesize=%"PRIu64", iosize=%"PRIu64, | |
1201 | pr->off, pr->size, pr->zone_size, pr->io_size)); | |
c62a6311 | 1202 | DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s", |
508e438b KZ |
1203 | blkid_probe_is_wholedisk(pr) ?"YES" : "NO", |
1204 | S_ISREG(pr->mode) ? "YES" : "NO")); | |
1205 | ||
a0948ffe | 1206 | return 0; |
0d17b1cf | 1207 | err: |
c62a6311 | 1208 | DBG(LOWPROBE, ul_debug("failed to prepare a device for low-probing")); |
0d17b1cf KZ |
1209 | return -1; |
1210 | ||
a0948ffe KZ |
1211 | } |
1212 | ||
f12cd8d1 | 1213 | int blkid_probe_get_dimension(blkid_probe pr, uint64_t *off, uint64_t *size) |
f319b5ca | 1214 | { |
f319b5ca KZ |
1215 | *off = pr->off; |
1216 | *size = pr->size; | |
1217 | return 0; | |
1218 | } | |
1219 | ||
f12cd8d1 | 1220 | int blkid_probe_set_dimension(blkid_probe pr, uint64_t off, uint64_t size) |
f319b5ca | 1221 | { |
c62a6311 | 1222 | DBG(LOWPROBE, ul_debug( |
63c9c05d | 1223 | "changing probing area: size=%"PRIu64", off=%"PRIu64" " |
fdbd7bb9 | 1224 | "-to-> size=%"PRIu64", off=%"PRIu64"", |
63c9c05d | 1225 | pr->size, pr->off, size, off)); |
f319b5ca KZ |
1226 | |
1227 | pr->off = off; | |
1228 | pr->size = size; | |
a9eef56c | 1229 | pr->flags &= ~BLKID_FL_TINY_DEV; |
88923b08 | 1230 | |
f12cd8d1 | 1231 | if (pr->size <= 1440ULL * 1024ULL && !S_ISCHR(pr->mode)) |
a9eef56c | 1232 | pr->flags |= BLKID_FL_TINY_DEV; |
f319b5ca | 1233 | |
d2b0c658 | 1234 | blkid_probe_reset_buffers(pr); |
f319b5ca KZ |
1235 | |
1236 | return 0; | |
1237 | } | |
1238 | ||
7eba8f98 | 1239 | const unsigned char *blkid_probe_get_sb_buffer(blkid_probe pr, const struct blkid_idmag *mag, size_t size) |
b5c5b420 | 1240 | { |
2816a817 | 1241 | uint64_t hint_offset, off; |
b5c5b420 | 1242 | |
2816a817 TW |
1243 | if (mag->kboff >= 0) { |
1244 | if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0) | |
1245 | hint_offset = 0; | |
b5c5b420 | 1246 | |
2816a817 TW |
1247 | off = hint_offset + (mag->kboff << 10); |
1248 | } else { | |
1249 | off = pr->size - (-mag->kboff << 10); | |
1250 | } | |
1251 | ||
1252 | return blkid_probe_get_buffer(pr, off, size); | |
b5c5b420 PR |
1253 | } |
1254 | ||
0928e6ed TW |
1255 | uint64_t blkid_probe_get_idmag_off(blkid_probe pr, const struct blkid_idmag *mag) |
1256 | { | |
1257 | if (mag->kboff >= 0) | |
1258 | return mag->kboff << 10; | |
1259 | else | |
1260 | return pr->size - (-mag->kboff << 10); | |
1261 | } | |
1262 | ||
8d0ce083 | 1263 | /* |
296d96e2 HR |
1264 | * Check for matching magic value. |
1265 | * Returns BLKID_PROBE_OK if found, BLKID_PROBE_NONE if not found | |
1266 | * or no magic present, or negative value on error. | |
1267 | */ | |
c76e710b | 1268 | int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id, |
f12cd8d1 | 1269 | uint64_t *offset, const struct blkid_idmag **res) |
c76e710b KZ |
1270 | { |
1271 | const struct blkid_idmag *mag = NULL; | |
f12cd8d1 | 1272 | uint64_t off = 0; |
c76e710b KZ |
1273 | |
1274 | if (id) | |
c03d12d8 | 1275 | mag = &id->magics[0]; |
c76e710b KZ |
1276 | if (res) |
1277 | *res = NULL; | |
1278 | ||
1279 | /* try to detect by magic string */ | |
1280 | while(mag && mag->magic) { | |
7eba8f98 | 1281 | const unsigned char *buf; |
2816a817 | 1282 | long kboff; |
3e31657d | 1283 | uint64_t hint_offset; |
c76e710b | 1284 | |
3e31657d PR |
1285 | if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0) |
1286 | hint_offset = 0; | |
c76e710b | 1287 | |
b250fbbf NA |
1288 | /* If the magic is for zoned device, skip non-zoned device */ |
1289 | if (mag->is_zoned && !pr->zone_size) { | |
1290 | mag++; | |
1291 | continue; | |
1292 | } | |
1293 | ||
1294 | if (!mag->is_zoned) | |
1295 | kboff = mag->kboff; | |
1296 | else | |
1297 | kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone; | |
1298 | ||
2816a817 | 1299 | if (kboff >= 0) |
51f9e093 | 1300 | off = hint_offset + (kboff << 10) + mag->sboff; |
2816a817 | 1301 | else |
51f9e093 TW |
1302 | off = pr->size - (-kboff << 10) + mag->sboff; |
1303 | buf = blkid_probe_get_buffer(pr, off, mag->len); | |
c76e710b | 1304 | |
296d96e2 HR |
1305 | if (!buf && errno) |
1306 | return -errno; | |
a674a0ab | 1307 | |
51f9e093 | 1308 | if (buf && !memcmp(mag->magic, buf, mag->len)) { |
2816a817 | 1309 | DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld", |
b250fbbf | 1310 | mag->sboff, kboff)); |
c76e710b | 1311 | if (offset) |
51f9e093 | 1312 | *offset = off; |
c76e710b KZ |
1313 | if (res) |
1314 | *res = mag; | |
296d96e2 | 1315 | return BLKID_PROBE_OK; |
c76e710b KZ |
1316 | } |
1317 | mag++; | |
1318 | } | |
1319 | ||
c03d12d8 | 1320 | if (id && id->magics[0].magic) |
c76e710b | 1321 | /* magic string(s) defined, but not found */ |
296d96e2 | 1322 | return BLKID_PROBE_NONE; |
c76e710b | 1323 | |
296d96e2 | 1324 | return BLKID_PROBE_OK; |
c76e710b KZ |
1325 | } |
1326 | ||
c81e7008 KZ |
1327 | static inline void blkid_probe_start(blkid_probe pr) |
1328 | { | |
63c9c05d | 1329 | DBG(LOWPROBE, ul_debug("start probe")); |
7f787ced KZ |
1330 | pr->cur_chain = NULL; |
1331 | pr->prob_flags = 0; | |
1332 | blkid_probe_set_wiper(pr, 0, 0); | |
c81e7008 KZ |
1333 | } |
1334 | ||
1335 | static inline void blkid_probe_end(blkid_probe pr) | |
1336 | { | |
63c9c05d | 1337 | DBG(LOWPROBE, ul_debug("end probe")); |
7f787ced KZ |
1338 | pr->cur_chain = NULL; |
1339 | pr->prob_flags = 0; | |
1340 | blkid_probe_set_wiper(pr, 0, 0); | |
c81e7008 KZ |
1341 | } |
1342 | ||
0bffad47 KZ |
1343 | /** |
1344 | * blkid_do_probe: | |
1345 | * @pr: prober | |
1346 | * | |
1347 | * Calls probing functions in all enabled chains. The superblocks chain is | |
1348 | * enabled by default. The blkid_do_probe() stores result from only one | |
1349 | * probing function. It's necessary to call this routine in a loop to get | |
3b159691 | 1350 | * results from all probing functions in all chains. The probing is reset |
44ef90bc | 1351 | * by blkid_reset_probe() or by filter functions. |
a0fc685c | 1352 | * |
0bffad47 KZ |
1353 | * This is string-based NAME=value interface only. |
1354 | * | |
1355 | * <example> | |
1356 | * <title>basic case - use the first result only</title> | |
1357 | * <programlisting> | |
2bd739c9 | 1358 | * if (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
a0fc685c KZ |
1359 | * int nvals = blkid_probe_numof_values(pr); |
1360 | * for (n = 0; n < nvals; n++) { | |
1361 | * if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0) | |
1362 | * printf("%s = %s\n", name, data); | |
1363 | * } | |
1364 | * } | |
0bffad47 KZ |
1365 | * </programlisting> |
1366 | * </example> | |
a0fc685c | 1367 | * |
0bffad47 KZ |
1368 | * <example> |
1369 | * <title>advanced case - probe for all signatures</title> | |
1370 | * <programlisting> | |
2bd739c9 | 1371 | * while (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
a0fc685c KZ |
1372 | * int nvals = blkid_probe_numof_values(pr); |
1373 | * ... | |
1374 | * } | |
0bffad47 KZ |
1375 | * </programlisting> |
1376 | * </example> | |
a0fc685c | 1377 | * |
0bffad47 | 1378 | * See also blkid_reset_probe(). |
a0fc685c | 1379 | * |
0bffad47 | 1380 | * Returns: 0 on success, 1 when probing is done and -1 in case of error. |
a0fc685c | 1381 | */ |
51410fc6 | 1382 | int blkid_do_probe(blkid_probe pr) |
a0948ffe | 1383 | { |
0bffad47 | 1384 | int rc = 1; |
a0948ffe | 1385 | |
20e1c3dc | 1386 | if (pr->flags & BLKID_FL_NOSCAN_DEV) |
2bd739c9 | 1387 | return BLKID_PROBE_NONE; |
20e1c3dc | 1388 | |
0bffad47 | 1389 | do { |
44ef90bc KZ |
1390 | struct blkid_chain *chn = pr->cur_chain; |
1391 | ||
c81e7008 KZ |
1392 | if (!chn) { |
1393 | blkid_probe_start(pr); | |
44ef90bc | 1394 | chn = pr->cur_chain = &pr->chains[0]; |
c81e7008 | 1395 | } |
44ef90bc KZ |
1396 | /* we go to the next chain only when the previous probing |
1397 | * result was nothing (rc == 1) and when the current chain is | |
1398 | * disabled or we are at end of the current chain (chain->idx + | |
046959cc CW |
1399 | * 1 == sizeof chain) or the current chain bailed out right at |
1400 | * the start (chain->idx == -1) | |
44ef90bc KZ |
1401 | */ |
1402 | else if (rc == 1 && (chn->enabled == FALSE || | |
c9f51c71 | 1403 | chn->idx + 1 == (int) chn->driver->nidinfos || |
046959cc | 1404 | chn->idx == -1)) { |
a0948ffe | 1405 | |
c9f51c71 | 1406 | size_t idx = chn->driver->id + 1; |
bd635f86 KZ |
1407 | |
1408 | if (idx < BLKID_NCHAINS) | |
44ef90bc | 1409 | chn = pr->cur_chain = &pr->chains[idx]; |
c81e7008 KZ |
1410 | else { |
1411 | blkid_probe_end(pr); | |
2bd739c9 | 1412 | return BLKID_PROBE_NONE; /* all chains already probed */ |
c81e7008 | 1413 | } |
bd635f86 | 1414 | } |
a0fc685c | 1415 | |
0bffad47 | 1416 | chn->binary = FALSE; /* for sure... */ |
6644688a | 1417 | |
c62a6311 | 1418 | DBG(LOWPROBE, ul_debug("chain probe %s %s (idx=%d)", |
0bffad47 | 1419 | chn->driver->name, |
44ef90bc KZ |
1420 | chn->enabled? "ENABLED" : "DISABLED", |
1421 | chn->idx)); | |
a0948ffe | 1422 | |
0bffad47 | 1423 | if (!chn->enabled) |
51410fc6 | 1424 | continue; |
a0948ffe | 1425 | |
0bffad47 KZ |
1426 | /* rc: -1 = error, 0 = success, 1 = no result */ |
1427 | rc = chn->driver->probe(pr, chn); | |
a0948ffe | 1428 | |
2bd739c9 KZ |
1429 | } while (rc == BLKID_PROBE_NONE); |
1430 | ||
1431 | if (rc < 0) | |
1432 | return BLKID_PROBE_ERROR; | |
a0948ffe | 1433 | |
0bffad47 KZ |
1434 | return rc; |
1435 | } | |
a0948ffe | 1436 | |
db221158 NA |
1437 | #ifdef HAVE_LINUX_BLKZONED_H |
1438 | static int is_conventional(blkid_probe pr, uint64_t offset) | |
1439 | { | |
1440 | struct blk_zone_report *rep = NULL; | |
1441 | int ret; | |
1442 | uint64_t zone_mask; | |
1443 | ||
1444 | if (!pr->zone_size) | |
1445 | return 1; | |
1446 | ||
1447 | zone_mask = ~(pr->zone_size - 1); | |
1448 | rep = blkdev_get_zonereport(blkid_probe_get_fd(pr), | |
1449 | (offset & zone_mask) >> 9, 1); | |
1450 | if (!rep) | |
1451 | return -1; | |
1452 | ||
1453 | if (rep->zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) | |
1454 | ret = 1; | |
1455 | else | |
1456 | ret = 0; | |
1457 | ||
1458 | free(rep); | |
1459 | ||
1460 | return ret; | |
1461 | } | |
1462 | #else | |
1463 | static inline int is_conventional(blkid_probe pr __attribute__((__unused__)), | |
1464 | uint64_t offset __attribute__((__unused__))) | |
1465 | { | |
1466 | return 1; | |
1467 | } | |
1468 | #endif | |
1469 | ||
2b89be6c KZ |
1470 | /** |
1471 | * blkid_do_wipe: | |
1472 | * @pr: prober | |
1473 | * @dryrun: if TRUE then don't touch the device. | |
1474 | * | |
1475 | * This function erases the current signature detected by @pr. The @pr has to | |
476b508e | 1476 | * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags |
9e930041 | 1477 | * has to be enabled (if you want to erase also superblock with broken check |
c93c2030 | 1478 | * sums then use BLKID_SUBLKS_BADCSUM too). |
2b89be6c KZ |
1479 | * |
1480 | * After successful signature removing the @pr prober will be moved one step | |
1481 | * back and the next blkid_do_probe() call will again call previously called | |
d2b0c658 | 1482 | * probing function. All in-memory cached data from the device are always |
73afd3f8 | 1483 | * reset. |
2b89be6c KZ |
1484 | * |
1485 | * <example> | |
1486 | * <title>wipe all filesystems or raids from the device</title> | |
1487 | * <programlisting> | |
49a8f58e | 1488 | * fd = open(devname, O_RDWR|O_CLOEXEC); |
2b89be6c KZ |
1489 | * blkid_probe_set_device(pr, fd, 0, 0); |
1490 | * | |
1491 | * blkid_probe_enable_superblocks(pr, 1); | |
1492 | * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC); | |
1493 | * | |
1494 | * while (blkid_do_probe(pr) == 0) | |
1495 | * blkid_do_wipe(pr, FALSE); | |
1496 | * </programlisting> | |
1497 | * </example> | |
1498 | * | |
11026083 | 1499 | * See also blkid_probe_step_back() if you cannot use this built-in wipe |
cd0fe5c1 KZ |
1500 | * function, but you want to use libblkid probing as a source for wiping. |
1501 | * | |
0c92c65a TW |
1502 | * See also blkid_wipe_all() which works the same as the example above. |
1503 | * | |
cd0fe5c1 | 1504 | * Returns: 0 on success, and -1 in case of error. |
2b89be6c KZ |
1505 | */ |
1506 | int blkid_do_wipe(blkid_probe pr, int dryrun) | |
1507 | { | |
44765fdd | 1508 | const char *off = NULL; |
2b89be6c | 1509 | size_t len = 0; |
a732e4a1 | 1510 | uint64_t offset, magoff; |
db221158 | 1511 | int conventional; |
2b89be6c | 1512 | char buf[BUFSIZ]; |
f8054232 | 1513 | int fd, rc = 0; |
2b89be6c KZ |
1514 | struct blkid_chain *chn; |
1515 | ||
2b89be6c KZ |
1516 | chn = pr->cur_chain; |
1517 | if (!chn) | |
2bd739c9 | 1518 | return BLKID_PROBE_ERROR; |
2b89be6c | 1519 | |
44765fdd KZ |
1520 | switch (chn->driver->id) { |
1521 | case BLKID_CHAIN_SUBLKS: | |
1522 | rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL); | |
1523 | if (!rc) | |
1524 | rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len); | |
1525 | break; | |
1526 | case BLKID_CHAIN_PARTS: | |
1527 | rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL); | |
1528 | if (!rc) | |
1529 | rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len); | |
1530 | break; | |
1531 | default: | |
2bd739c9 | 1532 | return BLKID_PROBE_OK; |
44765fdd KZ |
1533 | } |
1534 | ||
1535 | if (rc || len == 0 || off == NULL) | |
2bd739c9 | 1536 | return BLKID_PROBE_OK; |
2b89be6c | 1537 | |
0395e8f7 | 1538 | errno = 0; |
d2b0c658 | 1539 | magoff = strtoumax(off, NULL, 10); |
0395e8f7 | 1540 | if (errno) |
2bd739c9 | 1541 | return BLKID_PROBE_OK; |
0395e8f7 | 1542 | |
d2b0c658 | 1543 | offset = magoff + pr->off; |
2b89be6c KZ |
1544 | fd = blkid_probe_get_fd(pr); |
1545 | if (fd < 0) | |
2bd739c9 | 1546 | return BLKID_PROBE_ERROR; |
2b89be6c KZ |
1547 | |
1548 | if (len > sizeof(buf)) | |
1549 | len = sizeof(buf); | |
1550 | ||
db221158 NA |
1551 | rc = is_conventional(pr, offset); |
1552 | if (rc < 0) | |
2bd739c9 | 1553 | return BLKID_PROBE_ERROR; |
db221158 NA |
1554 | conventional = rc == 1; |
1555 | ||
c62a6311 | 1556 | DBG(LOWPROBE, ul_debug( |
fdbd7bb9 | 1557 | "do_wipe [offset=0x%"PRIx64" (%"PRIu64"), len=%zu, chain=%s, idx=%d, dryrun=%s]\n", |
f12cd8d1 | 1558 | offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not")); |
2b89be6c | 1559 | |
a732e4a1 | 1560 | if (lseek(fd, offset, SEEK_SET) == (off_t) -1) |
2bd739c9 | 1561 | return BLKID_PROBE_ERROR; |
2b89be6c | 1562 | |
2b89be6c | 1563 | if (!dryrun && len) { |
db221158 NA |
1564 | if (conventional) { |
1565 | memset(buf, 0, len); | |
1566 | ||
1567 | /* wipen on device */ | |
1568 | if (write_all(fd, buf, len)) | |
2bd739c9 | 1569 | return BLKID_PROBE_ERROR; |
133a0d70 | 1570 | if (fsync(fd) != 0) |
2bd739c9 | 1571 | return BLKID_PROBE_ERROR; |
db221158 NA |
1572 | } else { |
1573 | #ifdef HAVE_LINUX_BLKZONED_H | |
1574 | uint64_t zone_mask = ~(pr->zone_size - 1); | |
1575 | struct blk_zone_range range = { | |
1576 | .sector = (offset & zone_mask) >> 9, | |
1577 | .nr_sectors = pr->zone_size >> 9, | |
1578 | }; | |
1579 | ||
1580 | rc = ioctl(fd, BLKRESETZONE, &range); | |
1581 | if (rc < 0) | |
2bd739c9 | 1582 | return BLKID_PROBE_ERROR; |
db221158 NA |
1583 | #else |
1584 | /* Should not reach here */ | |
1585 | assert(0); | |
1586 | #endif | |
1587 | } | |
1588 | ||
d2b0c658 KZ |
1589 | pr->flags &= ~BLKID_FL_MODIF_BUFF; /* be paranoid */ |
1590 | ||
1591 | return blkid_probe_step_back(pr); | |
1592 | ||
042f62df RP |
1593 | } |
1594 | ||
1595 | if (dryrun) { | |
d2b0c658 KZ |
1596 | /* wipe in memory only */ |
1597 | blkid_probe_hide_range(pr, magoff, len); | |
cd0fe5c1 KZ |
1598 | return blkid_probe_step_back(pr); |
1599 | } | |
2b89be6c | 1600 | |
2bd739c9 | 1601 | return BLKID_PROBE_OK; |
cd0fe5c1 | 1602 | } |
2b89be6c | 1603 | |
0c92c65a TW |
1604 | /** |
1605 | * blkid_wipe_all: | |
1606 | * @pr: prober | |
1607 | * | |
1608 | * This function erases all detectable signatures from &pr. | |
1609 | * The @pr has to be open in O_RDWR mode. All other necessary configurations | |
1610 | * will be enabled automatically. | |
1611 | * | |
1612 | * <example> | |
1613 | * <title>wipe all filesystems or raids from the device</title> | |
1614 | * <programlisting> | |
1615 | * fd = open(devname, O_RDWR|O_CLOEXEC); | |
1616 | * blkid_probe_set_device(pr, fd, 0, 0); | |
1617 | * | |
1618 | * blkid_wipe_all(pr); | |
1619 | * </programlisting> | |
1620 | * </example> | |
1621 | * | |
1622 | * Returns: 0 on success, and -1 in case of error. | |
1623 | */ | |
1624 | int blkid_wipe_all(blkid_probe pr) | |
1625 | { | |
1626 | DBG(LOWPROBE, ul_debug("wiping all signatures")); | |
1627 | ||
1628 | blkid_probe_enable_superblocks(pr, 1); | |
1629 | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | | |
1630 | BLKID_SUBLKS_BADCSUM); | |
1631 | ||
1632 | blkid_probe_enable_partitions(pr, 1); | |
1633 | blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC | | |
1634 | BLKID_PARTS_FORCE_GPT); | |
1635 | ||
1636 | while (blkid_do_probe(pr) == 0) { | |
1637 | DBG(LOWPROBE, ul_debug("wiping one signature")); | |
1638 | blkid_do_wipe(pr, 0); | |
1639 | } | |
1640 | ||
1641 | return BLKID_PROBE_OK; | |
1642 | } | |
1643 | ||
cd0fe5c1 | 1644 | /** |
c0055e2a | 1645 | * blkid_probe_step_back: |
cd0fe5c1 KZ |
1646 | * @pr: prober |
1647 | * | |
1648 | * This function move pointer to the probing chain one step back -- it means | |
1649 | * that the previously used probing function will be called again in the next | |
1650 | * blkid_do_probe() call. | |
1651 | * | |
1652 | * This is necessary for example if you erase or modify on-disk superblock | |
1653 | * according to the current libblkid probing result. | |
1654 | * | |
d2b0c658 | 1655 | * Note that blkid_probe_hide_range() changes semantic of this function and |
311e33af | 1656 | * cached buffers are not reset, but library uses in-memory modified |
d2b0c658 KZ |
1657 | * buffers to call the next probing function. |
1658 | * | |
cd0fe5c1 KZ |
1659 | * <example> |
1660 | * <title>wipe all superblock, but use libblkid only for probing</title> | |
1661 | * <programlisting> | |
1662 | * pr = blkid_new_probe_from_filename(devname); | |
1663 | * | |
1664 | * blkid_probe_enable_superblocks(pr, 1); | |
1665 | * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC); | |
1666 | * | |
2bb7a706 KZ |
1667 | * blkid_probe_enable_partitions(pr, 1); |
1668 | * blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC); | |
1669 | * | |
2bd739c9 | 1670 | * while (blkid_do_probe(pr) == BLKID_PROBE_OK) { |
cd0fe5c1 KZ |
1671 | * const char *ostr = NULL; |
1672 | * size_t len = 0; | |
1673 | * | |
1674 | * // superblocks | |
1675 | * if (blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &ostr, NULL) == 0) | |
1676 | * blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len); | |
1677 | * | |
1678 | * // partition tables | |
1679 | * if (len == 0 && blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &ostr, NULL) == 0) | |
1680 | * blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len); | |
1681 | * | |
1682 | * if (!len || !str) | |
1683 | * continue; | |
1684 | * | |
1685 | * // convert ostr to the real offset by off = strtoll(ostr, NULL, 10); | |
9e930041 | 1686 | * // use your stuff to erase @len bytes at the @off |
cd0fe5c1 KZ |
1687 | * .... |
1688 | * | |
1689 | * // retry the last probing to check for backup superblocks ..etc. | |
1690 | * blkid_probe_step_back(pr); | |
1691 | * } | |
1692 | * </programlisting> | |
1693 | * </example> | |
1694 | * | |
1695 | * Returns: 0 on success, and -1 in case of error. | |
1696 | */ | |
1697 | int blkid_probe_step_back(blkid_probe pr) | |
1698 | { | |
1699 | struct blkid_chain *chn; | |
1700 | ||
cd0fe5c1 KZ |
1701 | chn = pr->cur_chain; |
1702 | if (!chn) | |
1703 | return -1; | |
1704 | ||
d2b0c658 KZ |
1705 | if (!(pr->flags & BLKID_FL_MODIF_BUFF)) |
1706 | blkid_probe_reset_buffers(pr); | |
cd0fe5c1 KZ |
1707 | |
1708 | if (chn->idx >= 0) { | |
1709 | chn->idx--; | |
c62a6311 | 1710 | DBG(LOWPROBE, ul_debug("step back: moving %s chain index to %d", |
cd0fe5c1 KZ |
1711 | chn->driver->name, |
1712 | chn->idx)); | |
2b89be6c | 1713 | } |
cd0fe5c1 KZ |
1714 | |
1715 | if (chn->idx == -1) { | |
1716 | /* blkid_do_probe() goes to the next chain if the index | |
1717 | * of the current chain is -1, so we have to set the | |
1718 | * chain pointer to the previous chain. | |
1719 | */ | |
1720 | size_t idx = chn->driver->id > 0 ? chn->driver->id - 1 : 0; | |
1721 | ||
c62a6311 | 1722 | DBG(LOWPROBE, ul_debug("step back: moving to previous chain")); |
cd0fe5c1 KZ |
1723 | |
1724 | if (idx > 0) | |
1725 | pr->cur_chain = &pr->chains[idx]; | |
1726 | else if (idx == 0) | |
1727 | pr->cur_chain = NULL; | |
1728 | } | |
1729 | ||
2b89be6c KZ |
1730 | return 0; |
1731 | } | |
1732 | ||
0bffad47 KZ |
1733 | /** |
1734 | * blkid_do_safeprobe: | |
1735 | * @pr: prober | |
1736 | * | |
1737 | * This function gathers probing results from all enabled chains and checks | |
1738 | * for ambivalent results (e.g. more filesystems on the device). | |
1739 | * | |
1740 | * This is string-based NAME=value interface only. | |
1741 | * | |
9e930041 | 1742 | * Note about superblocks chain -- the function does not check for filesystems |
0bffad47 | 1743 | * when a RAID signature is detected. The function also does not check for |
c81e7008 KZ |
1744 | * collision between RAIDs. The first detected RAID is returned. The function |
1745 | * checks for collision between partition table and RAID signature -- it's | |
1746 | * recommended to enable partitions chain together with superblocks chain. | |
0bffad47 | 1747 | * |
9e930041 | 1748 | * Returns: 0 on success, 1 if nothing is detected, -2 if ambivalent result is |
0bffad47 KZ |
1749 | * detected and -1 on case of error. |
1750 | */ | |
1751 | int blkid_do_safeprobe(blkid_probe pr) | |
1752 | { | |
1753 | int i, count = 0, rc = 0; | |
a0948ffe | 1754 | |
20e1c3dc | 1755 | if (pr->flags & BLKID_FL_NOSCAN_DEV) |
2bd739c9 | 1756 | return BLKID_PROBE_NONE; |
a0948ffe | 1757 | |
c81e7008 KZ |
1758 | blkid_probe_start(pr); |
1759 | ||
0bffad47 KZ |
1760 | for (i = 0; i < BLKID_NCHAINS; i++) { |
1761 | struct blkid_chain *chn; | |
a0948ffe | 1762 | |
0bffad47 KZ |
1763 | chn = pr->cur_chain = &pr->chains[i]; |
1764 | chn->binary = FALSE; /* for sure... */ | |
a0948ffe | 1765 | |
c62a6311 | 1766 | DBG(LOWPROBE, ul_debug("chain safeprobe %s %s", |
0bffad47 KZ |
1767 | chn->driver->name, |
1768 | chn->enabled? "ENABLED" : "DISABLED")); | |
1769 | ||
1770 | if (!chn->enabled) | |
1771 | continue; | |
1772 | ||
9e0f7bda | 1773 | blkid_probe_chain_reset_position(chn); |
0bffad47 | 1774 | |
0bffad47 | 1775 | rc = chn->driver->safeprobe(pr, chn); |
9e0f7bda KZ |
1776 | |
1777 | blkid_probe_chain_reset_position(chn); | |
1778 | ||
1779 | /* rc: -2 ambivalent, -1 = error, 0 = success, 1 = no result */ | |
0bffad47 KZ |
1780 | if (rc < 0) |
1781 | goto done; /* error */ | |
1782 | if (rc == 0) | |
1783 | count++; /* success */ | |
51410fc6 | 1784 | } |
0bffad47 KZ |
1785 | |
1786 | done: | |
c81e7008 | 1787 | blkid_probe_end(pr); |
0bffad47 | 1788 | if (rc < 0) |
2bd739c9 KZ |
1789 | return BLKID_PROBE_ERROR; |
1790 | ||
1791 | return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK; | |
a0948ffe KZ |
1792 | } |
1793 | ||
0bffad47 KZ |
1794 | /** |
1795 | * blkid_do_fullprobe: | |
1796 | * @pr: prober | |
1797 | * | |
1798 | * This function gathers probing results from all enabled chains. Same as | |
fd7c9e35 | 1799 | * blkid_do_safeprobe() but does not check for collision between probing |
0bffad47 KZ |
1800 | * result. |
1801 | * | |
1802 | * This is string-based NAME=value interface only. | |
7103157c | 1803 | * |
0bffad47 | 1804 | * Returns: 0 on success, 1 if nothing is detected or -1 on case of error. |
a2f01a1c | 1805 | */ |
0bffad47 | 1806 | int blkid_do_fullprobe(blkid_probe pr) |
a2f01a1c | 1807 | { |
0bffad47 | 1808 | int i, count = 0, rc = 0; |
7103157c | 1809 | |
20e1c3dc | 1810 | if (pr->flags & BLKID_FL_NOSCAN_DEV) |
2bd739c9 | 1811 | return BLKID_PROBE_NONE; |
a2f01a1c | 1812 | |
c81e7008 KZ |
1813 | blkid_probe_start(pr); |
1814 | ||
0bffad47 | 1815 | for (i = 0; i < BLKID_NCHAINS; i++) { |
0bffad47 | 1816 | struct blkid_chain *chn; |
a2f01a1c | 1817 | |
0bffad47 KZ |
1818 | chn = pr->cur_chain = &pr->chains[i]; |
1819 | chn->binary = FALSE; /* for sure... */ | |
1820 | ||
c62a6311 | 1821 | DBG(LOWPROBE, ul_debug("chain fullprobe %s: %s", |
0bffad47 KZ |
1822 | chn->driver->name, |
1823 | chn->enabled? "ENABLED" : "DISABLED")); | |
1824 | ||
1825 | if (!chn->enabled) | |
1826 | continue; | |
1827 | ||
9e0f7bda | 1828 | blkid_probe_chain_reset_position(chn); |
0bffad47 | 1829 | |
0bffad47 | 1830 | rc = chn->driver->probe(pr, chn); |
9e0f7bda KZ |
1831 | |
1832 | blkid_probe_chain_reset_position(chn); | |
1833 | ||
1834 | /* rc: -1 = error, 0 = success, 1 = no result */ | |
0bffad47 KZ |
1835 | if (rc < 0) |
1836 | goto done; /* error */ | |
1837 | if (rc == 0) | |
1838 | count++; /* success */ | |
1839 | } | |
1840 | ||
1841 | done: | |
c81e7008 | 1842 | blkid_probe_end(pr); |
0bffad47 | 1843 | if (rc < 0) |
2bd739c9 KZ |
1844 | return BLKID_PROBE_ERROR; |
1845 | ||
1846 | return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK; | |
a2f01a1c KZ |
1847 | } |
1848 | ||
ce011388 | 1849 | /* same sa blkid_probe_get_buffer() but works with 512-sectors */ |
7eba8f98 | 1850 | const unsigned char *blkid_probe_get_sector(blkid_probe pr, unsigned int sector) |
ce011388 | 1851 | { |
7f787ced | 1852 | return blkid_probe_get_buffer(pr, ((uint64_t) sector) << 9, 0x200); |
ce011388 KZ |
1853 | } |
1854 | ||
7f787ced | 1855 | struct blkid_prval *blkid_probe_assign_value(blkid_probe pr, const char *name) |
a0948ffe | 1856 | { |
51410fc6 | 1857 | struct blkid_prval *v; |
6c4a7811 | 1858 | |
08029093 | 1859 | v = calloc(1, sizeof(struct blkid_prval)); |
6c4a7811 | 1860 | if (!v) |
51410fc6 | 1861 | return NULL; |
a0948ffe | 1862 | |
08029093 | 1863 | INIT_LIST_HEAD(&v->prvals); |
51410fc6 | 1864 | v->name = name; |
9bdf6885 | 1865 | v->chain = pr->cur_chain; |
af17d349 | 1866 | list_add_tail(&v->prvals, &pr->values); |
6644688a | 1867 | |
c62a6311 | 1868 | DBG(LOWPROBE, ul_debug("assigning %s [%s]", name, v->chain->driver->name)); |
51410fc6 | 1869 | return v; |
a0948ffe KZ |
1870 | } |
1871 | ||
6c4a7811 | 1872 | /* Note that value data is always terminated by zero to keep things robust, |
3fd1f771 RM |
1873 | * this extra zero is not count to the value length. It's caller responsibility |
1874 | * to set proper value length (for strings we count terminator to the length, | |
6c4a7811 OO |
1875 | * for binary data it's without terminator). |
1876 | */ | |
1877 | int blkid_probe_value_set_data(struct blkid_prval *v, | |
47afae0c | 1878 | const unsigned char *data, size_t len) |
6c4a7811 OO |
1879 | { |
1880 | v->data = calloc(1, len + 1); /* always terminate by \0 */ | |
cdd5bada | 1881 | |
6c4a7811 OO |
1882 | if (!v->data) |
1883 | return -ENOMEM; | |
1884 | memcpy(v->data, data, len); | |
1885 | v->len = len; | |
cdd5bada | 1886 | return 0; |
cdd5bada KZ |
1887 | } |
1888 | ||
51410fc6 | 1889 | int blkid_probe_set_value(blkid_probe pr, const char *name, |
47afae0c | 1890 | const unsigned char *data, size_t len) |
a0948ffe | 1891 | { |
51410fc6 | 1892 | struct blkid_prval *v; |
a0948ffe | 1893 | |
51410fc6 KZ |
1894 | v = blkid_probe_assign_value(pr, name); |
1895 | if (!v) | |
1896 | return -1; | |
a0948ffe | 1897 | |
6c4a7811 | 1898 | return blkid_probe_value_set_data(v, data, len); |
a0948ffe KZ |
1899 | } |
1900 | ||
51410fc6 KZ |
1901 | int blkid_probe_vsprintf_value(blkid_probe pr, const char *name, |
1902 | const char *fmt, va_list ap) | |
a0948ffe | 1903 | { |
51410fc6 | 1904 | struct blkid_prval *v; |
a23facd6 | 1905 | ssize_t len; |
a0948ffe | 1906 | |
51410fc6 KZ |
1907 | v = blkid_probe_assign_value(pr, name); |
1908 | if (!v) | |
6c4a7811 | 1909 | return -ENOMEM; |
a0948ffe | 1910 | |
6c4a7811 | 1911 | len = vasprintf((char **) &v->data, fmt, ap); |
a0948ffe | 1912 | |
6c4a7811 | 1913 | if (len <= 0) { |
af17d349 | 1914 | blkid_probe_free_value(v); |
6c4a7811 | 1915 | return len == 0 ? -EINVAL : -ENOMEM; |
a0948ffe | 1916 | } |
51410fc6 | 1917 | v->len = len + 1; |
a0948ffe KZ |
1918 | return 0; |
1919 | } | |
1920 | ||
cc33d693 KZ |
1921 | int blkid_probe_sprintf_value(blkid_probe pr, const char *name, |
1922 | const char *fmt, ...) | |
1923 | { | |
1924 | int rc; | |
1925 | va_list ap; | |
1926 | ||
1927 | va_start(ap, fmt); | |
1928 | rc = blkid_probe_vsprintf_value(pr, name, fmt, ap); | |
1929 | va_end(ap); | |
1930 | ||
1931 | return rc; | |
1932 | } | |
1933 | ||
f12cd8d1 | 1934 | int blkid_probe_set_magic(blkid_probe pr, uint64_t offset, |
47afae0c | 1935 | size_t len, const unsigned char *magic) |
3c83b3b2 KZ |
1936 | { |
1937 | int rc = 0; | |
1938 | struct blkid_chain *chn = blkid_probe_get_chain(pr); | |
1939 | ||
7f787ced | 1940 | if (!chn || !len || chn->binary) |
3c83b3b2 KZ |
1941 | return 0; |
1942 | ||
1943 | switch (chn->driver->id) { | |
1944 | case BLKID_CHAIN_SUBLKS: | |
1945 | if (!(chn->flags & BLKID_SUBLKS_MAGIC)) | |
1946 | return 0; | |
1947 | rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len); | |
1948 | if (!rc) | |
1949 | rc = blkid_probe_sprintf_value(pr, | |
44064b3c | 1950 | "SBMAGIC_OFFSET", "%llu", (unsigned long long)offset); |
3c83b3b2 KZ |
1951 | break; |
1952 | case BLKID_CHAIN_PARTS: | |
1953 | if (!(chn->flags & BLKID_PARTS_MAGIC)) | |
1954 | return 0; | |
1955 | rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len); | |
1956 | if (!rc) | |
1957 | rc = blkid_probe_sprintf_value(pr, | |
44064b3c | 1958 | "PTMAGIC_OFFSET", "%llu", (unsigned long long)offset); |
3c83b3b2 KZ |
1959 | break; |
1960 | default: | |
1961 | break; | |
1962 | } | |
1963 | ||
1964 | return rc; | |
1965 | } | |
1966 | ||
8c408470 TW |
1967 | static void blkid_probe_log_csum_mismatch(blkid_probe pr, size_t n, const void *csum, |
1968 | const void *expected) | |
c89a1def | 1969 | { |
8c408470 TW |
1970 | char csum_hex[256]; |
1971 | char expected_hex[sizeof(csum_hex)]; | |
1972 | int hex_size = min(sizeof(csum_hex), n * 2); | |
1973 | ||
1974 | for (int i = 0; i < hex_size; i+=2) { | |
1975 | sprintf(&csum_hex[i], "%02X", ((const unsigned char *) csum)[i / 2]); | |
1976 | sprintf(&expected_hex[i], "%02X", ((const unsigned char *) expected)[i / 2]); | |
1977 | } | |
1978 | ||
1979 | ul_debug( | |
1980 | "incorrect checksum for type %s," | |
1981 | " got %*s, expected %*s", | |
1982 | blkid_probe_get_probername(pr), | |
1983 | hex_size, csum_hex, hex_size, expected_hex); | |
1984 | } | |
1985 | ||
1986 | ||
1987 | int blkid_probe_verify_csum_buf(blkid_probe pr, size_t n, const void *csum, | |
1988 | const void *expected) | |
1989 | { | |
1990 | if (memcmp(csum, expected, n) != 0) { | |
02f3c12a GP |
1991 | struct blkid_chain *chn = blkid_probe_get_chain(pr); |
1992 | ||
8c408470 TW |
1993 | ON_DBG(LOWPROBE, blkid_probe_log_csum_mismatch(pr, n, csum, expected)); |
1994 | ||
d88803a4 KZ |
1995 | /* |
1996 | * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set | |
1997 | */ | |
ffbec06d | 1998 | if (chn && chn->driver->id == BLKID_CHAIN_SUBLKS |
d88803a4 KZ |
1999 | && (chn->flags & BLKID_SUBLKS_BADCSUM)) { |
2000 | blkid_probe_set_value(pr, "SBBADCSUM", (unsigned char *) "1", 2); | |
2001 | goto accept; | |
2002 | } | |
2003 | return 0; /* bad checksum */ | |
c89a1def KZ |
2004 | } |
2005 | ||
d88803a4 KZ |
2006 | accept: |
2007 | return 1; | |
c89a1def KZ |
2008 | } |
2009 | ||
8c408470 TW |
2010 | int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected) |
2011 | { | |
2012 | return blkid_probe_verify_csum_buf(pr, sizeof(csum), &csum, &expected); | |
2013 | } | |
2014 | ||
b3ee97a3 KZ |
2015 | /** |
2016 | * blkid_probe_get_devno: | |
2017 | * @pr: probe | |
2018 | * | |
3b159691 | 2019 | * Returns: block device number, or 0 for regular files. |
b3ee97a3 KZ |
2020 | */ |
2021 | dev_t blkid_probe_get_devno(blkid_probe pr) | |
2022 | { | |
b3ee97a3 KZ |
2023 | return pr->devno; |
2024 | } | |
2025 | ||
601fb1c1 KZ |
2026 | /** |
2027 | * blkid_probe_get_wholedisk_devno: | |
2028 | * @pr: probe | |
2029 | * | |
3b159691 | 2030 | * Returns: device number of the wholedisk, or 0 for regular files. |
601fb1c1 KZ |
2031 | */ |
2032 | dev_t blkid_probe_get_wholedisk_devno(blkid_probe pr) | |
2033 | { | |
2034 | if (!pr->disk_devno) { | |
2035 | dev_t devno, disk_devno = 0; | |
2036 | ||
2037 | devno = blkid_probe_get_devno(pr); | |
2038 | if (!devno) | |
2039 | return 0; | |
2040 | ||
2bf68c93 | 2041 | if (blkid_devno_to_wholedisk(devno, NULL, 0, &disk_devno) == 0) |
601fb1c1 KZ |
2042 | pr->disk_devno = disk_devno; |
2043 | } | |
2044 | return pr->disk_devno; | |
2045 | } | |
2046 | ||
2047 | /** | |
2048 | * blkid_probe_is_wholedisk: | |
2049 | * @pr: probe | |
2050 | * | |
2051 | * Returns: 1 if the device is whole-disk or 0. | |
2052 | */ | |
2053 | int blkid_probe_is_wholedisk(blkid_probe pr) | |
2054 | { | |
2055 | dev_t devno, disk_devno; | |
2056 | ||
2057 | devno = blkid_probe_get_devno(pr); | |
2058 | if (!devno) | |
2059 | return 0; | |
2060 | ||
2061 | disk_devno = blkid_probe_get_wholedisk_devno(pr); | |
2062 | if (!disk_devno) | |
2063 | return 0; | |
2064 | ||
2065 | return devno == disk_devno; | |
2066 | } | |
2067 | ||
fd9f45e1 KZ |
2068 | blkid_probe blkid_probe_get_wholedisk_probe(blkid_probe pr) |
2069 | { | |
2070 | dev_t disk; | |
2071 | ||
2072 | if (blkid_probe_is_wholedisk(pr)) | |
2073 | return NULL; /* this is not partition */ | |
2074 | ||
2075 | if (pr->parent) | |
2076 | /* this is cloned blkid_probe, use parent's stuff */ | |
2077 | return blkid_probe_get_wholedisk_probe(pr->parent); | |
2078 | ||
2079 | disk = blkid_probe_get_wholedisk_devno(pr); | |
2080 | ||
2081 | if (pr->disk_probe && pr->disk_probe->devno != disk) { | |
2082 | /* we have disk prober, but for another disk... close it */ | |
2083 | blkid_free_probe(pr->disk_probe); | |
2084 | pr->disk_probe = NULL; | |
2085 | } | |
2086 | ||
2087 | if (!pr->disk_probe) { | |
2088 | /* Open a new disk prober */ | |
2089 | char *disk_path = blkid_devno_to_devname(disk); | |
22769acf | 2090 | int flags; |
fd9f45e1 KZ |
2091 | |
2092 | if (!disk_path) | |
2093 | return NULL; | |
2094 | ||
c62a6311 | 2095 | DBG(LOWPROBE, ul_debug("allocate a wholedisk probe")); |
fd9f45e1 KZ |
2096 | |
2097 | pr->disk_probe = blkid_new_probe_from_filename(disk_path); | |
7eac65fc KZ |
2098 | |
2099 | free(disk_path); | |
2100 | ||
fd9f45e1 KZ |
2101 | if (!pr->disk_probe) |
2102 | return NULL; /* ENOMEM? */ | |
22769acf KZ |
2103 | |
2104 | flags = blkid_probe_get_partitions_flags(pr); | |
2105 | if (flags & BLKID_PARTS_FORCE_GPT) | |
2106 | blkid_probe_set_partitions_flags(pr->disk_probe, | |
2107 | BLKID_PARTS_FORCE_GPT); | |
fd9f45e1 KZ |
2108 | } |
2109 | ||
2110 | return pr->disk_probe; | |
2111 | } | |
2112 | ||
b3ee97a3 KZ |
2113 | /** |
2114 | * blkid_probe_get_size: | |
2115 | * @pr: probe | |
2116 | * | |
30696241 KZ |
2117 | * This function returns size of probing area as defined by blkid_probe_set_device(). |
2118 | * If the size of the probing area is unrestricted then this function returns | |
2119 | * the real size of device. See also blkid_get_dev_size(). | |
2120 | * | |
2121 | * Returns: size in bytes or -1 in case of error. | |
b3ee97a3 KZ |
2122 | */ |
2123 | blkid_loff_t blkid_probe_get_size(blkid_probe pr) | |
2124 | { | |
7f787ced | 2125 | return (blkid_loff_t) pr->size; |
b3ee97a3 KZ |
2126 | } |
2127 | ||
56e961e2 KZ |
2128 | /** |
2129 | * blkid_probe_get_offset: | |
2130 | * @pr: probe | |
2131 | * | |
2132 | * This function returns offset of probing area as defined by blkid_probe_set_device(). | |
2133 | * | |
2134 | * Returns: offset in bytes or -1 in case of error. | |
2135 | */ | |
2136 | blkid_loff_t blkid_probe_get_offset(blkid_probe pr) | |
2137 | { | |
7f787ced | 2138 | return (blkid_loff_t) pr->off; |
56e961e2 KZ |
2139 | } |
2140 | ||
2141 | /** | |
2142 | * blkid_probe_get_fd: | |
2143 | * @pr: probe | |
2144 | * | |
e3436956 | 2145 | * Returns: file descriptor for assigned device/file or -1 in case of error. |
56e961e2 KZ |
2146 | */ |
2147 | int blkid_probe_get_fd(blkid_probe pr) | |
2148 | { | |
7f787ced | 2149 | return pr->fd; |
56e961e2 KZ |
2150 | } |
2151 | ||
b3ee97a3 KZ |
2152 | /** |
2153 | * blkid_probe_get_sectorsize: | |
90ec8d9c | 2154 | * @pr: probe or NULL (for NULL returns 512) |
b3ee97a3 | 2155 | * |
2a1dfbad | 2156 | * Returns: block device logical sector size (BLKSSZGET ioctl, default 512). |
b3ee97a3 KZ |
2157 | */ |
2158 | unsigned int blkid_probe_get_sectorsize(blkid_probe pr) | |
2159 | { | |
b3ee97a3 KZ |
2160 | if (pr->blkssz) |
2161 | return pr->blkssz; | |
b3ee97a3 | 2162 | |
90ec8d9c KZ |
2163 | if (S_ISBLK(pr->mode) && |
2164 | blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz) == 0) | |
2165 | return pr->blkssz; | |
b3ee97a3 | 2166 | |
b3ee97a3 KZ |
2167 | pr->blkssz = DEFAULT_SECTOR_SIZE; |
2168 | return pr->blkssz; | |
2169 | } | |
2170 | ||
76fab513 KZ |
2171 | /** |
2172 | * blkid_probe_set_sectorsize: | |
2173 | * @pr: probe | |
2174 | * @sz: new size (to overwrite system default) | |
2175 | * | |
2176 | * Note that blkid_probe_set_device() resets this setting. Use it after | |
2177 | * blkid_probe_set_device() and before any probing call. | |
2178 | * | |
ae4e2abc KZ |
2179 | * Since: 2.30 |
2180 | * | |
76fab513 KZ |
2181 | * Returns: 0 or <0 in case of error |
2182 | */ | |
2183 | int blkid_probe_set_sectorsize(blkid_probe pr, unsigned int sz) | |
2184 | { | |
2185 | pr->blkssz = sz; | |
2186 | return 0; | |
2187 | } | |
2188 | ||
e8ae4947 DB |
2189 | /** |
2190 | * blkid_probe_get_sectors: | |
2191 | * @pr: probe | |
2192 | * | |
2193 | * Returns: 512-byte sector count or -1 in case of error. | |
2194 | */ | |
2195 | blkid_loff_t blkid_probe_get_sectors(blkid_probe pr) | |
2196 | { | |
7f787ced | 2197 | return (blkid_loff_t) (pr->size >> 9); |
e8ae4947 DB |
2198 | } |
2199 | ||
4d72b337 KZ |
2200 | /** |
2201 | * blkid_probe_numof_values: | |
2202 | * @pr: probe | |
2203 | * | |
2204 | * Returns: number of values in probing result or -1 in case of error. | |
2205 | */ | |
2206 | int blkid_probe_numof_values(blkid_probe pr) | |
2207 | { | |
6c4a7811 OO |
2208 | int i = 0; |
2209 | struct list_head *p; | |
6c4a7811 | 2210 | |
af17d349 | 2211 | list_for_each(p, &pr->values) |
6c4a7811 OO |
2212 | ++i; |
2213 | return i; | |
4d72b337 KZ |
2214 | } |
2215 | ||
81f73792 KZ |
2216 | /** |
2217 | * blkid_probe_get_value: | |
2218 | * @pr: probe | |
2219 | * @num: wanted value in range 0..N, where N is blkid_probe_numof_values() - 1 | |
2220 | * @name: pointer to return value name or NULL | |
2221 | * @data: pointer to return value data or NULL | |
2222 | * @len: pointer to return value length or NULL | |
2223 | * | |
c2dbd49b KZ |
2224 | * Note, the @len returns length of the @data, including the terminating |
2225 | * '\0' character. | |
2226 | * | |
81f73792 KZ |
2227 | * Returns: 0 on success, or -1 in case of error. |
2228 | */ | |
51410fc6 | 2229 | int blkid_probe_get_value(blkid_probe pr, int num, const char **name, |
6d042d0d | 2230 | const char **data, size_t *len) |
a0948ffe | 2231 | { |
81f73792 | 2232 | struct blkid_prval *v = __blkid_probe_get_value(pr, num); |
a0948ffe | 2233 | |
81f73792 | 2234 | if (!v) |
51410fc6 | 2235 | return -1; |
51410fc6 KZ |
2236 | if (name) |
2237 | *name = v->name; | |
2238 | if (data) | |
6d042d0d | 2239 | *data = (char *) v->data; |
51410fc6 KZ |
2240 | if (len) |
2241 | *len = v->len; | |
6644688a | 2242 | |
c62a6311 | 2243 | DBG(LOWPROBE, ul_debug("returning %s value", v->name)); |
a0948ffe KZ |
2244 | return 0; |
2245 | } | |
a0948ffe | 2246 | |
81f73792 KZ |
2247 | /** |
2248 | * blkid_probe_lookup_value: | |
2249 | * @pr: probe | |
2250 | * @name: name of value | |
2251 | * @data: pointer to return value data or NULL | |
2252 | * @len: pointer to return value length or NULL | |
2253 | * | |
c2dbd49b KZ |
2254 | * Note, the @len returns length of the @data, including the terminating |
2255 | * '\0' character. | |
2256 | * | |
81f73792 KZ |
2257 | * Returns: 0 on success, or -1 in case of error. |
2258 | */ | |
51410fc6 | 2259 | int blkid_probe_lookup_value(blkid_probe pr, const char *name, |
6d042d0d | 2260 | const char **data, size_t *len) |
a0948ffe | 2261 | { |
81f73792 | 2262 | struct blkid_prval *v = __blkid_probe_lookup_value(pr, name); |
a0948ffe | 2263 | |
81f73792 | 2264 | if (!v) |
51410fc6 | 2265 | return -1; |
81f73792 KZ |
2266 | if (data) |
2267 | *data = (char *) v->data; | |
2268 | if (len) | |
2269 | *len = v->len; | |
81f73792 | 2270 | return 0; |
a0948ffe KZ |
2271 | } |
2272 | ||
81f73792 KZ |
2273 | /** |
2274 | * blkid_probe_has_value: | |
2275 | * @pr: probe | |
2276 | * @name: name of value | |
2277 | * | |
2278 | * Returns: 1 if value exist in probing result, otherwise 0. | |
2279 | */ | |
51410fc6 | 2280 | int blkid_probe_has_value(blkid_probe pr, const char *name) |
a0948ffe | 2281 | { |
51410fc6 KZ |
2282 | if (blkid_probe_lookup_value(pr, name, NULL, NULL) == 0) |
2283 | return 1; | |
a0948ffe KZ |
2284 | return 0; |
2285 | } | |
2286 | ||
1c1726a7 KZ |
2287 | struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num) |
2288 | { | |
6c4a7811 OO |
2289 | int i = 0; |
2290 | struct list_head *p; | |
2291 | ||
7f787ced | 2292 | if (num < 0) |
1c1726a7 KZ |
2293 | return NULL; |
2294 | ||
af17d349 | 2295 | list_for_each(p, &pr->values) { |
6c4a7811 OO |
2296 | if (i++ != num) |
2297 | continue; | |
2298 | return list_entry(p, struct blkid_prval, prvals); | |
2299 | } | |
2300 | return NULL; | |
1c1726a7 KZ |
2301 | } |
2302 | ||
2303 | struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name) | |
2304 | { | |
6c4a7811 | 2305 | struct list_head *p; |
1c1726a7 | 2306 | |
7f787ced | 2307 | if (list_empty(&pr->values)) |
1c1726a7 KZ |
2308 | return NULL; |
2309 | ||
af17d349 | 2310 | list_for_each(p, &pr->values) { |
6c4a7811 OO |
2311 | struct blkid_prval *v = list_entry(p, struct blkid_prval, |
2312 | prvals); | |
1c1726a7 KZ |
2313 | |
2314 | if (v->name && strcmp(name, v->name) == 0) { | |
c62a6311 | 2315 | DBG(LOWPROBE, ul_debug("returning %s value", v->name)); |
1c1726a7 KZ |
2316 | return v; |
2317 | } | |
2318 | } | |
2319 | return NULL; | |
2320 | } | |
2321 | ||
201529bd KZ |
2322 | |
2323 | /* converts DCE UUID (uuid[16]) to human readable string | |
2324 | * - the @len should be always 37 */ | |
c9f51c71 KZ |
2325 | void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len) |
2326 | { | |
201529bd KZ |
2327 | snprintf(str, len, |
2328 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", | |
2329 | uuid[0], uuid[1], uuid[2], uuid[3], | |
2330 | uuid[4], uuid[5], | |
2331 | uuid[6], uuid[7], | |
2332 | uuid[8], uuid[9], | |
2333 | uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],uuid[15]); | |
201529bd KZ |
2334 | } |
2335 | ||
9c06cdbf KZ |
2336 | /* like uuid_is_null() from libuuid, but works with arbitrary size of UUID */ |
2337 | int blkid_uuid_is_empty(const unsigned char *buf, size_t len) | |
2338 | { | |
2339 | size_t i; | |
2340 | ||
2341 | for (i = 0; i < len; i++) | |
2342 | if (buf[i]) | |
2343 | return 0; | |
2344 | return 1; | |
2345 | } | |
c2dbd49b KZ |
2346 | |
2347 | /* Removes whitespace from the right-hand side of a string (trailing | |
2348 | * whitespace). | |
2349 | * | |
2350 | * Returns size of the new string (without \0). | |
2351 | */ | |
2352 | size_t blkid_rtrim_whitespace(unsigned char *str) | |
2353 | { | |
22e9e9c8 | 2354 | return rtrim_whitespace(str); |
c2dbd49b KZ |
2355 | } |
2356 | ||
fafe46bc ZAK |
2357 | /* Removes whitespace from the left-hand side of a string. |
2358 | * | |
2359 | * Returns size of the new string (without \0). | |
2360 | */ | |
2361 | size_t blkid_ltrim_whitespace(unsigned char *str) | |
2362 | { | |
22e9e9c8 | 2363 | return ltrim_whitespace(str); |
fafe46bc | 2364 | } |
22e9e9c8 | 2365 | |
8b7eae45 KZ |
2366 | /* |
2367 | * Some mkfs-like utils wipe some parts (usually begin) of the device. | |
2368 | * For example LVM (pvcreate) or mkswap(8). This information could be used | |
2369 | * for later resolution to conflicts between superblocks. | |
2370 | * | |
2371 | * For example we found valid LVM superblock, LVM wipes 8KiB at the begin of | |
89d39d22 KZ |
2372 | * the device. If we found another signature (for example MBR) within the |
2373 | * wiped area then the signature has been added later and LVM superblock | |
2374 | * should be ignore. | |
8b7eae45 | 2375 | * |
29e204d1 KZ |
2376 | * Note that this heuristic is not 100% reliable, for example "pvcreate --zero n" |
2377 | * can be used to keep the begin of the device unmodified. It's probably better | |
8b7eae45 KZ |
2378 | * to use this heuristic for conflicts between superblocks and partition tables |
2379 | * than for conflicts between filesystem superblocks -- existence of unwanted | |
2380 | * partition table is very unusual, because PT is pretty visible (parsed and | |
2381 | * interpreted by kernel). | |
89d39d22 KZ |
2382 | * |
2383 | * Note that we usually expect only one signature on the device, it means that | |
2384 | * we have to remember only one wiped area from previously successfully | |
2385 | * detected signature. | |
2386 | * | |
2387 | * blkid_probe_set_wiper() -- defines wiped area (e.g. LVM) | |
2388 | * blkid_probe_use_wiper() -- try to use area (e.g. MBR) | |
2389 | * | |
2390 | * Note that there is not relation between _wiper and blkid_to_wipe(). | |
2391 | * | |
8b7eae45 | 2392 | */ |
f12cd8d1 | 2393 | void blkid_probe_set_wiper(blkid_probe pr, uint64_t off, uint64_t size) |
8b7eae45 KZ |
2394 | { |
2395 | struct blkid_chain *chn; | |
2396 | ||
8b7eae45 | 2397 | if (!size) { |
c62a6311 | 2398 | DBG(LOWPROBE, ul_debug("zeroize wiper")); |
8b7eae45 KZ |
2399 | pr->wipe_size = pr->wipe_off = 0; |
2400 | pr->wipe_chain = NULL; | |
2401 | return; | |
2402 | } | |
2403 | ||
2404 | chn = pr->cur_chain; | |
2405 | ||
2406 | if (!chn || !chn->driver || | |
c9f51c71 | 2407 | chn->idx < 0 || (size_t) chn->idx >= chn->driver->nidinfos) |
8b7eae45 KZ |
2408 | return; |
2409 | ||
2410 | pr->wipe_size = size; | |
2411 | pr->wipe_off = off; | |
2412 | pr->wipe_chain = chn; | |
2413 | ||
0540ea54 | 2414 | DBG(LOWPROBE, |
fdbd7bb9 | 2415 | ul_debug("wiper set to %s::%s off=%"PRIu64" size=%"PRIu64"", |
8b7eae45 KZ |
2416 | chn->driver->name, |
2417 | chn->driver->idinfos[chn->idx]->name, | |
2418 | pr->wipe_off, pr->wipe_size)); | |
8b7eae45 KZ |
2419 | } |
2420 | ||
2421 | /* | |
2422 | * Returns 1 if the <@off,@size> area was wiped | |
2423 | */ | |
f12cd8d1 | 2424 | int blkid_probe_is_wiped(blkid_probe pr, struct blkid_chain **chn, uint64_t off, uint64_t size) |
8b7eae45 | 2425 | { |
7f787ced | 2426 | if (!size) |
8b7eae45 KZ |
2427 | return 0; |
2428 | ||
2429 | if (pr->wipe_off <= off && off + size <= pr->wipe_off + pr->wipe_size) { | |
7f787ced | 2430 | *chn = pr->wipe_chain; |
8b7eae45 KZ |
2431 | return 1; |
2432 | } | |
2433 | return 0; | |
2434 | } | |
2435 | ||
89d39d22 KZ |
2436 | /* |
2437 | * Try to use any area -- if the area has been previously wiped then the | |
9e930041 | 2438 | * previous probing result should be ignored (reset). |
89d39d22 | 2439 | */ |
f12cd8d1 | 2440 | void blkid_probe_use_wiper(blkid_probe pr, uint64_t off, uint64_t size) |
8b7eae45 KZ |
2441 | { |
2442 | struct blkid_chain *chn = NULL; | |
2443 | ||
2444 | if (blkid_probe_is_wiped(pr, &chn, off, size) && chn) { | |
c62a6311 | 2445 | DBG(LOWPROBE, ul_debug("previously wiped area modified " |
0540ea54 | 2446 | " -- ignore previous results")); |
8b7eae45 | 2447 | blkid_probe_set_wiper(pr, 0, 0); |
af17d349 | 2448 | blkid_probe_chain_reset_values(pr, chn); |
8b7eae45 KZ |
2449 | } |
2450 | } | |
67719fbb | 2451 | |
b7bca244 KZ |
2452 | static struct blkid_hint *get_hint(blkid_probe pr, const char *name) |
2453 | { | |
2454 | struct list_head *p; | |
2455 | ||
2456 | if (list_empty(&pr->hints)) | |
2457 | return NULL; | |
2458 | ||
2459 | list_for_each(p, &pr->hints) { | |
2460 | struct blkid_hint *h = list_entry(p, struct blkid_hint, hints); | |
2461 | ||
2462 | if (h->name && strcmp(name, h->name) == 0) | |
2463 | return h; | |
2464 | } | |
2465 | return NULL; | |
2466 | } | |
2467 | ||
67719fbb | 2468 | /** |
248c239b | 2469 | * blkid_probe_set_hint: |
67719fbb KZ |
2470 | * @pr: probe |
2471 | * @name: hint name or NAME=value | |
2472 | * @value: offset or another number | |
2473 | * | |
2474 | * Sets extra hint for low-level prober. If the hint is set by NAME=value | |
2475 | * notation than @value is ignored. The functions blkid_probe_set_device() | |
2476 | * and blkid_reset_probe() resets all hints. | |
2477 | * | |
2478 | * The hints are optional way how to force libblkid probing functions to check | |
2479 | * for example another location. | |
2480 | * | |
2481 | * Returns: 0 on success, or -1 in case of error. | |
2482 | */ | |
2483 | int blkid_probe_set_hint(blkid_probe pr, const char *name, uint64_t value) | |
2484 | { | |
2485 | struct blkid_hint *hint = NULL; | |
2486 | char *n = NULL, *v = NULL; | |
2487 | ||
2488 | if (strchr(name, '=')) { | |
2489 | char *end = NULL; | |
2490 | ||
2491 | if (blkid_parse_tag_string(name, &n, &v) != 0) | |
2492 | goto done; | |
2493 | ||
2494 | errno = 0; | |
2495 | value = strtoumax(v, &end, 10); | |
2496 | ||
2497 | if (errno || v == end || (end && *end)) | |
2498 | goto done; | |
b7bca244 KZ |
2499 | } |
2500 | ||
2501 | hint = get_hint(pr, n ? n : name); | |
2502 | if (hint) { | |
2503 | /* alter old hint */ | |
2504 | hint->value = value; | |
2505 | DBG(LOWPROBE, | |
2506 | ul_debug("updated hint '%s' to %"PRIu64"", hint->name, hint->value)); | |
67719fbb | 2507 | } else { |
b7bca244 KZ |
2508 | /* add a new hint */ |
2509 | if (!n) { | |
2510 | n = strdup(name); | |
2511 | if (!n) | |
2512 | goto done; | |
2513 | } | |
2514 | hint = malloc(sizeof(*hint)); | |
2515 | if (!hint) | |
67719fbb | 2516 | goto done; |
67719fbb | 2517 | |
b7bca244 KZ |
2518 | hint->name = n; |
2519 | hint->value = value; | |
67719fbb | 2520 | |
b7bca244 KZ |
2521 | INIT_LIST_HEAD(&hint->hints); |
2522 | list_add_tail(&hint->hints, &pr->hints); | |
67719fbb | 2523 | |
b7bca244 KZ |
2524 | DBG(LOWPROBE, |
2525 | ul_debug("new hint '%s' is %"PRIu64"", hint->name, hint->value)); | |
2526 | n = NULL; | |
2527 | } | |
67719fbb KZ |
2528 | done: |
2529 | free(n); | |
2530 | free(v); | |
b7bca244 | 2531 | |
67719fbb KZ |
2532 | if (!hint) |
2533 | return errno ? -errno : -EINVAL; | |
2534 | return 0; | |
2535 | } | |
2536 | ||
2537 | int blkid_probe_get_hint(blkid_probe pr, const char *name, uint64_t *value) | |
2538 | { | |
b7bca244 | 2539 | struct blkid_hint *h = get_hint(pr, name); |
67719fbb | 2540 | |
b7bca244 | 2541 | if (!h) |
67719fbb | 2542 | return -EINVAL; |
b7bca244 KZ |
2543 | if (value) |
2544 | *value = h->value; | |
2545 | return 0; | |
67719fbb KZ |
2546 | } |
2547 | ||
248c239b KZ |
2548 | /** |
2549 | * blkid_probe_reset_hints: | |
95fbd2db | 2550 | * @pr: probe |
248c239b KZ |
2551 | * |
2552 | * Removes all previously defined probinig hints. See also blkid_probe_set_hint(). | |
2553 | */ | |
2554 | void blkid_probe_reset_hints(blkid_probe pr) | |
67719fbb KZ |
2555 | { |
2556 | if (list_empty(&pr->hints)) | |
2557 | return; | |
2558 | ||
2559 | DBG(LOWPROBE, ul_debug("resetting hints")); | |
2560 | ||
2561 | while (!list_empty(&pr->hints)) { | |
2562 | struct blkid_hint *h = list_entry(pr->hints.next, | |
2563 | struct blkid_hint, hints); | |
2564 | list_del(&h->hints); | |
2565 | free(h->name); | |
2566 | free(h); | |
2567 | } | |
2568 | ||
2569 | INIT_LIST_HEAD(&pr->hints); | |
2570 | } |