]>
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
) {
92 fprintf(f
, "mdp%d ", -1-mel
->devnum
);
94 fprintf(f
, "md%d ", mel
->devnum
);
95 fprintf(f
, "%s ", mel
->metadata
);
96 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
97 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
98 fprintf(f
, "%s\n", mel
->path
?:"");
107 return rename(mapname
[1],
112 static FILE *lf
= NULL
;
113 int map_lock(struct map_ent
**melp
)
117 lf
= open_map(MAP_LOCK
);
120 if (flock(fileno(lf
), LOCK_EX
) != 0) {
125 if (fstat(fileno(lf
), &buf
) != 0 ||
127 /* The owner of the lock unlinked it,
128 * so we have a lock on a stale file,
141 void map_unlock(struct map_ent
**melp
)
144 /* must unlink before closing the file,
145 * as only the owner of the lock may
156 /* We are forking, so must close the lock file.
157 * Don't risk flushing anything though.
166 void map_add(struct map_ent
**melp
,
167 int devnum
, char *metadata
, int uuid
[4], char *path
)
169 struct map_ent
*me
= malloc(sizeof(*me
));
172 strcpy(me
->metadata
, metadata
);
173 memcpy(me
->uuid
, uuid
, 16);
174 me
->path
= path
? strdup(path
) : NULL
;
180 void map_read(struct map_ent
**melp
)
191 f
= open_map(MAP_READ
);
194 f
= open_map(MAP_READ
);
199 while (fgets(buf
, sizeof(buf
), f
)) {
201 if (sscanf(buf
, " %3[mdp]%d %s %x:%x:%x:%x %200s",
202 nam
, &devnum
, metadata
, uuid
, uuid
+1,
203 uuid
+2, uuid
+3, path
) >= 7) {
204 if (strncmp(nam
, "md", 2) != 0)
207 devnum
= -1 - devnum
;
208 map_add(melp
, devnum
, metadata
, uuid
, path
);
214 void map_free(struct map_ent
*map
)
217 struct map_ent
*mp
= map
;
224 int map_update(struct map_ent
**mpp
, int devnum
, char *metadata
,
225 int *uuid
, char *path
)
227 struct map_ent
*map
, *mp
;
235 for (mp
= map
; mp
; mp
=mp
->next
)
236 if (mp
->devnum
== devnum
) {
237 strcpy(mp
->metadata
, metadata
);
238 memcpy(mp
->uuid
, uuid
, 16);
240 mp
->path
= path
? strdup(path
) : NULL
;
245 map_add(&map
, devnum
, metadata
, uuid
, path
);
253 void map_delete(struct map_ent
**mapp
, int devnum
)
260 for (mp
= *mapp
; mp
; mp
= *mapp
) {
261 if (mp
->devnum
== devnum
) {
270 void map_remove(struct map_ent
**mapp
, int devnum
)
272 if (devnum
== NoMdDev
)
275 map_delete(mapp
, devnum
);
280 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
286 for (mp
= *map
; mp
; mp
= mp
->next
) {
287 if (memcmp(uuid
, mp
->uuid
, 16) != 0)
289 if (!mddev_busy(mp
->devnum
)) {
298 struct map_ent
*map_by_devnum(struct map_ent
**map
, int devnum
)
304 for (mp
= *map
; mp
; mp
= mp
->next
) {
305 if (mp
->devnum
!= devnum
)
307 if (!mddev_busy(mp
->devnum
)) {
316 struct map_ent
*map_by_name(struct map_ent
**map
, char *name
)
322 for (mp
= *map
; mp
; mp
= mp
->next
) {
325 if (strncmp(mp
->path
, "/dev/md/", 8) != 0)
327 if (strcmp(mp
->path
+8, name
) != 0)
329 if (!mddev_busy(mp
->devnum
)) {
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()
342 static char *get_member_info(struct mdstat_ent
*ent
)
345 if (ent
->metadata_version
== NULL
||
346 strncmp(ent
->metadata_version
, "external:", 9) != 0)
349 if (is_subarray(&ent
->metadata_version
[9])) {
352 subarray
= strrchr(ent
->metadata_version
, '/');
358 void RebuildMap(void)
360 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
361 struct mdstat_ent
*md
;
362 struct map_ent
*map
= NULL
;
363 int mdp
= get_mdp_major();
364 int require_homehost
;
365 char sys_hostname
[256];
366 char *homehost
= conf_get_homehost(&require_homehost
);
368 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
369 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
370 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
371 homehost
= sys_hostname
;
375 for (md
= mdstat
; md
; md
= md
->next
) {
376 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
382 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
387 struct supertype
*st
;
388 char *subarray
= NULL
;
392 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
393 dfd
= dev_open(dn
, O_RDONLY
);
396 st
= guess_super(dfd
);
400 subarray
= get_member_info(md
);
401 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
406 info
= st
->ss
->container_content(st
, subarray
);
411 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
413 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
415 strncmp(path
, "/dev/md/", 8) != 0) {
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.
420 * It needs to match watch -I or -As would come
423 * Check if array is in mdadm.conf
425 * determine trustworthy from homehost etc
426 * find a unique name based on metadata name.
429 struct mddev_ident
*match
= conf_match(st
, info
,
433 if (match
&& match
->devname
&& match
->devname
[0] == '/') {
434 path
= match
->devname
;
435 if (path
[0] != '/') {
436 strcpy(namebuf
, "/dev/md/");
437 strcat(namebuf
, path
);
445 if ((homehost
== NULL
||
446 st
->ss
->match_home(st
, homehost
) != 1) &&
447 st
->ss
->match_home(st
, "any") != 1 &&
449 || ! conf_name_is_free(info
->name
)))
450 /* require a numeric suffix */
453 /* allow name to be used as-is if no conflict */
458 if (!isdigit(name
[strlen(name
)-1]) &&
464 if (strchr(name
, ':'))
465 /* probably a uniquifying
466 * hostname prefix. Allow
473 sprintf(namebuf
, "/dev/md/%s%s%d",
476 sprintf(namebuf
, "/dev/md/%s",
479 if (lstat(namebuf
, &stb
) != 0 &&
481 !map_by_name(&map
, namebuf
+8)))
487 map_add(&map
, md
->devnum
,
490 st
->ss
->free_super(st
);
496 /* Only trigger a change if we wrote a new map file */
498 for (md
= mdstat
; md
; md
= md
->next
) {
499 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
,
502 sysfs_uevent(sra
, "change");