]>
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 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
53 #define MAP_DIR "/dev/.mdadm"
54 #define MAP_FILE "map"
61 #define mapnames(dir, base) { \
65 MAP_DIR
"/" MAP_FILE
".new",
66 MAP_DIR
"/" MAP_FILE
".lock",
70 int mapmode
[3] = { O_RDONLY
, O_RDWR
|O_CREAT
, O_RDWR
|O_CREAT
|O_TRUNC
};
71 char *mapsmode
[3] = { "r", "w", "w"};
73 FILE *open_map(int modenum
)
76 if ((mapmode
[modenum
] & O_CREAT
))
77 /* Attempt to create directory, don't worry about
80 (void)mkdir(mapname
[MAP_DIRNAME
], 0755);
81 fd
= open(mapname
[modenum
], mapmode
[modenum
], 0600);
83 return fdopen(fd
, mapsmode
[modenum
]);
87 int map_write(struct map_ent
*mel
)
92 f
= open_map(MAP_NEW
);
96 for (; mel
; mel
= mel
->next
) {
100 fprintf(f
, "mdp%d ", -1-mel
->devnum
);
102 fprintf(f
, "md%d ", mel
->devnum
);
103 fprintf(f
, "%s ", mel
->metadata
);
104 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
105 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
106 fprintf(f
, "%s\n", mel
->path
?:"");
115 return rename(mapname
[1],
120 static FILE *lf
= NULL
;
121 int map_lock(struct map_ent
**melp
)
125 lf
= open_map(MAP_LOCK
);
128 if (flock(fileno(lf
), LOCK_EX
) != 0) {
133 if (fstat(fileno(lf
), &buf
) != 0 ||
135 /* The owner of the lock unlinked it,
136 * so we have a lock on a stale file,
149 void map_unlock(struct map_ent
**melp
)
152 /* must unlink before closing the file,
153 * as only the owner of the lock may
164 /* We are forking, so must close the lock file.
165 * Don't risk flushing anything though.
174 void map_add(struct map_ent
**melp
,
175 int devnum
, char *metadata
, int uuid
[4], char *path
)
177 struct map_ent
*me
= malloc(sizeof(*me
));
180 strcpy(me
->metadata
, metadata
);
181 memcpy(me
->uuid
, uuid
, 16);
182 me
->path
= path
? strdup(path
) : NULL
;
188 void map_read(struct map_ent
**melp
)
199 f
= open_map(MAP_READ
);
202 f
= open_map(MAP_READ
);
207 while (fgets(buf
, sizeof(buf
), f
)) {
209 if (sscanf(buf
, " %3[mdp]%d %s %x:%x:%x:%x %200s",
210 nam
, &devnum
, metadata
, uuid
, uuid
+1,
211 uuid
+2, uuid
+3, path
) >= 7) {
212 if (strncmp(nam
, "md", 2) != 0)
215 devnum
= -1 - devnum
;
216 map_add(melp
, devnum
, metadata
, uuid
, path
);
222 void map_free(struct map_ent
*map
)
225 struct map_ent
*mp
= map
;
232 int map_update(struct map_ent
**mpp
, int devnum
, char *metadata
,
233 int *uuid
, char *path
)
235 struct map_ent
*map
, *mp
;
243 for (mp
= map
; mp
; mp
=mp
->next
)
244 if (mp
->devnum
== devnum
) {
245 strcpy(mp
->metadata
, metadata
);
246 memcpy(mp
->uuid
, uuid
, 16);
248 mp
->path
= path
? strdup(path
) : NULL
;
253 map_add(&map
, devnum
, metadata
, uuid
, path
);
261 void map_delete(struct map_ent
**mapp
, int devnum
)
268 for (mp
= *mapp
; mp
; mp
= *mapp
) {
269 if (mp
->devnum
== devnum
) {
278 void map_remove(struct map_ent
**mapp
, int devnum
)
280 if (devnum
== NoMdDev
)
283 map_delete(mapp
, devnum
);
288 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
294 for (mp
= *map
; mp
; mp
= mp
->next
) {
295 if (memcmp(uuid
, mp
->uuid
, 16) != 0)
297 if (!mddev_busy(mp
->devnum
)) {
306 struct map_ent
*map_by_devnum(struct map_ent
**map
, int devnum
)
312 for (mp
= *map
; mp
; mp
= mp
->next
) {
313 if (mp
->devnum
!= devnum
)
315 if (!mddev_busy(mp
->devnum
)) {
324 struct map_ent
*map_by_name(struct map_ent
**map
, char *name
)
330 for (mp
= *map
; mp
; mp
= mp
->next
) {
333 if (strncmp(mp
->path
, "/dev/md/", 8) != 0)
335 if (strcmp(mp
->path
+8, name
) != 0)
337 if (!mddev_busy(mp
->devnum
)) {
346 /* sets the proper subarray and container_dev according to the metadata
347 * version super_by_fd does this automatically, this routine is meant as
348 * a supplement for guess_super()
350 static char *get_member_info(struct mdstat_ent
*ent
)
353 if (ent
->metadata_version
== NULL
||
354 strncmp(ent
->metadata_version
, "external:", 9) != 0)
357 if (is_subarray(&ent
->metadata_version
[9])) {
360 subarray
= strrchr(ent
->metadata_version
, '/');
366 void RebuildMap(void)
368 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
369 struct mdstat_ent
*md
;
370 struct map_ent
*map
= NULL
;
371 int mdp
= get_mdp_major();
372 int require_homehost
;
373 char sys_hostname
[256];
374 char *homehost
= conf_get_homehost(&require_homehost
);
376 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
377 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
378 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
379 homehost
= sys_hostname
;
383 for (md
= mdstat
; md
; md
= md
->next
) {
384 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
390 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
395 struct supertype
*st
;
396 char *subarray
= NULL
;
400 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
401 dfd
= dev_open(dn
, O_RDONLY
);
404 st
= guess_super(dfd
);
408 subarray
= get_member_info(md
);
409 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
414 info
= st
->ss
->container_content(st
, subarray
);
417 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
419 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
421 strncmp(path
, "/dev/md/", 8) != 0) {
422 /* We would really like a name that provides
423 * an MD_DEVNAME for udev.
424 * The name needs to be unique both in /dev/md/
425 * and in this mapfile.
426 * It needs to match watch -I or -As would come
429 * Check if array is in mdadm.conf
431 * determine trustworthy from homehost etc
432 * find a unique name based on metadata name.
435 struct mddev_ident
*match
= conf_match(st
, info
,
439 if (match
&& match
->devname
&& match
->devname
[0] == '/') {
440 path
= match
->devname
;
441 if (path
[0] != '/') {
442 strcpy(namebuf
, "/dev/md/");
443 strcat(namebuf
, path
);
451 if ((homehost
== NULL
||
452 st
->ss
->match_home(st
, homehost
) != 1) &&
453 st
->ss
->match_home(st
, "any") != 1 &&
455 || ! conf_name_is_free(info
->name
)))
456 /* require a numeric suffix */
459 /* allow name to be used as-is if no conflict */
464 if (!isdigit(name
[strlen(name
)-1]) &&
470 if (strchr(name
, ':'))
471 /* probably a uniquifying
472 * hostname prefix. Allow
479 sprintf(namebuf
, "/dev/md/%s%s%d",
482 sprintf(namebuf
, "/dev/md/%s",
485 if (lstat(namebuf
, &stb
) != 0 &&
487 !map_by_name(&map
, namebuf
+8)))
493 map_add(&map
, md
->devnum
,
496 st
->ss
->free_super(st
);
502 /* Only trigger a change if we wrote a new map file */
504 for (md
= mdstat
; md
; md
= md
->next
) {
505 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
,
508 sysfs_uevent(sra
, "change");