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