]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mapfile.c
Provide a mdstat_ent to subarray helper
[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 *
9e751dc7
N
41 * The best place for the mapfile wold be /var/run/mdadm/map. However
42 * it is needed during initramfs early-boot, and /var/run doesn't exist there
43 * and certainly doesn't persist through to normal boot.
44 * So we store it in /dev/.mdadm/map but allow this to be changed at
45 * compile time. via MAP_DIR and MAP_FILE
46 *
8382f19b 47 */
360b4636 48#include "mdadm.h"
fc7e81e5 49#include <sys/file.h>
360b4636 50#include <ctype.h>
8382f19b 51
9e751dc7
N
52#ifndef MAP_DIR
53#define MAP_DIR "/dev/.mdadm"
54#define MAP_FILE "map"
55#endif
56
df65ac70
N
57#define MAP_READ 0
58#define MAP_NEW 1
59#define MAP_LOCK 2
60#define MAP_DIRNAME 3
61#define mapnames(dir, base) { \
9e751dc7
N
62
63char *mapname[4] = {
64 MAP_DIR "/" MAP_FILE,
65 MAP_DIR "/" MAP_FILE ".new",
66 MAP_DIR "/" MAP_FILE ".lock",
67 MAP_DIR
cf3a3d78 68};
8382f19b 69
753cf905 70int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
cf3a3d78 71char *mapsmode[3] = { "r", "w", "w"};
8382f19b 72
9e751dc7 73FILE *open_map(int modenum)
cf3a3d78 74{
9e751dc7
N
75 int fd;
76 if ((mapmode[modenum] & O_CREAT))
77 /* Attempt to create directory, don't worry about
78 * failure.
79 */
80 (void)mkdir(mapname[MAP_DIRNAME], 0755);
81 fd = open(mapname[modenum], mapmode[modenum], 0600);
82 if (fd >= 0)
83 return fdopen(fd, mapsmode[modenum]);
cf3a3d78
N
84 return NULL;
85}
8382f19b
NB
86
87int map_write(struct map_ent *mel)
88{
89 FILE *f;
90 int err;
cf3a3d78 91
9e751dc7 92 f = open_map(MAP_NEW);
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) {
9e751dc7 112 unlink(mapname[1]);
8382f19b
NB
113 return 0;
114 }
9e751dc7
N
115 return rename(mapname[1],
116 mapname[0]) == 0;
8382f19b
NB
117}
118
ad5bc697 119
cf3a3d78 120static FILE *lf = NULL;
ad5bc697
N
121int map_lock(struct map_ent **melp)
122{
08415c46
N
123 while (lf == NULL) {
124 struct stat buf;
9e751dc7 125 lf = open_map(MAP_LOCK);
cf3a3d78 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 }
08415c46
N
133 if (fstat(fileno(lf), &buf) != 0 ||
134 buf.st_nlink == 0) {
135 /* The owner of the lock unlinked it,
136 * so we have a lock on a stale file,
137 * try again
138 */
139 fclose(lf);
140 lf = NULL;
141 }
ad5bc697
N
142 }
143 if (*melp)
144 map_free(*melp);
145 map_read(melp);
146 return 0;
147}
148
149void map_unlock(struct map_ent **melp)
150{
fc7e81e5 151 if (lf) {
08415c46
N
152 /* must unlink before closing the file,
153 * as only the owner of the lock may
154 * unlink the file
155 */
9e751dc7 156 unlink(mapname[2]);
cf3a3d78 157 fclose(lf);
fc7e81e5 158 }
cf3a3d78 159 lf = NULL;
ad5bc697
N
160}
161
8382f19b 162void map_add(struct map_ent **melp,
1522c538 163 int devnum, char *metadata, int uuid[4], char *path)
8382f19b
NB
164{
165 struct map_ent *me = malloc(sizeof(*me));
166
167 me->devnum = devnum;
1522c538 168 strcpy(me->metadata, metadata);
8382f19b 169 memcpy(me->uuid, uuid, 16);
8615dcff 170 me->path = path ? strdup(path) : NULL;
8382f19b 171 me->next = *melp;
195254b8 172 me->bad = 0;
8382f19b
NB
173 *melp = me;
174}
175
176void map_read(struct map_ent **melp)
177{
178 FILE *f;
179 char buf[8192];
180 char path[200];
1522c538
NB
181 int devnum, uuid[4];
182 char metadata[30];
8382f19b
NB
183 char nam[4];
184
185 *melp = NULL;
186
9e751dc7 187 f = open_map(MAP_READ);
3a56f223
N
188 if (!f) {
189 RebuildMap();
9e751dc7 190 f = open_map(MAP_READ);
3a56f223 191 }
8382f19b
NB
192 if (!f)
193 return;
194
195 while (fgets(buf, sizeof(buf), f)) {
8615dcff 196 path[0] = 0;
c5afc314 197 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
1522c538 198 nam, &devnum, metadata, uuid, uuid+1,
8615dcff 199 uuid+2, uuid+3, path) >= 7) {
c5afc314
N
200 if (strncmp(nam, "md", 2) != 0)
201 continue;
202 if (nam[2] == 'p')
8382f19b 203 devnum = -1 - devnum;
1522c538 204 map_add(melp, devnum, metadata, uuid, path);
8382f19b
NB
205 }
206 }
207 fclose(f);
208}
209
210void map_free(struct map_ent *map)
211{
212 while (map) {
213 struct map_ent *mp = map;
214 map = mp->next;
215 free(mp->path);
216 free(mp);
217 }
218}
219
1522c538 220int map_update(struct map_ent **mpp, int devnum, char *metadata,
8382f19b
NB
221 int *uuid, char *path)
222{
223 struct map_ent *map, *mp;
224 int rv;
225
226 if (mpp && *mpp)
227 map = *mpp;
228 else
229 map_read(&map);
230
231 for (mp = map ; mp ; mp=mp->next)
232 if (mp->devnum == devnum) {
1522c538 233 strcpy(mp->metadata, metadata);
8382f19b
NB
234 memcpy(mp->uuid, uuid, 16);
235 free(mp->path);
8615dcff 236 mp->path = path ? strdup(path) : NULL;
8382f19b
NB
237 break;
238 }
239 if (!mp)
1522c538 240 map_add(&map, devnum, 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
248void map_delete(struct map_ent **mapp, int devnum)
249{
250 struct map_ent *mp;
251
252 if (*mapp == NULL)
253 map_read(mapp);
254
255 for (mp = *mapp; mp; mp = *mapp) {
256 if (mp->devnum == devnum) {
257 *mapp = mp->next;
258 free(mp->path);
259 free(mp);
260 } else
261 mapp = & mp->next;
262 }
263}
264
4eb26970
DW
265void map_remove(struct map_ent **mapp, int devnum)
266{
267 if (devnum == NoMdDev)
268 return;
269
270 map_delete(mapp, devnum);
271 map_write(*mapp);
272 map_free(*mapp);
273}
274
8382f19b
NB
275struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
276{
277 struct map_ent *mp;
278 if (!*map)
279 map_read(map);
280
195254b8
N
281 for (mp = *map ; mp ; mp = mp->next) {
282 if (memcmp(uuid, mp->uuid, 16) != 0)
283 continue;
284 if (!mddev_busy(mp->devnum)) {
285 mp->bad = 1;
286 continue;
287 }
288 return mp;
289 }
8382f19b 290 return NULL;
4ccad7b1 291}
8382f19b 292
4ccad7b1
N
293struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
294{
295 struct map_ent *mp;
296 if (!*map)
297 map_read(map);
298
195254b8
N
299 for (mp = *map ; mp ; mp = mp->next) {
300 if (mp->devnum != devnum)
301 continue;
302 if (!mddev_busy(mp->devnum)) {
303 mp->bad = 1;
304 continue;
305 }
306 return mp;
307 }
4ccad7b1 308 return NULL;
8382f19b 309}
f2e55ecc
N
310
311struct map_ent *map_by_name(struct map_ent **map, char *name)
312{
313 struct map_ent *mp;
314 if (!*map)
315 map_read(map);
316
317 for (mp = *map ; mp ; mp = mp->next) {
8615dcff
N
318 if (!mp->path)
319 continue;
f2e55ecc
N
320 if (strncmp(mp->path, "/dev/md/", 8) != 0)
321 continue;
195254b8
N
322 if (strcmp(mp->path+8, name) != 0)
323 continue;
324 if (!mddev_busy(mp->devnum)) {
325 mp->bad = 1;
326 continue;
327 }
328 return mp;
f2e55ecc
N
329 }
330 return NULL;
331}
3a56f223 332
f98d41dd
DW
333/* sets the proper subarray and container_dev according to the metadata
334 * version super_by_fd does this automatically, this routine is meant as
335 * a supplement for guess_super()
336 */
ca145a1e 337static char *get_member_info(struct mdstat_ent *ent)
f98d41dd 338{
f98d41dd 339
2b9aa337
N
340 if (ent->metadata_version == NULL ||
341 strncmp(ent->metadata_version, "external:", 9) != 0)
ca145a1e 342 return NULL;
f98d41dd 343
2b9aa337 344 if (is_subarray(&ent->metadata_version[9])) {
2b9aa337 345 char *subarray;
2b9aa337 346
ca145a1e
N
347 subarray = strrchr(ent->metadata_version, '/');
348 return subarray + 1;
f98d41dd 349 }
ca145a1e 350 return NULL;
f98d41dd
DW
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) {
b526e52d 371 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_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;
a1f976a0 383 char *subarray;
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;
a1f976a0
N
401 info = st->ss->container_content(st, subarray);
402
60f8cb9b 403 if (md->devnum >= 0)
3a56f223
N
404 path = map_dev(MD_MAJOR, md->devnum, 0);
405 else
406 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
360b4636
N
407 if (path == NULL ||
408 strncmp(path, "/dev/md/", 8) != 0) {
409 /* We would really like a name that provides
410 * an MD_DEVNAME for udev.
411 * The name needs to be unique both in /dev/md/
412 * and in this mapfile.
413 * It needs to match watch -I or -As would come
414 * up with.
415 * That means:
416 * Check if array is in mdadm.conf
417 * - if so use that.
418 * determine trustworthy from homehost etc
419 * find a unique name based on metadata name.
420 *
421 */
fa56eddb 422 struct mddev_ident *match = conf_match(info, st);
360b4636
N
423 struct stat stb;
424 if (match && match->devname && match->devname[0] == '/') {
425 path = match->devname;
426 if (path[0] != '/') {
427 strcpy(namebuf, "/dev/md/");
428 strcat(namebuf, path);
429 path = namebuf;
430 }
431 } else {
432 int unum = 0;
433 char *sep = "_";
434 const char *name;
435 int conflict = 1;
436 if ((homehost == NULL ||
437 st->ss->match_home(st, homehost) != 1) &&
438 st->ss->match_home(st, "any") != 1 &&
439 (require_homehost
a1f976a0 440 || ! conf_name_is_free(info->name)))
360b4636
N
441 /* require a numeric suffix */
442 unum = 0;
443 else
444 /* allow name to be used as-is if no conflict */
445 unum = -1;
a1f976a0 446 name = info->name;
360b4636
N
447 if (!*name) {
448 name = st->ss->name;
449 if (!isdigit(name[strlen(name)-1]) &&
450 unum == -1) {
451 unum = 0;
452 sep = "";
453 }
454 }
455 if (strchr(name, ':'))
456 /* probably a uniquifying
457 * hostname prefix. Allow
458 * without a suffix
459 */
460 unum = -1;
461
462 while (conflict) {
463 if (unum >= 0)
464 sprintf(namebuf, "/dev/md/%s%s%d",
465 name, sep, unum);
466 else
467 sprintf(namebuf, "/dev/md/%s",
468 name);
469 unum++;
470 if (lstat(namebuf, &stb) != 0 &&
471 (map == NULL ||
472 !map_by_name(&map, namebuf+8)))
473 conflict = 0;
474 }
475 path = namebuf;
476 }
477 }
8a659c33 478 map_add(&map, md->devnum,
a1f976a0
N
479 info->text_version,
480 info->uuid, path);
3a56f223 481 st->ss->free_super(st);
a1f976a0 482 free(info);
3a56f223
N
483 break;
484 }
8a659c33 485 sysfs_free(sra);
3a56f223 486 }
7bf59f5c
DL
487 /* Only trigger a change if we wrote a new map file */
488 if (map_write(map))
489 for (md = mdstat ; md ; md = md->next) {
490 struct mdinfo *sra = sysfs_read(-1, md->devnum,
491 GET_VERSION);
b526e52d
DW
492 if (sra)
493 sysfs_uevent(sra, "change");
7bf59f5c
DL
494 sysfs_free(sra);
495 }
3a56f223 496 map_free(map);
78fbcc10 497 free_mdstat(mdstat);
3a56f223 498}