2 * mapfile - manage /var/run/mdadm.map. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006 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 is a number.
37 * metadata - 0.90 1.0 1.1 1.2
38 * UUID - uuid of the array
39 * path - path where device created: /dev/md/home
47 int map_write(struct map_ent
*mel
)
53 f
= fopen("/var/run/mdadm/map.new", "w");
55 f
= fopen("/var/run/mdadm.map.new", "w");
62 fprintf(f
, "mdp%d ", -1-mel
->devnum
);
64 fprintf(f
, "md%d ", mel
->devnum
);
65 fprintf(f
, "%d.%d ", mel
->major
, mel
->minor
);
66 fprintf(f
, "%08x:%08x:%08x:%08x ", mel
->uuid
[0],
67 mel
->uuid
[1], mel
->uuid
[2], mel
->uuid
[3]);
68 fprintf(f
, "%s\n", mel
->path
);
76 unlink("/var/run/mdadm/map.new");
78 unlink("/var/run/mdadm.map.new");
82 return rename("/var/run/mdadm/map.new",
83 "/var/run/mdadm/map") == 0;
85 return rename("/var/run/mdadm.map.new",
86 "/var/run/mdadm.map") == 0;
89 void map_add(struct map_ent
**melp
,
90 int devnum
, int major
, int minor
, int uuid
[4], char *path
)
92 struct map_ent
*me
= malloc(sizeof(*me
));
97 memcpy(me
->uuid
, uuid
, 16);
98 me
->path
= strdup(path
);
103 void map_read(struct map_ent
**melp
)
108 int devnum
, major
, minor
, uuid
[4];
113 f
= fopen("/var/run/mdadm/map", "r");
115 f
= fopen("/var/run/mdadm.map", "r");
119 while (fgets(buf
, sizeof(buf
), f
)) {
120 if (sscanf(buf
, " md%1[p]%d %d.%d %x:%x:%x:%x %200s",
121 nam
, &devnum
, &major
, &minor
, uuid
, uuid
+1,
122 uuid
+2, uuid
+3, path
) == 9) {
124 devnum
= -1 - devnum
;
125 map_add(melp
, devnum
, major
, minor
, uuid
, path
);
131 void map_free(struct map_ent
*map
)
134 struct map_ent
*mp
= map
;
141 int map_update(struct map_ent
**mpp
, int devnum
, int major
, int minor
,
142 int *uuid
, char *path
)
144 struct map_ent
*map
, *mp
;
152 for (mp
= map
; mp
; mp
=mp
->next
)
153 if (mp
->devnum
== devnum
) {
156 memcpy(mp
->uuid
, uuid
, 16);
158 mp
->path
= strdup(path
);
162 map_add(&map
, devnum
, major
, minor
, uuid
, path
);
169 void map_delete(struct map_ent
**mapp
, int devnum
)
176 for (mp
= *mapp
; mp
; mp
= *mapp
) {
177 if (mp
->devnum
== devnum
) {
186 struct map_ent
*map_by_uuid(struct map_ent
**map
, int uuid
[4])
192 for (mp
= *map
; mp
; mp
= mp
->next
)
193 if (memcmp(uuid
, mp
->uuid
, 16) == 0)