]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
601c4ccf378b53ae185e326b45aa6f884be167e3
2 * mapfile - manage /var/run/mdadm.map. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 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 /* /var/run/mdadm.map is used to track arrays being created in --incremental
32 * more. 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 preferred location for the map file is /var/run/mdadm.map.
42 * However /var/run may not exist or be writable in early boot. And if
43 * no-one has created /var/run/mdadm, we still want to survive.
44 * So possible locations are:
45 * /var/run/mdadm/map /var/run/mdadm.map /dev/.mdadm.map
46 * the last, because udev requires a writable /dev very early.
47 * We read from the first one that exists and write to the first
53 #define mapnames(base) { #base, #base ".new", #base ".lock"}
54 char *mapname
[3][3] = {
55 mapnames(/var
/run
/mdadm
/map
),
56 mapnames(/var
/run
/mdadm
.map
),
57 mapnames(/dev
/.mdadm
.map
)
60 int mapmode
[3] = { O_RDONLY
, O_RDWR
|O_CREAT
, O_RDWR
|O_CREAT
| O_TRUNC
};
61 char *mapsmode
[3] = { "r", "w", "w"};
63 FILE *open_map(int modenum
, int *choice
)
66 for (i
= 0 ; i
< 3 ; i
++) {
67 int fd
= open(mapname
[i
][modenum
], mapmode
[modenum
], 0600);
70 return fdopen(fd
, mapsmode
[modenum
]);
76 int map_write(struct map_ent
*mel
)
82 f
= open_map(1, &which
);
86 for (; mel
; mel
= mel
->next
) {
90 fprintf(f
, "mdp%d ", -1-mel
->devnum
);
92 fprintf(f
, "md%d ", mel
->devnum
);
93 fprintf(f
, "%s ", mel
->metadata
);
94 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
95 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
96 fprintf(f
, "%s\n", mel
->path
?:"");
102 unlink(mapname
[which
][1]);
105 return rename(mapname
[which
][1],
106 mapname
[which
][0]) == 0;
110 static FILE *lf
= NULL
;
111 static int lwhich
= 0;
112 int map_lock(struct map_ent
**melp
)
115 lf
= open_map(2, &lwhich
);
118 if (lockf(fileno(lf
), F_LOCK
, 0) != 0) {
130 void map_unlock(struct map_ent
**melp
)
134 unlink(mapname
[lwhich
][2]);
138 void map_add(struct map_ent
**melp
,
139 int devnum
, char *metadata
, int uuid
[4], char *path
)
141 struct map_ent
*me
= malloc(sizeof(*me
));
144 strcpy(me
->metadata
, metadata
);
145 memcpy(me
->uuid
, uuid
, 16);
146 me
->path
= path
? strdup(path
) : NULL
;
152 void map_read(struct map_ent
**melp
)
164 f
= open_map(0, &which
);
167 f
= open_map(0, &which
);
172 while (fgets(buf
, sizeof(buf
), f
)) {
174 if (sscanf(buf
, " %3[mdp]%d %s %x:%x:%x:%x %200s",
175 nam
, &devnum
, metadata
, uuid
, uuid
+1,
176 uuid
+2, uuid
+3, path
) >= 7) {
177 if (strncmp(nam
, "md", 2) != 0)
180 devnum
= -1 - devnum
;
181 map_add(melp
, devnum
, metadata
, uuid
, path
);
187 void map_free(struct map_ent
*map
)
190 struct map_ent
*mp
= map
;
197 int map_update(struct map_ent
**mpp
, int devnum
, char *metadata
,
198 int *uuid
, char *path
)
200 struct map_ent
*map
, *mp
;
208 for (mp
= map
; mp
; mp
=mp
->next
)
209 if (mp
->devnum
== devnum
) {
210 strcpy(mp
->metadata
, metadata
);
211 memcpy(mp
->uuid
, uuid
, 16);
213 mp
->path
= path
? strdup(path
) : NULL
;
217 map_add(&map
, devnum
, metadata
, uuid
, path
);
225 void map_delete(struct map_ent
**mapp
, int devnum
)
232 for (mp
= *mapp
; mp
; mp
= *mapp
) {
233 if (mp
->devnum
== devnum
) {
242 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
248 for (mp
= *map
; mp
; mp
= mp
->next
) {
249 if (memcmp(uuid
, mp
->uuid
, 16) != 0)
251 if (!mddev_busy(mp
->devnum
)) {
260 struct map_ent
*map_by_devnum(struct map_ent
**map
, int devnum
)
266 for (mp
= *map
; mp
; mp
= mp
->next
) {
267 if (mp
->devnum
!= devnum
)
269 if (!mddev_busy(mp
->devnum
)) {
278 struct map_ent
*map_by_name(struct map_ent
**map
, char *name
)
284 for (mp
= *map
; mp
; mp
= mp
->next
) {
287 if (strncmp(mp
->path
, "/dev/md/", 8) != 0)
289 if (strcmp(mp
->path
+8, name
) != 0)
291 if (!mddev_busy(mp
->devnum
)) {
300 void RebuildMap(void)
302 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
303 struct mdstat_ent
*md
;
304 struct map_ent
*map
= NULL
;
305 int mdp
= get_mdp_major();
306 int require_homehost
;
307 char sys_hostname
[256];
308 char *homehost
= conf_get_homehost(&require_homehost
);
310 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
311 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
312 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
313 homehost
= sys_hostname
;
317 for (md
= mdstat
; md
; md
= md
->next
) {
318 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
|SKIP_GONE_DEVS
);
324 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
329 struct supertype
*st
;
333 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
334 dfd
= dev_open(dn
, O_RDONLY
);
337 st
= guess_super(dfd
);
341 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
345 st
->ss
->getinfo_super(st
, &info
);
347 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
349 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
351 strncmp(path
, "/dev/md/", 8) != 0) {
352 /* We would really like a name that provides
353 * an MD_DEVNAME for udev.
354 * The name needs to be unique both in /dev/md/
355 * and in this mapfile.
356 * It needs to match watch -I or -As would come
359 * Check if array is in mdadm.conf
361 * determine trustworthy from homehost etc
362 * find a unique name based on metadata name.
365 struct mddev_ident_s
*match
= conf_match(&info
, st
);
367 if (match
&& match
->devname
&& match
->devname
[0] == '/') {
368 path
= match
->devname
;
369 if (path
[0] != '/') {
370 strcpy(namebuf
, "/dev/md/");
371 strcat(namebuf
, path
);
379 if ((homehost
== NULL
||
380 st
->ss
->match_home(st
, homehost
) != 1) &&
381 st
->ss
->match_home(st
, "any") != 1 &&
383 || ! conf_name_is_free(info
.name
)))
384 /* require a numeric suffix */
387 /* allow name to be used as-is if no conflict */
392 if (!isdigit(name
[strlen(name
)-1]) &&
398 if (strchr(name
, ':'))
399 /* probably a uniquifying
400 * hostname prefix. Allow
407 sprintf(namebuf
, "/dev/md/%s%s%d",
410 sprintf(namebuf
, "/dev/md/%s",
413 if (lstat(namebuf
, &stb
) != 0 &&
415 !map_by_name(&map
, namebuf
+8)))
421 map_add(&map
, md
->devnum
,
424 st
->ss
->free_super(st
);
431 for (md
= mdstat
; md
; md
= md
->next
) {
432 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_VERSION
);
433 sysfs_uevent(sra
, "change");