]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
2 * mapfile - keep track of uuid <-> array mapping. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2010 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
31 /* The mapfile is used to track arrays being created in --incremental
32 * mode. It particularly allows lookup from UUID to array device, but
33 * also allows the array device name to be easily found.
35 * The map file is line based with space separated fields. The fields are:
36 * Device id - mdX or mdpX where X is a number.
37 * metadata - 0.90 1.0 1.1 1.2 ddf ...
38 * UUID - uuid of the array
39 * path - path where device created: /dev/md/home
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.
57 MAP_DIR
"/" MAP_FILE
".new",
58 MAP_DIR
"/" MAP_FILE
".lock",
62 int mapmode
[3] = { O_RDONLY
, O_RDWR
|O_CREAT
, O_RDWR
|O_CREAT
|O_TRUNC
};
63 char *mapsmode
[3] = { "r", "w", "w"};
65 FILE *open_map(int modenum
)
68 if ((mapmode
[modenum
] & O_CREAT
))
69 /* Attempt to create directory, don't worry about
72 (void)mkdir(mapname
[MAP_DIRNAME
], 0755);
73 fd
= open(mapname
[modenum
], mapmode
[modenum
], 0600);
75 return fdopen(fd
, mapsmode
[modenum
]);
79 int map_write(struct map_ent
*mel
)
84 f
= open_map(MAP_NEW
);
88 for (; mel
; mel
= mel
->next
) {
91 fprintf(f
, "%s ", mel
->devnm
);
92 fprintf(f
, "%s ", mel
->metadata
);
93 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
94 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
95 fprintf(f
, "%s\n", mel
->path
?:"");
104 return rename(mapname
[1],
109 static FILE *lf
= NULL
;
110 int map_lock(struct map_ent
**melp
)
114 lf
= open_map(MAP_LOCK
);
117 if (flock(fileno(lf
), LOCK_EX
) != 0) {
122 if (fstat(fileno(lf
), &buf
) != 0 ||
124 /* The owner of the lock unlinked it,
125 * so we have a lock on a stale file,
138 void map_unlock(struct map_ent
**melp
)
141 /* must unlink before closing the file,
142 * as only the owner of the lock may
153 /* We are forking, so must close the lock file.
154 * Don't risk flushing anything though.
163 void map_add(struct map_ent
**melp
,
164 char * devnm
, char *metadata
, int uuid
[4], char *path
)
166 struct map_ent
*me
= xmalloc(sizeof(*me
));
168 strcpy(me
->devnm
, devnm
);
169 strcpy(me
->metadata
, metadata
);
170 memcpy(me
->uuid
, uuid
, 16);
171 me
->path
= path
? xstrdup(path
) : NULL
;
177 void map_read(struct map_ent
**melp
)
188 f
= open_map(MAP_READ
);
191 f
= open_map(MAP_READ
);
196 while (fgets(buf
, sizeof(buf
), f
)) {
198 if (sscanf(buf
, " %s %s %x:%x:%x:%x %200s",
199 devnm
, metadata
, uuid
, uuid
+1,
200 uuid
+2, uuid
+3, path
) >= 7) {
201 map_add(melp
, devnm
, metadata
, uuid
, path
);
207 void map_free(struct map_ent
*map
)
210 struct map_ent
*mp
= map
;
217 int map_update(struct map_ent
**mpp
, char *devnm
, char *metadata
,
218 int *uuid
, char *path
)
220 struct map_ent
*map
, *mp
;
228 for (mp
= map
; mp
; mp
=mp
->next
)
229 if (strcmp(mp
->devnm
, devnm
) == 0) {
230 strcpy(mp
->metadata
, metadata
);
231 memcpy(mp
->uuid
, uuid
, 16);
233 mp
->path
= path
? xstrdup(path
) : NULL
;
238 map_add(&map
, devnm
, metadata
, uuid
, path
);
246 void map_delete(struct map_ent
**mapp
, char *devnm
)
253 for (mp
= *mapp
; mp
; mp
= *mapp
) {
254 if (strcmp(mp
->devnm
, devnm
) == 0) {
263 void map_remove(struct map_ent
**mapp
, char *devnm
)
268 map_delete(mapp
, devnm
);
273 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
279 for (mp
= *map
; mp
; mp
= mp
->next
) {
280 if (memcmp(uuid
, mp
->uuid
, 16) != 0)
282 if (!mddev_busy(mp
->devnm
)) {
291 struct map_ent
*map_by_devnm(struct map_ent
**map
, char *devnm
)
297 for (mp
= *map
; mp
; mp
= mp
->next
) {
298 if (strcmp(mp
->devnm
, devnm
) != 0)
300 if (!mddev_busy(mp
->devnm
)) {
309 struct map_ent
*map_by_name(struct map_ent
**map
, char *name
)
315 for (mp
= *map
; mp
; mp
= mp
->next
) {
318 if (strncmp(mp
->path
, "/dev/md/", 8) != 0)
320 if (strcmp(mp
->path
+8, name
) != 0)
322 if (!mddev_busy(mp
->devnm
)) {
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()
335 static char *get_member_info(struct mdstat_ent
*ent
)
338 if (ent
->metadata_version
== NULL
||
339 strncmp(ent
->metadata_version
, "external:", 9) != 0)
342 if (is_subarray(&ent
->metadata_version
[9])) {
345 subarray
= strrchr(ent
->metadata_version
, '/');
351 void RebuildMap(void)
353 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
354 struct mdstat_ent
*md
;
355 struct map_ent
*map
= NULL
;
356 int require_homehost
;
357 char sys_hostname
[256];
358 char *homehost
= conf_get_homehost(&require_homehost
);
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
;
367 for (md
= mdstat
; md
; md
= md
->next
) {
368 struct mdinfo
*sra
= sysfs_read(-1, md
->devnm
, GET_DEVS
);
374 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
380 struct supertype
*st
;
381 char *subarray
= NULL
;
385 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
386 dfd
= dev_open(dn
, O_RDONLY
);
389 st
= guess_super(dfd
);
393 subarray
= get_member_info(md
);
394 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
400 info
= st
->ss
->container_content(st
, subarray
);
402 info
= xmalloc(sizeof(*info
));
403 st
->ss
->getinfo_super(st
, info
, NULL
);
408 devid
= devnm2devid(md
->devnm
);
409 path
= map_dev(major(devid
), minor(devid
), 0);
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.
416 * It needs to match what -I or -As would come
419 * Check if array is in mdadm.conf
421 * determine trustworthy from homehost etc
422 * find a unique name based on metadata name.
425 struct mddev_ident
*match
= conf_match(st
, info
,
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
);
441 if ((homehost
== NULL
||
442 st
->ss
->match_home(st
, homehost
) != 1) &&
443 st
->ss
->match_home(st
, "any") != 1 &&
445 || ! conf_name_is_free(info
->name
)))
446 /* require a numeric suffix */
449 /* allow name to be used as-is if no conflict */
454 if (!isdigit(name
[strlen(name
)-1]) &&
460 if (strchr(name
, ':'))
461 /* probably a uniquifying
462 * hostname prefix. Allow
469 sprintf(namebuf
, "/dev/md/%s%s%d",
472 sprintf(namebuf
, "/dev/md/%s",
475 if (lstat(namebuf
, &stb
) != 0 &&
477 !map_by_name(&map
, namebuf
+8)))
483 map_add(&map
, md
->devnm
,
486 st
->ss
->free_super(st
);
492 /* Only trigger a change if we wrote a new map file */
494 for (md
= mdstat
; md
; md
= md
->next
) {
495 struct mdinfo
*sra
= sysfs_read(-1, md
->devnm
,
498 sysfs_uevent(sra
, "change");