]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
RebuildMap: handle missing disks
[thirdparty/mdadm.git] / mapfile.c
1 /*
2 * mapfile - manage /var/run/mdadm.map. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 * Author: Neil Brown
23 * Email: <neilb@suse.de>
24 * Paper: Neil Brown
25 * Novell Inc
26 * GPO Box Q1283
27 * QVB Post Office, NSW 1230
28 * Australia
29 */
30
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.
34 *
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
40 *
41 */
42
43
44 #include "mdadm.h"
45
46
47 int map_write(struct map_ent *mel)
48 {
49 FILE *f;
50 int err;
51 int subdir = 1;
52
53 f = fopen("/var/run/mdadm/map.new", "w");
54 if (!f) {
55 f = fopen("/var/run/mdadm.map.new", "w");
56 subdir = 0;
57 }
58 if (!f)
59 return 0;
60 for (; mel; mel = mel->next) {
61 if (mel->bad)
62 continue;
63 if (mel->devnum < 0)
64 fprintf(f, "mdp%d ", -1-mel->devnum);
65 else
66 fprintf(f, "md%d ", mel->devnum);
67 fprintf(f, "%s ", mel->metadata);
68 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
69 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
70 fprintf(f, "%s\n", mel->path);
71 }
72 fflush(f);
73 err = ferror(f);
74 fclose(f);
75 if (err) {
76 if (subdir)
77 unlink("/var/run/mdadm/map.new");
78 else
79 unlink("/var/run/mdadm.map.new");
80 return 0;
81 }
82 if (subdir)
83 return rename("/var/run/mdadm/map.new",
84 "/var/run/mdadm/map") == 0;
85 else
86 return rename("/var/run/mdadm.map.new",
87 "/var/run/mdadm.map") == 0;
88 }
89
90
91 static int lfd = -1;
92 static int lsubdir = 0;
93 int map_lock(struct map_ent **melp)
94 {
95 if (lfd < 0) {
96 lfd = open("/var/run/mdadm/map.lock", O_CREAT|O_RDWR, 0600);
97 if (lfd < 0) {
98 lfd = open("/var/run/mdadm.map.lock", O_CREAT|O_RDWR, 0600);
99 lsubdir = 0;
100 } else
101 lsubdir = 1;
102 if (lfd < 0)
103 return -1;
104 if (lockf(lfd, F_LOCK, 0) != 0) {
105 close(lfd);
106 lfd = -1;
107 return -1;
108 }
109 }
110 if (*melp)
111 map_free(*melp);
112 map_read(melp);
113 return 0;
114 }
115
116 void map_unlock(struct map_ent **melp)
117 {
118 if (lfd >= 0)
119 close(lfd);
120 if (lsubdir)
121 unlink("/var/run/mdadm/map.lock");
122 else
123 unlink("/var/run/mdadm.map.lock");
124 lfd = -1;
125 }
126
127 void map_add(struct map_ent **melp,
128 int devnum, char *metadata, int uuid[4], char *path)
129 {
130 struct map_ent *me = malloc(sizeof(*me));
131
132 me->devnum = devnum;
133 strcpy(me->metadata, metadata);
134 memcpy(me->uuid, uuid, 16);
135 me->path = strdup(path);
136 me->next = *melp;
137 me->bad = 0;
138 *melp = me;
139 }
140
141 void map_read(struct map_ent **melp)
142 {
143 FILE *f;
144 char buf[8192];
145 char path[200];
146 int devnum, uuid[4];
147 char metadata[30];
148 char nam[4];
149
150 *melp = NULL;
151
152 f = fopen("/var/run/mdadm/map", "r");
153 if (!f)
154 f = fopen("/var/run/mdadm.map", "r");
155 if (!f) {
156 RebuildMap();
157 f = fopen("/var/run/mdadm/map", "r");
158 }
159 if (!f)
160 f = fopen("/var/run/mdadm.map", "r");
161 if (!f)
162 return;
163
164 while (fgets(buf, sizeof(buf), f)) {
165 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
166 nam, &devnum, metadata, uuid, uuid+1,
167 uuid+2, uuid+3, path) == 8) {
168 if (strncmp(nam, "md", 2) != 0)
169 continue;
170 if (nam[2] == 'p')
171 devnum = -1 - devnum;
172 map_add(melp, devnum, metadata, uuid, path);
173 }
174 }
175 fclose(f);
176 }
177
178 void map_free(struct map_ent *map)
179 {
180 while (map) {
181 struct map_ent *mp = map;
182 map = mp->next;
183 free(mp->path);
184 free(mp);
185 }
186 }
187
188 int map_update(struct map_ent **mpp, int devnum, char *metadata,
189 int *uuid, char *path)
190 {
191 struct map_ent *map, *mp;
192 int rv;
193
194 if (mpp && *mpp)
195 map = *mpp;
196 else
197 map_read(&map);
198
199 for (mp = map ; mp ; mp=mp->next)
200 if (mp->devnum == devnum) {
201 strcpy(mp->metadata, metadata);
202 memcpy(mp->uuid, uuid, 16);
203 free(mp->path);
204 mp->path = strdup(path);
205 break;
206 }
207 if (!mp)
208 map_add(&map, devnum, metadata, uuid, path);
209 if (mpp)
210 *mpp = NULL;
211 rv = map_write(map);
212 map_free(map);
213 return rv;
214 }
215
216 void map_delete(struct map_ent **mapp, int devnum)
217 {
218 struct map_ent *mp;
219
220 if (*mapp == NULL)
221 map_read(mapp);
222
223 for (mp = *mapp; mp; mp = *mapp) {
224 if (mp->devnum == devnum) {
225 *mapp = mp->next;
226 free(mp->path);
227 free(mp);
228 } else
229 mapp = & mp->next;
230 }
231 }
232
233 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
234 {
235 struct map_ent *mp;
236 if (!*map)
237 map_read(map);
238
239 for (mp = *map ; mp ; mp = mp->next) {
240 if (memcmp(uuid, mp->uuid, 16) != 0)
241 continue;
242 if (!mddev_busy(mp->devnum)) {
243 mp->bad = 1;
244 continue;
245 }
246 return mp;
247 }
248 return NULL;
249 }
250
251 struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
252 {
253 struct map_ent *mp;
254 if (!*map)
255 map_read(map);
256
257 for (mp = *map ; mp ; mp = mp->next) {
258 if (mp->devnum != devnum)
259 continue;
260 if (!mddev_busy(mp->devnum)) {
261 mp->bad = 1;
262 continue;
263 }
264 return mp;
265 }
266 return NULL;
267 }
268
269 struct map_ent *map_by_name(struct map_ent **map, char *name)
270 {
271 struct map_ent *mp;
272 if (!*map)
273 map_read(map);
274
275 for (mp = *map ; mp ; mp = mp->next) {
276 if (strncmp(mp->path, "/dev/md/", 8) != 0)
277 continue;
278 if (strcmp(mp->path+8, name) != 0)
279 continue;
280 if (!mddev_busy(mp->devnum)) {
281 mp->bad = 1;
282 continue;
283 }
284 return mp;
285 }
286 return NULL;
287 }
288
289 void RebuildMap(void)
290 {
291 struct mdstat_ent *mdstat = mdstat_read(0, 0);
292 struct mdstat_ent *md;
293 struct map_ent *map = NULL;
294 int mdp = get_mdp_major();
295
296 for (md = mdstat ; md ; md = md->next) {
297 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
298 struct mdinfo *sd;
299
300 if (!sra)
301 continue;
302
303 for (sd = sra->devs ; sd ; sd = sd->next) {
304 char dn[30];
305 int dfd;
306 int ok;
307 struct supertype *st;
308 char *path;
309 struct mdinfo info;
310
311 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
312 dfd = dev_open(dn, O_RDONLY);
313 if (dfd < 0)
314 continue;
315 st = guess_super(dfd);
316 if ( st == NULL)
317 ok = -1;
318 else
319 ok = st->ss->load_super(st, dfd, NULL);
320 close(dfd);
321 if (ok != 0)
322 continue;
323 st->ss->getinfo_super(st, &info);
324 if (md->devnum > 0)
325 path = map_dev(MD_MAJOR, md->devnum, 0);
326 else
327 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
328 map_add(&map, md->devnum,
329 info.text_version,
330 info.uuid, path ? : "/unknown");
331 st->ss->free_super(st);
332 break;
333 }
334 sysfs_free(sra);
335 }
336 map_write(map);
337 map_free(map);
338 for (md = mdstat ; md ; md = md->next) {
339 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION);
340 sysfs_uevent(sra, "change");
341 sysfs_free(sra);
342 }
343 free_mdstat(mdstat);
344 }