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