Debian Codegraph @

libfdisk/src/wipe.c

raw
1 #include "c.h"
2 #include "strutils.h"
3
4 #ifdef HAVE_LIBBLKID
5 # include <blkid.h>
6 #endif
7
8 #include "fdiskP.h"
9
10 struct fdisk_wipe {
11 struct list_head wipes;
12 uint64_t start; /* sectors */
13 uint64_t size; /* sectors */
14 };
15
16 static struct fdisk_wipe *fdisk_get_wipe_area(
17 struct fdisk_context *cxt,
18 uint64_t start,
19 uint64_t size)
20 {
21 struct list_head *p;
22
23 if (cxt == NULL || list_empty(&cxt->wipes))
24 return NULL;
25
26 list_for_each(p, &cxt->wipes) {
27 struct fdisk_wipe *wp = list_entry(p, struct fdisk_wipe, wipes);
28 if (wp->start == start && wp->size == size)
29 return wp;
30 }
31 return NULL;
32 }
33
34 void fdisk_free_wipe_areas(struct fdisk_context *cxt)
35 {
36 while (!list_empty(&cxt->wipes)) {
37 struct fdisk_wipe *wp = list_entry(cxt->wipes.next,
38 struct fdisk_wipe, wipes);
39 DBG(WIPE, ul_debugobj(wp, "free [start=%ju, size=%ju]",
40 (uintmax_t) wp->start, (uintmax_t) wp->size));
41 list_del(&wp->wipes);
42 free(wp);
43 }
44 }
45
46 int fdisk_has_wipe_area(struct fdisk_context *cxt,
47 uint64_t start,
48 uint64_t size)
49 {
50 return fdisk_get_wipe_area(cxt, start, size) != NULL;
51 }
52
53 /* Add/remove new wiping area
54 *
55 * Returns: <0 on error, or old area setting (1: enabled, 0: disabled)
56 */
57 int fdisk_set_wipe_area(struct fdisk_context *cxt,
58 uint64_t start,
59 uint64_t size,
60 int enable)
61 {
62 struct fdisk_wipe *wp;
63
64 if (FDISK_IS_UNDEF(start) || FDISK_IS_UNDEF(size))
65 return -EINVAL;
66
67 wp = fdisk_get_wipe_area(cxt, start, size);
68
69 /* disable */
70 if (!enable) {
71 if (wp) {
72 DBG(WIPE, ul_debugobj(wp, "disable [start=%ju, size=%ju]",
73 (uintmax_t) start, (uintmax_t) size));
74 list_del(&wp->wipes);
75 free(wp);
76 return 1;
77 }
78 DBG(WIPE, ul_debug("not requested"));
79 return 0;
80 }
81
82 /* enable */
83 if (wp)
84 return 1; /* already enabled */
85
86 wp = calloc(1, sizeof(*wp));
87 if (!wp)
88 return -ENOMEM;
89
90 DBG(WIPE, ul_debugobj(wp, "enable [start=%ju, size=%ju]",
91 (uintmax_t) start, (uintmax_t) size));
92
93 INIT_LIST_HEAD(&wp->wipes);
94 wp->start = start;
95 wp->size = size;
96 list_add_tail(&wp->wipes, &cxt->wipes);
97
98 return 0;
99 }
100
101 #ifndef HAVE_LIBBLKID
102 int fdisk_do_wipe(struct fdisk_context *cxt __attribute__((__unused__)))
103 {
104 return 0;
105 }
106 #else
107 int fdisk_do_wipe(struct fdisk_context *cxt)
108 {
109 struct list_head *p;
110 blkid_probe pr;
111 int rc;
112
113 assert(cxt);
114 assert(cxt->dev_fd >= 0);
115
116 if (list_empty(&cxt->wipes))
117 return 0;
118
119 pr = blkid_new_probe();
120 if (!pr)
121 return -ENOMEM;
122
123 list_for_each(p, &cxt->wipes) {
124 struct fdisk_wipe *wp = list_entry(p, struct fdisk_wipe, wipes);
125 blkid_loff_t start = (blkid_loff_t) wp->start * cxt->sector_size,
126 size = (blkid_loff_t) wp->size * cxt->sector_size;
127
128 DBG(WIPE, ul_debugobj(wp, "initialize libblkid prober [start=%ju, size=%ju]",
129 (uintmax_t) start, (uintmax_t) size));
130
131 rc = blkid_probe_set_device(pr, cxt->dev_fd, start, size);
132 if (rc) {
133 DBG(WIPE, ul_debugobj(wp, "blkid_probe_set_device() failed [rc=%d]", rc));
134 return rc;
135 }
136 blkid_probe_set_sectorsize(pr, cxt->sector_size);
137
138 DBG(WIPE, ul_debugobj(wp, " wiping..."));
139 blkid_wipe_all(pr);
140 }
141
142 blkid_free_probe(pr);
143 return 0;
144 }
145 #endif
146
147
148 /*
149 * Please don't call this function if there is already a PT.
150 *
151 * Returns: 0 if nothing found, < 0 on error, 1 if found a signature
152 */
153 #ifndef HAVE_LIBBLKID
154 int fdisk_check_collisions(struct fdisk_context *cxt __attribute__((__unused__)))
155 {
156 return 0;
157 }
158 #else
159 int fdisk_check_collisions(struct fdisk_context *cxt)
160 {
161 int rc = 0;
162 blkid_probe pr;
163
164 assert(cxt);
165 assert(cxt->dev_fd >= 0);
166
167 DBG(WIPE, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
168
169 pr = blkid_new_probe();
170 if (!pr)
171 return -ENOMEM;
172 rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0);
173 if (rc)
174 return rc;
175
176 cxt->pt_collision = 0;
177 free(cxt->collision);
178 cxt->collision = NULL;
179
180 blkid_probe_set_sectorsize(pr, cxt->sector_size);
181
182 blkid_probe_enable_superblocks(pr, 1);
183 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE |
184 BLKID_SUBLKS_MAGIC |
185 BLKID_SUBLKS_BADCSUM);
186 blkid_probe_enable_partitions(pr, 1);
187 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_FORCE_GPT |
188 BLKID_PARTS_MAGIC);
189
190 /* we care about the first found FS/raid, so don't call blkid_do_probe()
191 * in loop or don't use blkid_do_fullprobe() ... */
192 rc = blkid_do_probe(pr);
193 if (rc == 0) {
194 const char *name = NULL;
195 const char *off = NULL;
196
197 if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0) {
198 blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
199
200 } else if (blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
201 blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
202 cxt->pt_collision = 1;
203 }
204
205 if (name) {
206 cxt->collision = strdup(name);
207 if (!cxt->collision)
208 rc = -ENOMEM;
209 }
210 if (!rc && off)
211 cxt->collision_offset = strtoumax(off, NULL, 10);
212 }
213
214 blkid_free_probe(pr);
215 return rc < 0 ? rc : cxt->collision ? 1 : 0;
216 }
217 #endif