]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mapfile.c
Create.c: fix uclibc build
[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
df65ac70
N
49#define MAP_READ 0
50#define MAP_NEW 1
51#define MAP_LOCK 2
52#define MAP_DIRNAME 3
9e751dc7
N
53
54char *mapname[4] = {
55 MAP_DIR "/" MAP_FILE,
56 MAP_DIR "/" MAP_FILE ".new",
57 MAP_DIR "/" MAP_FILE ".lock",
58 MAP_DIR
cf3a3d78 59};
8382f19b 60
753cf905 61int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
cf3a3d78 62char *mapsmode[3] = { "r", "w", "w"};
8382f19b 63
9e751dc7 64FILE *open_map(int modenum)
cf3a3d78 65{
9e751dc7
N
66 int fd;
67 if ((mapmode[modenum] & O_CREAT))
68 /* Attempt to create directory, don't worry about
69 * failure.
70 */
71 (void)mkdir(mapname[MAP_DIRNAME], 0755);
72 fd = open(mapname[modenum], mapmode[modenum], 0600);
73 if (fd >= 0)
74 return fdopen(fd, mapsmode[modenum]);
cf3a3d78
N
75 return NULL;
76}
8382f19b
NB
77
78int map_write(struct map_ent *mel)
79{
80 FILE *f;
81 int err;
cf3a3d78 82
9e751dc7 83 f = open_map(MAP_NEW);
8382f19b 84
8382f19b
NB
85 if (!f)
86 return 0;
195254b8
N
87 for (; mel; mel = mel->next) {
88 if (mel->bad)
89 continue;
4dd2df09 90 fprintf(f, "%s ", mel->devnm);
1522c538 91 fprintf(f, "%s ", mel->metadata);
8382f19b
NB
92 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
93 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
8615dcff 94 fprintf(f, "%s\n", mel->path?:"");
8382f19b
NB
95 }
96 fflush(f);
97 err = ferror(f);
98 fclose(f);
99 if (err) {
9e751dc7 100 unlink(mapname[1]);
8382f19b
NB
101 return 0;
102 }
9e751dc7
N
103 return rename(mapname[1],
104 mapname[0]) == 0;
8382f19b
NB
105}
106
cf3a3d78 107static FILE *lf = NULL;
ad5bc697
N
108int map_lock(struct map_ent **melp)
109{
08415c46
N
110 while (lf == NULL) {
111 struct stat buf;
9e751dc7 112 lf = open_map(MAP_LOCK);
cf3a3d78 113 if (lf == NULL)
ad5bc697 114 return -1;
fc7e81e5 115 if (flock(fileno(lf), LOCK_EX) != 0) {
cf3a3d78
N
116 fclose(lf);
117 lf = NULL;
ad5bc697
N
118 return -1;
119 }
08415c46
N
120 if (fstat(fileno(lf), &buf) != 0 ||
121 buf.st_nlink == 0) {
122 /* The owner of the lock unlinked it,
123 * so we have a lock on a stale file,
124 * try again
125 */
126 fclose(lf);
127 lf = NULL;
128 }
ad5bc697
N
129 }
130 if (*melp)
131 map_free(*melp);
132 map_read(melp);
133 return 0;
134}
135
136void map_unlock(struct map_ent **melp)
137{
fc7e81e5 138 if (lf) {
08415c46
N
139 /* must unlink before closing the file,
140 * as only the owner of the lock may
141 * unlink the file
142 */
9e751dc7 143 unlink(mapname[2]);
cf3a3d78 144 fclose(lf);
fc7e81e5 145 }
898bd1ec
GJ
146 if (*melp)
147 map_free(*melp);
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];
1158f25e 181 char path[201];
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,
ccd61ebf 218 int uuid[4], char *path)
8382f19b
NB
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);
531d7991 271 *mapp = NULL;
4eb26970
DW
272}
273
8382f19b
NB
274struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
275{
276 struct map_ent *mp;
277 if (!*map)
278 map_read(map);
279
195254b8
N
280 for (mp = *map ; mp ; mp = mp->next) {
281 if (memcmp(uuid, mp->uuid, 16) != 0)
282 continue;
4dd2df09 283 if (!mddev_busy(mp->devnm)) {
195254b8
N
284 mp->bad = 1;
285 continue;
286 }
287 return mp;
288 }
8382f19b 289 return NULL;
4ccad7b1 290}
8382f19b 291
4dd2df09 292struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
4ccad7b1
N
293{
294 struct map_ent *mp;
f1f3ef7d
LXK
295
296 if (!devnm)
297 return NULL;
298
4ccad7b1
N
299 if (!*map)
300 map_read(map);
301
195254b8 302 for (mp = *map ; mp ; mp = mp->next) {
4dd2df09 303 if (strcmp(mp->devnm, devnm) != 0)
195254b8 304 continue;
4dd2df09 305 if (!mddev_busy(mp->devnm)) {
195254b8
N
306 mp->bad = 1;
307 continue;
308 }
309 return mp;
310 }
4ccad7b1 311 return NULL;
8382f19b 312}
f2e55ecc
N
313
314struct map_ent *map_by_name(struct map_ent **map, char *name)
315{
316 struct map_ent *mp;
317 if (!*map)
318 map_read(map);
319
320 for (mp = *map ; mp ; mp = mp->next) {
8615dcff
N
321 if (!mp->path)
322 continue;
b9ce7ab0 323 if (strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0)
f2e55ecc 324 continue;
b9ce7ab0 325 if (strcmp(mp->path + DEV_MD_DIR_LEN, name) != 0)
195254b8 326 continue;
4dd2df09 327 if (!mddev_busy(mp->devnm)) {
195254b8
N
328 mp->bad = 1;
329 continue;
330 }
331 return mp;
f2e55ecc
N
332 }
333 return NULL;
334}
3a56f223 335
f98d41dd
DW
336/* sets the proper subarray and container_dev according to the metadata
337 * version super_by_fd does this automatically, this routine is meant as
338 * a supplement for guess_super()
339 */
ca145a1e 340static char *get_member_info(struct mdstat_ent *ent)
f98d41dd 341{
f98d41dd 342
2b9aa337
N
343 if (ent->metadata_version == NULL ||
344 strncmp(ent->metadata_version, "external:", 9) != 0)
ca145a1e 345 return NULL;
f98d41dd 346
2b9aa337 347 if (is_subarray(&ent->metadata_version[9])) {
2b9aa337 348 char *subarray;
2b9aa337 349
ca145a1e
N
350 subarray = strrchr(ent->metadata_version, '/');
351 return subarray + 1;
f98d41dd 352 }
ca145a1e 353 return NULL;
f98d41dd
DW
354}
355
3a56f223
N
356void RebuildMap(void)
357{
358 struct mdstat_ent *mdstat = mdstat_read(0, 0);
359 struct mdstat_ent *md;
360 struct map_ent *map = NULL;
360b4636
N
361 int require_homehost;
362 char sys_hostname[256];
363 char *homehost = conf_get_homehost(&require_homehost);
364
365 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
21e622f2 366 if (s_gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
360b4636
N
367 homehost = sys_hostname;
368 }
369 }
3a56f223
N
370
371 for (md = mdstat ; md ; md = md->next) {
4dd2df09 372 struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS);
3a56f223
N
373 struct mdinfo *sd;
374
506ffd1e
DW
375 if (!sra)
376 continue;
377
3a56f223 378 for (sd = sra->devs ; sd ; sd = sd->next) {
360b4636 379 char namebuf[100];
3a56f223
N
380 char dn[30];
381 int dfd;
382 int ok;
13db17bd 383 dev_t devid;
3a56f223 384 struct supertype *st;
71204a50 385 char *subarray = NULL;
3a56f223 386 char *path;
a1f976a0 387 struct mdinfo *info;
3a56f223
N
388
389 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
390 dfd = dev_open(dn, O_RDONLY);
391 if (dfd < 0)
392 continue;
393 st = guess_super(dfd);
394 if ( st == NULL)
395 ok = -1;
f98d41dd 396 else {
a1f976a0 397 subarray = get_member_info(md);
3a56f223 398 ok = st->ss->load_super(st, dfd, NULL);
f98d41dd 399 }
3a56f223
N
400 close(dfd);
401 if (ok != 0)
402 continue;
a74e5731
N
403 if (subarray)
404 info = st->ss->container_content(st, subarray);
405 else {
406 info = xmalloc(sizeof(*info));
407 st->ss->getinfo_super(st, info, NULL);
408 }
ccced50f
N
409 if (!info)
410 continue;
a1f976a0 411
4dd2df09
N
412 devid = devnm2devid(md->devnm);
413 path = map_dev(major(devid), minor(devid), 0);
360b4636 414 if (path == NULL ||
b9ce7ab0 415 strncmp(path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0) {
360b4636
N
416 /* We would really like a name that provides
417 * an MD_DEVNAME for udev.
418 * The name needs to be unique both in /dev/md/
419 * and in this mapfile.
7103b9b8 420 * It needs to match what -I or -As would come
360b4636
N
421 * up with.
422 * That means:
1011e834 423 * Check if array is in mdadm.conf
360b4636
N
424 * - if so use that.
425 * determine trustworthy from homehost etc
426 * find a unique name based on metadata name.
1011e834 427 *
360b4636 428 */
2244d1a9
N
429 struct mddev_ident *match = conf_match(st, info,
430 NULL, 0,
431 NULL);
360b4636
N
432 struct stat stb;
433 if (match && match->devname && match->devname[0] == '/') {
434 path = match->devname;
435 if (path[0] != '/') {
b9ce7ab0 436 strcpy(namebuf, DEV_MD_DIR);
360b4636
N
437 strcat(namebuf, path);
438 path = namebuf;
439 }
440 } else {
441 int unum = 0;
442 char *sep = "_";
443 const char *name;
444 int conflict = 1;
445 if ((homehost == NULL ||
446 st->ss->match_home(st, homehost) != 1) &&
447 st->ss->match_home(st, "any") != 1 &&
d7be7d87
JS
448 (require_homehost ||
449 !conf_name_is_free(info->name)))
360b4636
N
450 /* require a numeric suffix */
451 unum = 0;
452 else
453 /* allow name to be used as-is if no conflict */
454 unum = -1;
a1f976a0 455 name = info->name;
360b4636
N
456 if (!*name) {
457 name = st->ss->name;
458 if (!isdigit(name[strlen(name)-1]) &&
459 unum == -1) {
460 unum = 0;
461 sep = "";
462 }
463 }
628cdf19
N
464 if (strchr(name, ':')) {
465 /* Probably a uniquifying
360b4636 466 * hostname prefix. Allow
628cdf19
N
467 * without a suffix, and strip
468 * hostname if it is us.
360b4636 469 */
628cdf19
N
470 if (homehost && unum == -1 &&
471 strncmp(name, homehost,
472 strlen(homehost)) == 0 &&
473 name[strlen(homehost)] == ':')
474 name += strlen(homehost)+1;
360b4636 475 unum = -1;
628cdf19 476 }
360b4636
N
477
478 while (conflict) {
479 if (unum >= 0)
b9ce7ab0 480 sprintf(namebuf, DEV_MD_DIR "%s%s%d",
360b4636
N
481 name, sep, unum);
482 else
b9ce7ab0 483 sprintf(namebuf, DEV_MD_DIR "%s",
360b4636
N
484 name);
485 unum++;
486 if (lstat(namebuf, &stb) != 0 &&
487 (map == NULL ||
488 !map_by_name(&map, namebuf+8)))
489 conflict = 0;
490 }
491 path = namebuf;
492 }
493 }
4dd2df09 494 map_add(&map, md->devnm,
a1f976a0
N
495 info->text_version,
496 info->uuid, path);
3a56f223 497 st->ss->free_super(st);
a1f976a0 498 free(info);
3a56f223
N
499 break;
500 }
8a659c33 501 sysfs_free(sra);
3a56f223 502 }
7bf59f5c
DL
503 /* Only trigger a change if we wrote a new map file */
504 if (map_write(map))
505 for (md = mdstat ; md ; md = md->next) {
4dd2df09 506 struct mdinfo *sra = sysfs_read(-1, md->devnm,
7bf59f5c 507 GET_VERSION);
b526e52d
DW
508 if (sra)
509 sysfs_uevent(sra, "change");
7bf59f5c
DL
510 sysfs_free(sra);
511 }
3a56f223 512 map_free(map);
78fbcc10 513 free_mdstat(mdstat);
3a56f223 514}