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