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