]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mapfile.c
fix mdmon takeover
[thirdparty/mdadm.git] / mapfile.c
CommitLineData
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 */
360b4636 50#include "mdadm.h"
fc7e81e5 51#include <sys/file.h>
360b4636 52#include <ctype.h>
8382f19b 53
319767b8 54#define mapnames(base) { base, base ".new", base ".lock"}
cf3a3d78 55char *mapname[3][3] = {
319767b8
N
56 mapnames(VAR_RUN "/map"),
57 mapnames("/var/run/mdadm.map"),
58 mapnames(ALT_RUN "/map")
cf3a3d78 59};
8382f19b 60
cf3a3d78
N
61int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC };
62char *mapsmode[3] = { "r", "w", "w"};
8382f19b 63
cf3a3d78
N
64FILE *open_map(int modenum, int *choice)
65{
66 int i;
67 for (i = 0 ; i < 3 ; i++) {
68 int fd = open(mapname[i][modenum], mapmode[modenum], 0600);
69 if (fd >= 0) {
70 *choice = i;
71 return fdopen(fd, mapsmode[modenum]);
72 }
73 }
74 return NULL;
75}
8382f19b
NB
76
77int map_write(struct map_ent *mel)
78{
79 FILE *f;
80 int err;
cf3a3d78
N
81 int which;
82
83 f = open_map(1, &which);
8382f19b 84
8382f19b
NB
85 if (!f)
86 return 0;
195254b8
N
87 for (; mel; mel = mel->next) {
88 if (mel->bad)
89 continue;
8382f19b
NB
90 if (mel->devnum < 0)
91 fprintf(f, "mdp%d ", -1-mel->devnum);
92 else
93 fprintf(f, "md%d ", mel->devnum);
1522c538 94 fprintf(f, "%s ", mel->metadata);
8382f19b
NB
95 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
96 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
8615dcff 97 fprintf(f, "%s\n", mel->path?:"");
8382f19b
NB
98 }
99 fflush(f);
100 err = ferror(f);
101 fclose(f);
102 if (err) {
cf3a3d78 103 unlink(mapname[which][1]);
8382f19b
NB
104 return 0;
105 }
cf3a3d78
N
106 return rename(mapname[which][1],
107 mapname[which][0]) == 0;
8382f19b
NB
108}
109
ad5bc697 110
cf3a3d78
N
111static FILE *lf = NULL;
112static int lwhich = 0;
ad5bc697
N
113int map_lock(struct map_ent **melp)
114{
cf3a3d78
N
115 if (lf == NULL) {
116 lf = open_map(2, &lwhich);
117 if (lf == NULL)
ad5bc697 118 return -1;
fc7e81e5 119 if (flock(fileno(lf), LOCK_EX) != 0) {
cf3a3d78
N
120 fclose(lf);
121 lf = NULL;
ad5bc697
N
122 return -1;
123 }
124 }
125 if (*melp)
126 map_free(*melp);
127 map_read(melp);
128 return 0;
129}
130
131void map_unlock(struct map_ent **melp)
132{
fc7e81e5
N
133 if (lf) {
134 flock(fileno(lf), LOCK_UN);
cf3a3d78 135 fclose(lf);
fc7e81e5 136 }
cf3a3d78
N
137 unlink(mapname[lwhich][2]);
138 lf = NULL;
ad5bc697
N
139}
140
8382f19b 141void map_add(struct map_ent **melp,
1522c538 142 int devnum, char *metadata, int uuid[4], char *path)
8382f19b
NB
143{
144 struct map_ent *me = malloc(sizeof(*me));
145
146 me->devnum = devnum;
1522c538 147 strcpy(me->metadata, metadata);
8382f19b 148 memcpy(me->uuid, uuid, 16);
8615dcff 149 me->path = path ? strdup(path) : NULL;
8382f19b 150 me->next = *melp;
195254b8 151 me->bad = 0;
8382f19b
NB
152 *melp = me;
153}
154
155void map_read(struct map_ent **melp)
156{
157 FILE *f;
158 char buf[8192];
159 char path[200];
1522c538
NB
160 int devnum, uuid[4];
161 char metadata[30];
8382f19b 162 char nam[4];
cf3a3d78 163 int which;
8382f19b
NB
164
165 *melp = NULL;
166
cf3a3d78 167 f = open_map(0, &which);
3a56f223
N
168 if (!f) {
169 RebuildMap();
cf3a3d78 170 f = open_map(0, &which);
3a56f223 171 }
8382f19b
NB
172 if (!f)
173 return;
174
175 while (fgets(buf, sizeof(buf), f)) {
8615dcff 176 path[0] = 0;
c5afc314 177 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
1522c538 178 nam, &devnum, metadata, uuid, uuid+1,
8615dcff 179 uuid+2, uuid+3, path) >= 7) {
c5afc314
N
180 if (strncmp(nam, "md", 2) != 0)
181 continue;
182 if (nam[2] == 'p')
8382f19b 183 devnum = -1 - devnum;
1522c538 184 map_add(melp, devnum, metadata, uuid, path);
8382f19b
NB
185 }
186 }
187 fclose(f);
188}
189
190void map_free(struct map_ent *map)
191{
192 while (map) {
193 struct map_ent *mp = map;
194 map = mp->next;
195 free(mp->path);
196 free(mp);
197 }
198}
199
1522c538 200int map_update(struct map_ent **mpp, int devnum, char *metadata,
8382f19b
NB
201 int *uuid, char *path)
202{
203 struct map_ent *map, *mp;
204 int rv;
205
206 if (mpp && *mpp)
207 map = *mpp;
208 else
209 map_read(&map);
210
211 for (mp = map ; mp ; mp=mp->next)
212 if (mp->devnum == devnum) {
1522c538 213 strcpy(mp->metadata, metadata);
8382f19b
NB
214 memcpy(mp->uuid, uuid, 16);
215 free(mp->path);
8615dcff 216 mp->path = path ? strdup(path) : NULL;
8382f19b
NB
217 break;
218 }
219 if (!mp)
1522c538 220 map_add(&map, devnum, metadata, uuid, path);
a04d5763
N
221 if (mpp)
222 *mpp = NULL;
8382f19b
NB
223 rv = map_write(map);
224 map_free(map);
225 return rv;
226}
227
228void map_delete(struct map_ent **mapp, int devnum)
229{
230 struct map_ent *mp;
231
232 if (*mapp == NULL)
233 map_read(mapp);
234
235 for (mp = *mapp; mp; mp = *mapp) {
236 if (mp->devnum == devnum) {
237 *mapp = mp->next;
238 free(mp->path);
239 free(mp);
240 } else
241 mapp = & mp->next;
242 }
243}
244
245struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
246{
247 struct map_ent *mp;
248 if (!*map)
249 map_read(map);
250
195254b8
N
251 for (mp = *map ; mp ; mp = mp->next) {
252 if (memcmp(uuid, mp->uuid, 16) != 0)
253 continue;
254 if (!mddev_busy(mp->devnum)) {
255 mp->bad = 1;
256 continue;
257 }
258 return mp;
259 }
8382f19b 260 return NULL;
4ccad7b1 261}
8382f19b 262
4ccad7b1
N
263struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
264{
265 struct map_ent *mp;
266 if (!*map)
267 map_read(map);
268
195254b8
N
269 for (mp = *map ; mp ; mp = mp->next) {
270 if (mp->devnum != devnum)
271 continue;
272 if (!mddev_busy(mp->devnum)) {
273 mp->bad = 1;
274 continue;
275 }
276 return mp;
277 }
4ccad7b1 278 return NULL;
8382f19b 279}
f2e55ecc
N
280
281struct map_ent *map_by_name(struct map_ent **map, char *name)
282{
283 struct map_ent *mp;
284 if (!*map)
285 map_read(map);
286
287 for (mp = *map ; mp ; mp = mp->next) {
8615dcff
N
288 if (!mp->path)
289 continue;
f2e55ecc
N
290 if (strncmp(mp->path, "/dev/md/", 8) != 0)
291 continue;
195254b8
N
292 if (strcmp(mp->path+8, name) != 0)
293 continue;
294 if (!mddev_busy(mp->devnum)) {
295 mp->bad = 1;
296 continue;
297 }
298 return mp;
f2e55ecc
N
299 }
300 return NULL;
301}
3a56f223 302
f98d41dd
DW
303/* sets the proper subarray and container_dev according to the metadata
304 * version super_by_fd does this automatically, this routine is meant as
305 * a supplement for guess_super()
306 */
307static void set_member_info(struct supertype *st, struct mdstat_ent *ent)
308{
f98d41dd
DW
309
310 st->subarray[0] = '\0';
311
2b9aa337
N
312 if (ent->metadata_version == NULL ||
313 strncmp(ent->metadata_version, "external:", 9) != 0)
f98d41dd
DW
314 return;
315
2b9aa337
N
316 if (is_subarray(&ent->metadata_version[9])) {
317 char version[strlen(ent->metadata_version)+1];
318 char *subarray;
f98d41dd
DW
319 char *name = &version[10];
320
2b9aa337
N
321 strcpy(version, ent->metadata_version);
322 subarray = strrchr(version, '/');
323 name = &version[10];
324
f98d41dd
DW
325 if (!subarray)
326 return;
327 *subarray++ = '\0';
328
329 st->container_dev = devname2devnum(name);
330 strncpy(st->subarray, subarray, sizeof(st->subarray));
331 }
332}
333
3a56f223
N
334void RebuildMap(void)
335{
336 struct mdstat_ent *mdstat = mdstat_read(0, 0);
337 struct mdstat_ent *md;
338 struct map_ent *map = NULL;
339 int mdp = get_mdp_major();
360b4636
N
340 int require_homehost;
341 char sys_hostname[256];
342 char *homehost = conf_get_homehost(&require_homehost);
343
344 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
345 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
346 sys_hostname[sizeof(sys_hostname)-1] = 0;
347 homehost = sys_hostname;
348 }
349 }
3a56f223
N
350
351 for (md = mdstat ; md ; md = md->next) {
506ffd1e 352 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
3a56f223
N
353 struct mdinfo *sd;
354
506ffd1e
DW
355 if (!sra)
356 continue;
357
3a56f223 358 for (sd = sra->devs ; sd ; sd = sd->next) {
360b4636 359 char namebuf[100];
3a56f223
N
360 char dn[30];
361 int dfd;
362 int ok;
363 struct supertype *st;
364 char *path;
365 struct mdinfo info;
366
367 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
368 dfd = dev_open(dn, O_RDONLY);
369 if (dfd < 0)
370 continue;
371 st = guess_super(dfd);
372 if ( st == NULL)
373 ok = -1;
f98d41dd
DW
374 else {
375 set_member_info(st, md);
3a56f223 376 ok = st->ss->load_super(st, dfd, NULL);
f98d41dd 377 }
3a56f223
N
378 close(dfd);
379 if (ok != 0)
380 continue;
381 st->ss->getinfo_super(st, &info);
60f8cb9b 382 if (md->devnum >= 0)
3a56f223
N
383 path = map_dev(MD_MAJOR, md->devnum, 0);
384 else
385 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
360b4636
N
386 if (path == NULL ||
387 strncmp(path, "/dev/md/", 8) != 0) {
388 /* We would really like a name that provides
389 * an MD_DEVNAME for udev.
390 * The name needs to be unique both in /dev/md/
391 * and in this mapfile.
392 * It needs to match watch -I or -As would come
393 * up with.
394 * That means:
395 * Check if array is in mdadm.conf
396 * - if so use that.
397 * determine trustworthy from homehost etc
398 * find a unique name based on metadata name.
399 *
400 */
401 struct mddev_ident_s *match = conf_match(&info, st);
402 struct stat stb;
403 if (match && match->devname && match->devname[0] == '/') {
404 path = match->devname;
405 if (path[0] != '/') {
406 strcpy(namebuf, "/dev/md/");
407 strcat(namebuf, path);
408 path = namebuf;
409 }
410 } else {
411 int unum = 0;
412 char *sep = "_";
413 const char *name;
414 int conflict = 1;
415 if ((homehost == NULL ||
416 st->ss->match_home(st, homehost) != 1) &&
417 st->ss->match_home(st, "any") != 1 &&
418 (require_homehost
419 || ! conf_name_is_free(info.name)))
420 /* require a numeric suffix */
421 unum = 0;
422 else
423 /* allow name to be used as-is if no conflict */
424 unum = -1;
425 name = info.name;
426 if (!*name) {
427 name = st->ss->name;
428 if (!isdigit(name[strlen(name)-1]) &&
429 unum == -1) {
430 unum = 0;
431 sep = "";
432 }
433 }
434 if (strchr(name, ':'))
435 /* probably a uniquifying
436 * hostname prefix. Allow
437 * without a suffix
438 */
439 unum = -1;
440
441 while (conflict) {
442 if (unum >= 0)
443 sprintf(namebuf, "/dev/md/%s%s%d",
444 name, sep, unum);
445 else
446 sprintf(namebuf, "/dev/md/%s",
447 name);
448 unum++;
449 if (lstat(namebuf, &stb) != 0 &&
450 (map == NULL ||
451 !map_by_name(&map, namebuf+8)))
452 conflict = 0;
453 }
454 path = namebuf;
455 }
456 }
8a659c33
N
457 map_add(&map, md->devnum,
458 info.text_version,
8615dcff 459 info.uuid, path);
3a56f223
N
460 st->ss->free_super(st);
461 break;
462 }
8a659c33 463 sysfs_free(sra);
3a56f223
N
464 }
465 map_write(map);
466 map_free(map);
8a659c33
N
467 for (md = mdstat ; md ; md = md->next) {
468 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION);
469 sysfs_uevent(sra, "change");
470 sysfs_free(sra);
471 }
78fbcc10 472 free_mdstat(mdstat);
3a56f223 473}