]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mapfile.c
mdmonitor: use MAILFROM to set sendmail envelope sender address
[thirdparty/mdadm.git] / mapfile.c
CommitLineData
8382f19b 1/*
9e751dc7 2 * mapfile - keep track of uuid <-> array mapping. Part of:
8382f19b
NB
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
9e751dc7 5 * Copyright (C) 2006-2010 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
9e751dc7 31/* The mapfile 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 *
96fd06ed
N
41 * The best place for the mapfile is /run/mdadm/map. Distros and users
42 * which have not switched to /run yet can choose a different location
43 * at compile time via MAP_DIR and MAP_FILE.
8382f19b 44 */
360b4636 45#include "mdadm.h"
ee3a6cab
MT
46#include "xmalloc.h"
47
fc7e81e5 48#include <sys/file.h>
360b4636 49#include <ctype.h>
8382f19b 50
df65ac70
N
51#define MAP_READ 0
52#define MAP_NEW 1
53#define MAP_LOCK 2
54#define MAP_DIRNAME 3
9e751dc7
N
55
56char *mapname[4] = {
57 MAP_DIR "/" MAP_FILE,
58 MAP_DIR "/" MAP_FILE ".new",
59 MAP_DIR "/" MAP_FILE ".lock",
60 MAP_DIR
cf3a3d78 61};
8382f19b 62
753cf905 63int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
cf3a3d78 64char *mapsmode[3] = { "r", "w", "w"};
8382f19b 65
9e751dc7 66FILE *open_map(int modenum)
cf3a3d78 67{
9e751dc7
N
68 int fd;
69 if ((mapmode[modenum] & O_CREAT))
70 /* Attempt to create directory, don't worry about
71 * failure.
72 */
73 (void)mkdir(mapname[MAP_DIRNAME], 0755);
74 fd = open(mapname[modenum], mapmode[modenum], 0600);
75 if (fd >= 0)
76 return fdopen(fd, mapsmode[modenum]);
cf3a3d78
N
77 return NULL;
78}
8382f19b
NB
79
80int map_write(struct map_ent *mel)
81{
82 FILE *f;
83 int err;
cf3a3d78 84
9e751dc7 85 f = open_map(MAP_NEW);
8382f19b 86
8382f19b
NB
87 if (!f)
88 return 0;
195254b8
N
89 for (; mel; mel = mel->next) {
90 if (mel->bad)
91 continue;
4dd2df09 92 fprintf(f, "%s ", mel->devnm);
1522c538 93 fprintf(f, "%s ", mel->metadata);
8382f19b
NB
94 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
95 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
8615dcff 96 fprintf(f, "%s\n", mel->path?:"");
8382f19b
NB
97 }
98 fflush(f);
99 err = ferror(f);
100 fclose(f);
101 if (err) {
9e751dc7 102 unlink(mapname[1]);
8382f19b
NB
103 return 0;
104 }
9e751dc7
N
105 return rename(mapname[1],
106 mapname[0]) == 0;
8382f19b
NB
107}
108
cf3a3d78 109static FILE *lf = NULL;
ad5bc697
N
110int map_lock(struct map_ent **melp)
111{
08415c46
N
112 while (lf == NULL) {
113 struct stat buf;
9e751dc7 114 lf = open_map(MAP_LOCK);
cf3a3d78 115 if (lf == NULL)
ad5bc697 116 return -1;
fc7e81e5 117 if (flock(fileno(lf), LOCK_EX) != 0) {
cf3a3d78
N
118 fclose(lf);
119 lf = NULL;
ad5bc697
N
120 return -1;
121 }
08415c46
N
122 if (fstat(fileno(lf), &buf) != 0 ||
123 buf.st_nlink == 0) {
124 /* The owner of the lock unlinked it,
125 * so we have a lock on a stale file,
126 * try again
127 */
128 fclose(lf);
129 lf = NULL;
130 }
ad5bc697
N
131 }
132 if (*melp)
133 map_free(*melp);
134 map_read(melp);
135 return 0;
136}
137
138void map_unlock(struct map_ent **melp)
139{
fc7e81e5 140 if (lf) {
08415c46
N
141 /* must unlink before closing the file,
142 * as only the owner of the lock may
143 * unlink the file
144 */
9e751dc7 145 unlink(mapname[2]);
cf3a3d78 146 fclose(lf);
fc7e81e5 147 }
898bd1ec
GJ
148 if (*melp)
149 map_free(*melp);
cf3a3d78 150 lf = NULL;
ad5bc697
N
151}
152
cc700db3
LD
153void map_fork(void)
154{
155 /* We are forking, so must close the lock file.
156 * Don't risk flushing anything though.
157 */
158 if (lf) {
159 close(fileno(lf));
160 fclose(lf);
161 lf = NULL;
162 }
163}
164
8382f19b 165void map_add(struct map_ent **melp,
4dd2df09 166 char * devnm, char *metadata, int uuid[4], char *path)
8382f19b 167{
503975b9 168 struct map_ent *me = xmalloc(sizeof(*me));
8382f19b 169
48c36537
AS
170 snprintf(me->devnm, sizeof(me->devnm), "%s", devnm);
171 snprintf(me->metadata, sizeof(me->metadata), "%s", metadata);
8382f19b 172 memcpy(me->uuid, uuid, 16);
503975b9 173 me->path = path ? xstrdup(path) : NULL;
8382f19b 174 me->next = *melp;
195254b8 175 me->bad = 0;
8382f19b
NB
176 *melp = me;
177}
178
179void map_read(struct map_ent **melp)
180{
181 FILE *f;
182 char buf[8192];
1158f25e 183 char path[201];
4dd2df09
N
184 int uuid[4];
185 char devnm[32];
1522c538 186 char metadata[30];
8382f19b
NB
187
188 *melp = NULL;
189
9e751dc7 190 f = open_map(MAP_READ);
3a56f223
N
191 if (!f) {
192 RebuildMap();
9e751dc7 193 f = open_map(MAP_READ);
3a56f223 194 }
8382f19b
NB
195 if (!f)
196 return;
197
198 while (fgets(buf, sizeof(buf), f)) {
8615dcff 199 path[0] = 0;
4dd2df09
N
200 if (sscanf(buf, " %s %s %x:%x:%x:%x %200s",
201 devnm, metadata, uuid, uuid+1,
8615dcff 202 uuid+2, uuid+3, path) >= 7) {
4dd2df09 203 map_add(melp, devnm, metadata, uuid, path);
8382f19b
NB
204 }
205 }
206 fclose(f);
207}
208
209void map_free(struct map_ent *map)
210{
211 while (map) {
212 struct map_ent *mp = map;
213 map = mp->next;
214 free(mp->path);
215 free(mp);
216 }
217}
218
4dd2df09 219int map_update(struct map_ent **mpp, char *devnm, char *metadata,
ccd61ebf 220 int uuid[4], char *path)
8382f19b
NB
221{
222 struct map_ent *map, *mp;
223 int rv;
224
225 if (mpp && *mpp)
226 map = *mpp;
227 else
228 map_read(&map);
229
230 for (mp = map ; mp ; mp=mp->next)
4dd2df09 231 if (strcmp(mp->devnm, devnm) == 0) {
48c36537 232 snprintf(mp->metadata, sizeof(mp->metadata), "%s", metadata);
8382f19b
NB
233 memcpy(mp->uuid, uuid, 16);
234 free(mp->path);
503975b9 235 mp->path = path ? xstrdup(path) : NULL;
52f07f57 236 mp->bad = 0;
8382f19b
NB
237 break;
238 }
239 if (!mp)
4dd2df09 240 map_add(&map, devnm, metadata, uuid, path);
a04d5763
N
241 if (mpp)
242 *mpp = NULL;
8382f19b
NB
243 rv = map_write(map);
244 map_free(map);
245 return rv;
246}
247
4dd2df09 248void map_delete(struct map_ent **mapp, char *devnm)
8382f19b
NB
249{
250 struct map_ent *mp;
251
252 if (*mapp == NULL)
253 map_read(mapp);
254
255 for (mp = *mapp; mp; mp = *mapp) {
4dd2df09 256 if (strcmp(mp->devnm, devnm) == 0) {
8382f19b
NB
257 *mapp = mp->next;
258 free(mp->path);
259 free(mp);
260 } else
261 mapp = & mp->next;
262 }
263}
264
4dd2df09 265void map_remove(struct map_ent **mapp, char *devnm)
4eb26970 266{
4dd2df09 267 if (devnm[0] == 0)
4eb26970
DW
268 return;
269
4dd2df09 270 map_delete(mapp, devnm);
4eb26970
DW
271 map_write(*mapp);
272 map_free(*mapp);
531d7991 273 *mapp = NULL;
4eb26970
DW
274}
275
8382f19b
NB
276struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
277{
278 struct map_ent *mp;
279 if (!*map)
280 map_read(map);
281
195254b8
N
282 for (mp = *map ; mp ; mp = mp->next) {
283 if (memcmp(uuid, mp->uuid, 16) != 0)
284 continue;
4dd2df09 285 if (!mddev_busy(mp->devnm)) {
195254b8
N
286 mp->bad = 1;
287 continue;
288 }
289 return mp;
290 }
8382f19b 291 return NULL;
4ccad7b1 292}
8382f19b 293
4dd2df09 294struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
4ccad7b1
N
295{
296 struct map_ent *mp;
f1f3ef7d
LXK
297
298 if (!devnm)
299 return NULL;
300
4ccad7b1
N
301 if (!*map)
302 map_read(map);
303
195254b8 304 for (mp = *map ; mp ; mp = mp->next) {
4dd2df09 305 if (strcmp(mp->devnm, devnm) != 0)
195254b8 306 continue;
4dd2df09 307 if (!mddev_busy(mp->devnm)) {
195254b8
N
308 mp->bad = 1;
309 continue;
310 }
311 return mp;
312 }
4ccad7b1 313 return NULL;
8382f19b 314}
f2e55ecc
N
315
316struct map_ent *map_by_name(struct map_ent **map, char *name)
317{
318 struct map_ent *mp;
319 if (!*map)
320 map_read(map);
321
322 for (mp = *map ; mp ; mp = mp->next) {
8615dcff
N
323 if (!mp->path)
324 continue;
b9ce7ab0 325 if (strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0)
f2e55ecc 326 continue;
b9ce7ab0 327 if (strcmp(mp->path + DEV_MD_DIR_LEN, name) != 0)
195254b8 328 continue;
4dd2df09 329 if (!mddev_busy(mp->devnm)) {
195254b8
N
330 mp->bad = 1;
331 continue;
332 }
333 return mp;
f2e55ecc
N
334 }
335 return NULL;
336}
3a56f223 337
f98d41dd
DW
338/* sets the proper subarray and container_dev according to the metadata
339 * version super_by_fd does this automatically, this routine is meant as
340 * a supplement for guess_super()
341 */
ca145a1e 342static char *get_member_info(struct mdstat_ent *ent)
f98d41dd 343{
4b3644ab 344 char *subarray;
f98d41dd 345
4b3644ab 346 if (!is_mdstat_ent_subarray(ent))
ca145a1e 347 return NULL;
f98d41dd 348
4b3644ab 349 subarray = strrchr(ent->metadata_version, '/');
2b9aa337 350
4b3644ab 351 return subarray + 1;
f98d41dd
DW
352}
353
3a56f223
N
354void RebuildMap(void)
355{
356 struct mdstat_ent *mdstat = mdstat_read(0, 0);
357 struct mdstat_ent *md;
358 struct map_ent *map = NULL;
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) {
21e622f2 364 if (s_gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
360b4636
N
365 homehost = sys_hostname;
366 }
367 }
3a56f223
N
368
369 for (md = mdstat ; md ; md = md->next) {
4dd2df09 370 struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS);
3a56f223
N
371 struct mdinfo *sd;
372
506ffd1e
DW
373 if (!sra)
374 continue;
375
3a56f223 376 for (sd = sra->devs ; sd ; sd = sd->next) {
360b4636 377 char namebuf[100];
3a56f223
N
378 char dn[30];
379 int dfd;
380 int ok;
13db17bd 381 dev_t devid;
3a56f223 382 struct supertype *st;
71204a50 383 char *subarray = NULL;
3a56f223 384 char *path;
a1f976a0 385 struct mdinfo *info;
3a56f223
N
386
387 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
388 dfd = dev_open(dn, O_RDONLY);
389 if (dfd < 0)
390 continue;
391 st = guess_super(dfd);
392 if ( st == NULL)
393 ok = -1;
f98d41dd 394 else {
a1f976a0 395 subarray = get_member_info(md);
3a56f223 396 ok = st->ss->load_super(st, dfd, NULL);
f98d41dd 397 }
3a56f223
N
398 close(dfd);
399 if (ok != 0)
400 continue;
a74e5731
N
401 if (subarray)
402 info = st->ss->container_content(st, subarray);
403 else {
404 info = xmalloc(sizeof(*info));
405 st->ss->getinfo_super(st, info, NULL);
406 }
ccced50f
N
407 if (!info)
408 continue;
a1f976a0 409
4dd2df09
N
410 devid = devnm2devid(md->devnm);
411 path = map_dev(major(devid), minor(devid), 0);
360b4636 412 if (path == NULL ||
b9ce7ab0 413 strncmp(path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0) {
360b4636
N
414 /* We would really like a name that provides
415 * an MD_DEVNAME for udev.
416 * The name needs to be unique both in /dev/md/
417 * and in this mapfile.
7103b9b8 418 * It needs to match what -I or -As would come
360b4636
N
419 * up with.
420 * That means:
1011e834 421 * Check if array is in mdadm.conf
360b4636
N
422 * - if so use that.
423 * determine trustworthy from homehost etc
424 * find a unique name based on metadata name.
1011e834 425 *
360b4636 426 */
2244d1a9
N
427 struct mddev_ident *match = conf_match(st, info,
428 NULL, 0,
429 NULL);
360b4636
N
430 struct stat stb;
431 if (match && match->devname && match->devname[0] == '/') {
432 path = match->devname;
433 if (path[0] != '/') {
b9ce7ab0 434 strcpy(namebuf, DEV_MD_DIR);
360b4636
N
435 strcat(namebuf, path);
436 path = namebuf;
437 }
438 } else {
439 int unum = 0;
440 char *sep = "_";
441 const char *name;
442 int conflict = 1;
443 if ((homehost == NULL ||
444 st->ss->match_home(st, homehost) != 1) &&
445 st->ss->match_home(st, "any") != 1 &&
d7be7d87
JS
446 (require_homehost ||
447 !conf_name_is_free(info->name)))
360b4636
N
448 /* require a numeric suffix */
449 unum = 0;
450 else
451 /* allow name to be used as-is if no conflict */
452 unum = -1;
a1f976a0 453 name = info->name;
360b4636
N
454 if (!*name) {
455 name = st->ss->name;
456 if (!isdigit(name[strlen(name)-1]) &&
457 unum == -1) {
458 unum = 0;
459 sep = "";
460 }
461 }
628cdf19
N
462 if (strchr(name, ':')) {
463 /* Probably a uniquifying
360b4636 464 * hostname prefix. Allow
628cdf19
N
465 * without a suffix, and strip
466 * hostname if it is us.
360b4636 467 */
628cdf19
N
468 if (homehost && unum == -1 &&
469 strncmp(name, homehost,
470 strlen(homehost)) == 0 &&
471 name[strlen(homehost)] == ':')
472 name += strlen(homehost)+1;
360b4636 473 unum = -1;
628cdf19 474 }
360b4636
N
475
476 while (conflict) {
477 if (unum >= 0)
b9ce7ab0 478 sprintf(namebuf, DEV_MD_DIR "%s%s%d",
360b4636
N
479 name, sep, unum);
480 else
b9ce7ab0 481 sprintf(namebuf, DEV_MD_DIR "%s",
360b4636
N
482 name);
483 unum++;
484 if (lstat(namebuf, &stb) != 0 &&
485 (map == NULL ||
486 !map_by_name(&map, namebuf+8)))
487 conflict = 0;
488 }
489 path = namebuf;
490 }
491 }
4dd2df09 492 map_add(&map, md->devnm,
a1f976a0
N
493 info->text_version,
494 info->uuid, path);
3a56f223 495 st->ss->free_super(st);
a1f976a0 496 free(info);
3a56f223
N
497 break;
498 }
8a659c33 499 sysfs_free(sra);
3a56f223 500 }
7bf59f5c
DL
501 /* Only trigger a change if we wrote a new map file */
502 if (map_write(map))
503 for (md = mdstat ; md ; md = md->next) {
4dd2df09 504 struct mdinfo *sra = sysfs_read(-1, md->devnm,
7bf59f5c 505 GET_VERSION);
b526e52d
DW
506 if (sra)
507 sysfs_uevent(sra, "change");
7bf59f5c
DL
508 sysfs_free(sra);
509 }
3a56f223 510 map_free(map);
78fbcc10 511 free_mdstat(mdstat);
3a56f223 512}