]>
Commit | Line | Data |
---|---|---|
8382f19b NB |
1 | /* |
2 | * mapfile - manage /var/run/mdadm.map. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
cf3a3d78 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
8382f19b NB |
6 | * |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | * Paper: Neil Brown | |
25 | * Novell Inc | |
26 | * GPO Box Q1283 | |
27 | * QVB Post Office, NSW 1230 | |
28 | * Australia | |
29 | */ | |
30 | ||
31 | /* /var/run/mdadm.map is used to track arrays being created in --incremental | |
32 | * more. It particularly allows lookup from UUID to array device, but | |
33 | * also allows the array device name to be easily found. | |
34 | * | |
35 | * The map file is line based with space separated fields. The fields are: | |
1522c538 NB |
36 | * Device id - mdX or mdpX where X is a number. |
37 | * metadata - 0.90 1.0 1.1 1.2 ddf ... | |
8382f19b NB |
38 | * UUID - uuid of the array |
39 | * path - path where device created: /dev/md/home | |
40 | * | |
cf3a3d78 N |
41 | * The preferred location for the map file is /var/run/mdadm.map. |
42 | * However /var/run may not exist or be writable in early boot. And if | |
43 | * no-one has created /var/run/mdadm, we still want to survive. | |
44 | * So possible locations are: | |
45 | * /var/run/mdadm/map /var/run/mdadm.map /dev/.mdadm.map | |
46 | * the last, because udev requires a writable /dev very early. | |
47 | * We read from the first one that exists and write to the first | |
48 | * one that we can. | |
8382f19b | 49 | */ |
cf3a3d78 | 50 | #include "mdadm.h" |
8382f19b | 51 | |
cf3a3d78 N |
52 | #define mapnames(base) { #base, #base ".new", #base ".lock"} |
53 | char *mapname[3][3] = { | |
54 | mapnames(/var/run/mdadm/map), | |
55 | mapnames(/var/run/mdadm.map), | |
56 | mapnames(/dev/.mdadm.map) | |
57 | }; | |
8382f19b | 58 | |
cf3a3d78 N |
59 | int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC }; |
60 | char *mapsmode[3] = { "r", "w", "w"}; | |
8382f19b | 61 | |
cf3a3d78 N |
62 | FILE *open_map(int modenum, int *choice) |
63 | { | |
64 | int i; | |
65 | for (i = 0 ; i < 3 ; i++) { | |
66 | int fd = open(mapname[i][modenum], mapmode[modenum], 0600); | |
67 | if (fd >= 0) { | |
68 | *choice = i; | |
69 | return fdopen(fd, mapsmode[modenum]); | |
70 | } | |
71 | } | |
72 | return NULL; | |
73 | } | |
8382f19b NB |
74 | |
75 | int map_write(struct map_ent *mel) | |
76 | { | |
77 | FILE *f; | |
78 | int err; | |
cf3a3d78 N |
79 | int which; |
80 | ||
81 | f = open_map(1, &which); | |
8382f19b | 82 | |
8382f19b NB |
83 | if (!f) |
84 | return 0; | |
195254b8 N |
85 | for (; mel; mel = mel->next) { |
86 | if (mel->bad) | |
87 | continue; | |
8382f19b NB |
88 | if (mel->devnum < 0) |
89 | fprintf(f, "mdp%d ", -1-mel->devnum); | |
90 | else | |
91 | fprintf(f, "md%d ", mel->devnum); | |
1522c538 | 92 | fprintf(f, "%s ", mel->metadata); |
8382f19b NB |
93 | fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0], |
94 | mel->uuid[1], mel->uuid[2], mel->uuid[3]); | |
95 | fprintf(f, "%s\n", mel->path); | |
8382f19b NB |
96 | } |
97 | fflush(f); | |
98 | err = ferror(f); | |
99 | fclose(f); | |
100 | if (err) { | |
cf3a3d78 | 101 | unlink(mapname[which][1]); |
8382f19b NB |
102 | return 0; |
103 | } | |
cf3a3d78 N |
104 | return rename(mapname[which][1], |
105 | mapname[which][0]) == 0; | |
8382f19b NB |
106 | } |
107 | ||
ad5bc697 | 108 | |
cf3a3d78 N |
109 | static FILE *lf = NULL; |
110 | static int lwhich = 0; | |
ad5bc697 N |
111 | int map_lock(struct map_ent **melp) |
112 | { | |
cf3a3d78 N |
113 | if (lf == NULL) { |
114 | lf = open_map(2, &lwhich); | |
115 | if (lf == NULL) | |
ad5bc697 | 116 | return -1; |
cf3a3d78 N |
117 | if (lockf(fileno(lf), F_LOCK, 0) != 0) { |
118 | fclose(lf); | |
119 | lf = NULL; | |
ad5bc697 N |
120 | return -1; |
121 | } | |
122 | } | |
123 | if (*melp) | |
124 | map_free(*melp); | |
125 | map_read(melp); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | void map_unlock(struct map_ent **melp) | |
130 | { | |
cf3a3d78 N |
131 | if (lf) |
132 | fclose(lf); | |
133 | unlink(mapname[lwhich][2]); | |
134 | lf = NULL; | |
ad5bc697 N |
135 | } |
136 | ||
8382f19b | 137 | void map_add(struct map_ent **melp, |
1522c538 | 138 | int devnum, char *metadata, int uuid[4], char *path) |
8382f19b NB |
139 | { |
140 | struct map_ent *me = malloc(sizeof(*me)); | |
141 | ||
142 | me->devnum = devnum; | |
1522c538 | 143 | strcpy(me->metadata, metadata); |
8382f19b NB |
144 | memcpy(me->uuid, uuid, 16); |
145 | me->path = strdup(path); | |
146 | me->next = *melp; | |
195254b8 | 147 | me->bad = 0; |
8382f19b NB |
148 | *melp = me; |
149 | } | |
150 | ||
151 | void map_read(struct map_ent **melp) | |
152 | { | |
153 | FILE *f; | |
154 | char buf[8192]; | |
155 | char path[200]; | |
1522c538 NB |
156 | int devnum, uuid[4]; |
157 | char metadata[30]; | |
8382f19b | 158 | char nam[4]; |
cf3a3d78 | 159 | int which; |
8382f19b NB |
160 | |
161 | *melp = NULL; | |
162 | ||
cf3a3d78 | 163 | f = open_map(0, &which); |
3a56f223 N |
164 | if (!f) { |
165 | RebuildMap(); | |
cf3a3d78 | 166 | f = open_map(0, &which); |
3a56f223 | 167 | } |
8382f19b NB |
168 | if (!f) |
169 | return; | |
170 | ||
171 | while (fgets(buf, sizeof(buf), f)) { | |
c5afc314 | 172 | if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s", |
1522c538 | 173 | nam, &devnum, metadata, uuid, uuid+1, |
c5afc314 N |
174 | uuid+2, uuid+3, path) == 8) { |
175 | if (strncmp(nam, "md", 2) != 0) | |
176 | continue; | |
177 | if (nam[2] == 'p') | |
8382f19b | 178 | devnum = -1 - devnum; |
1522c538 | 179 | map_add(melp, devnum, metadata, uuid, path); |
8382f19b NB |
180 | } |
181 | } | |
182 | fclose(f); | |
183 | } | |
184 | ||
185 | void map_free(struct map_ent *map) | |
186 | { | |
187 | while (map) { | |
188 | struct map_ent *mp = map; | |
189 | map = mp->next; | |
190 | free(mp->path); | |
191 | free(mp); | |
192 | } | |
193 | } | |
194 | ||
1522c538 | 195 | int map_update(struct map_ent **mpp, int devnum, char *metadata, |
8382f19b NB |
196 | int *uuid, char *path) |
197 | { | |
198 | struct map_ent *map, *mp; | |
199 | int rv; | |
200 | ||
201 | if (mpp && *mpp) | |
202 | map = *mpp; | |
203 | else | |
204 | map_read(&map); | |
205 | ||
206 | for (mp = map ; mp ; mp=mp->next) | |
207 | if (mp->devnum == devnum) { | |
1522c538 | 208 | strcpy(mp->metadata, metadata); |
8382f19b NB |
209 | memcpy(mp->uuid, uuid, 16); |
210 | free(mp->path); | |
211 | mp->path = strdup(path); | |
212 | break; | |
213 | } | |
214 | if (!mp) | |
1522c538 | 215 | map_add(&map, devnum, metadata, uuid, path); |
a04d5763 N |
216 | if (mpp) |
217 | *mpp = NULL; | |
8382f19b NB |
218 | rv = map_write(map); |
219 | map_free(map); | |
220 | return rv; | |
221 | } | |
222 | ||
223 | void map_delete(struct map_ent **mapp, int devnum) | |
224 | { | |
225 | struct map_ent *mp; | |
226 | ||
227 | if (*mapp == NULL) | |
228 | map_read(mapp); | |
229 | ||
230 | for (mp = *mapp; mp; mp = *mapp) { | |
231 | if (mp->devnum == devnum) { | |
232 | *mapp = mp->next; | |
233 | free(mp->path); | |
234 | free(mp); | |
235 | } else | |
236 | mapp = & mp->next; | |
237 | } | |
238 | } | |
239 | ||
240 | struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4]) | |
241 | { | |
242 | struct map_ent *mp; | |
243 | if (!*map) | |
244 | map_read(map); | |
245 | ||
195254b8 N |
246 | for (mp = *map ; mp ; mp = mp->next) { |
247 | if (memcmp(uuid, mp->uuid, 16) != 0) | |
248 | continue; | |
249 | if (!mddev_busy(mp->devnum)) { | |
250 | mp->bad = 1; | |
251 | continue; | |
252 | } | |
253 | return mp; | |
254 | } | |
8382f19b | 255 | return NULL; |
4ccad7b1 | 256 | } |
8382f19b | 257 | |
4ccad7b1 N |
258 | struct map_ent *map_by_devnum(struct map_ent **map, int devnum) |
259 | { | |
260 | struct map_ent *mp; | |
261 | if (!*map) | |
262 | map_read(map); | |
263 | ||
195254b8 N |
264 | for (mp = *map ; mp ; mp = mp->next) { |
265 | if (mp->devnum != devnum) | |
266 | continue; | |
267 | if (!mddev_busy(mp->devnum)) { | |
268 | mp->bad = 1; | |
269 | continue; | |
270 | } | |
271 | return mp; | |
272 | } | |
4ccad7b1 | 273 | return NULL; |
8382f19b | 274 | } |
f2e55ecc N |
275 | |
276 | struct map_ent *map_by_name(struct map_ent **map, char *name) | |
277 | { | |
278 | struct map_ent *mp; | |
279 | if (!*map) | |
280 | map_read(map); | |
281 | ||
282 | for (mp = *map ; mp ; mp = mp->next) { | |
283 | if (strncmp(mp->path, "/dev/md/", 8) != 0) | |
284 | continue; | |
195254b8 N |
285 | if (strcmp(mp->path+8, name) != 0) |
286 | continue; | |
287 | if (!mddev_busy(mp->devnum)) { | |
288 | mp->bad = 1; | |
289 | continue; | |
290 | } | |
291 | return mp; | |
f2e55ecc N |
292 | } |
293 | return NULL; | |
294 | } | |
3a56f223 N |
295 | |
296 | void RebuildMap(void) | |
297 | { | |
298 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
299 | struct mdstat_ent *md; | |
300 | struct map_ent *map = NULL; | |
301 | int mdp = get_mdp_major(); | |
302 | ||
303 | for (md = mdstat ; md ; md = md->next) { | |
506ffd1e | 304 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS); |
3a56f223 N |
305 | struct mdinfo *sd; |
306 | ||
506ffd1e DW |
307 | if (!sra) |
308 | continue; | |
309 | ||
3a56f223 N |
310 | for (sd = sra->devs ; sd ; sd = sd->next) { |
311 | char dn[30]; | |
312 | int dfd; | |
313 | int ok; | |
314 | struct supertype *st; | |
315 | char *path; | |
316 | struct mdinfo info; | |
317 | ||
318 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); | |
319 | dfd = dev_open(dn, O_RDONLY); | |
320 | if (dfd < 0) | |
321 | continue; | |
322 | st = guess_super(dfd); | |
323 | if ( st == NULL) | |
324 | ok = -1; | |
325 | else | |
326 | ok = st->ss->load_super(st, dfd, NULL); | |
327 | close(dfd); | |
328 | if (ok != 0) | |
329 | continue; | |
330 | st->ss->getinfo_super(st, &info); | |
331 | if (md->devnum > 0) | |
332 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
333 | else | |
334 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
8a659c33 N |
335 | map_add(&map, md->devnum, |
336 | info.text_version, | |
3a56f223 N |
337 | info.uuid, path ? : "/unknown"); |
338 | st->ss->free_super(st); | |
339 | break; | |
340 | } | |
8a659c33 | 341 | sysfs_free(sra); |
3a56f223 N |
342 | } |
343 | map_write(map); | |
344 | map_free(map); | |
8a659c33 N |
345 | for (md = mdstat ; md ; md = md->next) { |
346 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION); | |
347 | sysfs_uevent(sra, "change"); | |
348 | sysfs_free(sra); | |
349 | } | |
78fbcc10 | 350 | free_mdstat(mdstat); |
3a56f223 | 351 | } |