]>
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.
56 MAP_DIR
"/" MAP_FILE
".new",
57 MAP_DIR
"/" MAP_FILE
".lock",
61 int mapmode
[3] = { O_RDONLY
, O_RDWR
|O_CREAT
, O_RDWR
|O_CREAT
|O_TRUNC
};
62 char *mapsmode
[3] = { "r", "w", "w"};
64 FILE *open_map(int modenum
)
67 if ((mapmode
[modenum
] & O_CREAT
))
68 /* Attempt to create directory, don't worry about
71 (void)mkdir(mapname
[MAP_DIRNAME
], 0755);
72 fd
= open(mapname
[modenum
], mapmode
[modenum
], 0600);
74 return fdopen(fd
, mapsmode
[modenum
]);
78 int map_write(struct map_ent
*mel
)
83 f
= open_map(MAP_NEW
);
87 for (; mel
; mel
= mel
->next
) {
90 fprintf(f
, "%s ", mel
->devnm
);
91 fprintf(f
, "%s ", mel
->metadata
);
92 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
93 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
94 fprintf(f
, "%s\n", mel
->path
?:"");
103 return rename(mapname
[1],
107 static FILE *lf
= NULL
;
108 int map_lock(struct map_ent
**melp
)
112 lf
= open_map(MAP_LOCK
);
115 if (flock(fileno(lf
), LOCK_EX
) != 0) {
120 if (fstat(fileno(lf
), &buf
) != 0 ||
122 /* The owner of the lock unlinked it,
123 * so we have a lock on a stale file,
136 void map_unlock(struct map_ent
**melp
)
139 /* must unlink before closing the file,
140 * 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
);
274 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
280 for (mp
= *map
; mp
; mp
= mp
->next
) {
281 if (memcmp(uuid
, mp
->uuid
, 16) != 0)
283 if (!mddev_busy(mp
->devnm
)) {
292 struct map_ent
*map_by_devnm(struct map_ent
**map
, char *devnm
)
298 for (mp
= *map
; mp
; mp
= mp
->next
) {
299 if (strcmp(mp
->devnm
, devnm
) != 0)
301 if (!mddev_busy(mp
->devnm
)) {
310 struct map_ent
*map_by_name(struct map_ent
**map
, char *name
)
316 for (mp
= *map
; mp
; mp
= mp
->next
) {
319 if (strncmp(mp
->path
, "/dev/md/", 8) != 0)
321 if (strcmp(mp
->path
+8, name
) != 0)
323 if (!mddev_busy(mp
->devnm
)) {
332 /* sets the proper subarray and container_dev according to the metadata
333 * version super_by_fd does this automatically, this routine is meant as
334 * a supplement for guess_super()
336 static char *get_member_info(struct mdstat_ent
*ent
)
339 if (ent
->metadata_version
== NULL
||
340 strncmp(ent
->metadata_version
, "external:", 9) != 0)
343 if (is_subarray(&ent
->metadata_version
[9])) {
346 subarray
= strrchr(ent
->metadata_version
, '/');
352 void RebuildMap(void)
354 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
355 struct mdstat_ent
*md
;
356 struct map_ent
*map
= NULL
;
357 int require_homehost
;
358 char sys_hostname
[256];
359 char *homehost
= conf_get_homehost(&require_homehost
);
361 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
362 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
363 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
364 homehost
= sys_hostname
;
368 for (md
= mdstat
; md
; md
= md
->next
) {
369 struct mdinfo
*sra
= sysfs_read(-1, md
->devnm
, GET_DEVS
);
375 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
381 struct supertype
*st
;
382 char *subarray
= NULL
;
386 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
387 dfd
= dev_open(dn
, O_RDONLY
);
390 st
= guess_super(dfd
);
394 subarray
= get_member_info(md
);
395 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
401 info
= st
->ss
->container_content(st
, subarray
);
403 info
= xmalloc(sizeof(*info
));
404 st
->ss
->getinfo_super(st
, info
, NULL
);
409 devid
= devnm2devid(md
->devnm
);
410 path
= map_dev(major(devid
), minor(devid
), 0);
412 strncmp(path
, "/dev/md/", 8) != 0) {
413 /* We would really like a name that provides
414 * an MD_DEVNAME for udev.
415 * The name needs to be unique both in /dev/md/
416 * and in this mapfile.
417 * It needs to match what -I or -As would come
420 * Check if array is in mdadm.conf
422 * determine trustworthy from homehost etc
423 * find a unique name based on metadata name.
426 struct mddev_ident
*match
= conf_match(st
, info
,
430 if (match
&& match
->devname
&& match
->devname
[0] == '/') {
431 path
= match
->devname
;
432 if (path
[0] != '/') {
433 strcpy(namebuf
, "/dev/md/");
434 strcat(namebuf
, path
);
442 if ((homehost
== NULL
||
443 st
->ss
->match_home(st
, homehost
) != 1) &&
444 st
->ss
->match_home(st
, "any") != 1 &&
446 !conf_name_is_free(info
->name
)))
447 /* require a numeric suffix */
450 /* allow name to be used as-is if no conflict */
455 if (!isdigit(name
[strlen(name
)-1]) &&
461 if (strchr(name
, ':')) {
462 /* Probably a uniquifying
463 * hostname prefix. Allow
464 * without a suffix, and strip
465 * hostname if it is us.
467 if (homehost
&& unum
== -1 &&
468 strncmp(name
, homehost
,
469 strlen(homehost
)) == 0 &&
470 name
[strlen(homehost
)] == ':')
471 name
+= strlen(homehost
)+1;
477 sprintf(namebuf
, "/dev/md/%s%s%d",
480 sprintf(namebuf
, "/dev/md/%s",
483 if (lstat(namebuf
, &stb
) != 0 &&
485 !map_by_name(&map
, namebuf
+8)))
491 map_add(&map
, md
->devnm
,
494 st
->ss
->free_super(st
);
500 /* Only trigger a change if we wrote a new map file */
502 for (md
= mdstat
; md
; md
= md
->next
) {
503 struct mdinfo
*sra
= sysfs_read(-1, md
->devnm
,
506 sysfs_uevent(sra
, "change");