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