]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
Create: cleanup after failed create in duplicated array member case
[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-2009 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 * mode. 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 * 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 /lib/initrw/madam/map
46 * The last can easily be change at compile to e.g. somewhere in /dev.
47 * We read from the first one that exists and write to the first
48 * one that we can.
49 */
50 #include "mdadm.h"
51 #include <sys/file.h>
52 #include <ctype.h>
53
54 #define mapnames(base) { base, base ".new", base ".lock"}
55 char *mapname[3][3] = {
56 mapnames(VAR_RUN "/map"),
57 mapnames("/var/run/mdadm.map"),
58 mapnames(ALT_RUN "/" ALT_MAPFILE)
59 };
60 char *mapdir[3] = { VAR_RUN, NULL, ALT_RUN };
61
62 int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC };
63 char *mapsmode[3] = { "r", "w", "w"};
64
65 FILE *open_map(int modenum, int *choice)
66 {
67 int i;
68
69 for (i = 0 ; i < 3 ; i++) {
70 int fd;
71 if ((mapmode[modenum] & O_CREAT) &&
72 mapdir[modenum])
73 /* Attempt to create directory, don't worry about
74 * failure.
75 */
76 mkdir(mapdir[modenum], 0755);
77 fd = open(mapname[i][modenum], mapmode[modenum], 0600);
78 if (fd >= 0) {
79 *choice = i;
80 return fdopen(fd, mapsmode[modenum]);
81 }
82 }
83 return NULL;
84 }
85
86 int map_write(struct map_ent *mel)
87 {
88 FILE *f;
89 int err;
90 int which;
91
92 f = open_map(1, &which);
93
94 if (!f)
95 return 0;
96 for (; mel; mel = mel->next) {
97 if (mel->bad)
98 continue;
99 if (mel->devnum < 0)
100 fprintf(f, "mdp%d ", -1-mel->devnum);
101 else
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?:"");
107 }
108 fflush(f);
109 err = ferror(f);
110 fclose(f);
111 if (err) {
112 unlink(mapname[which][1]);
113 return 0;
114 }
115 return rename(mapname[which][1],
116 mapname[which][0]) == 0;
117 }
118
119
120 static FILE *lf = NULL;
121 static int lwhich = 0;
122 int map_lock(struct map_ent **melp)
123 {
124 if (lf == NULL) {
125 lf = open_map(2, &lwhich);
126 if (lf == NULL)
127 return -1;
128 if (flock(fileno(lf), LOCK_EX) != 0) {
129 fclose(lf);
130 lf = NULL;
131 return -1;
132 }
133 }
134 if (*melp)
135 map_free(*melp);
136 map_read(melp);
137 return 0;
138 }
139
140 void map_unlock(struct map_ent **melp)
141 {
142 if (lf) {
143 flock(fileno(lf), LOCK_UN);
144 fclose(lf);
145 }
146 unlink(mapname[lwhich][2]);
147 lf = NULL;
148 }
149
150 void map_add(struct map_ent **melp,
151 int devnum, char *metadata, int uuid[4], char *path)
152 {
153 struct map_ent *me = malloc(sizeof(*me));
154
155 me->devnum = devnum;
156 strcpy(me->metadata, metadata);
157 memcpy(me->uuid, uuid, 16);
158 me->path = path ? strdup(path) : NULL;
159 me->next = *melp;
160 me->bad = 0;
161 *melp = me;
162 }
163
164 void map_read(struct map_ent **melp)
165 {
166 FILE *f;
167 char buf[8192];
168 char path[200];
169 int devnum, uuid[4];
170 char metadata[30];
171 char nam[4];
172 int which;
173
174 *melp = NULL;
175
176 f = open_map(0, &which);
177 if (!f) {
178 RebuildMap();
179 f = open_map(0, &which);
180 }
181 if (!f)
182 return;
183
184 while (fgets(buf, sizeof(buf), f)) {
185 path[0] = 0;
186 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
187 nam, &devnum, metadata, uuid, uuid+1,
188 uuid+2, uuid+3, path) >= 7) {
189 if (strncmp(nam, "md", 2) != 0)
190 continue;
191 if (nam[2] == 'p')
192 devnum = -1 - devnum;
193 map_add(melp, devnum, metadata, uuid, path);
194 }
195 }
196 fclose(f);
197 }
198
199 void map_free(struct map_ent *map)
200 {
201 while (map) {
202 struct map_ent *mp = map;
203 map = mp->next;
204 free(mp->path);
205 free(mp);
206 }
207 }
208
209 int map_update(struct map_ent **mpp, int devnum, char *metadata,
210 int *uuid, char *path)
211 {
212 struct map_ent *map, *mp;
213 int rv;
214
215 if (mpp && *mpp)
216 map = *mpp;
217 else
218 map_read(&map);
219
220 for (mp = map ; mp ; mp=mp->next)
221 if (mp->devnum == devnum) {
222 strcpy(mp->metadata, metadata);
223 memcpy(mp->uuid, uuid, 16);
224 free(mp->path);
225 mp->path = path ? strdup(path) : NULL;
226 break;
227 }
228 if (!mp)
229 map_add(&map, devnum, metadata, uuid, path);
230 if (mpp)
231 *mpp = NULL;
232 rv = map_write(map);
233 map_free(map);
234 return rv;
235 }
236
237 void map_delete(struct map_ent **mapp, int devnum)
238 {
239 struct map_ent *mp;
240
241 if (*mapp == NULL)
242 map_read(mapp);
243
244 for (mp = *mapp; mp; mp = *mapp) {
245 if (mp->devnum == devnum) {
246 *mapp = mp->next;
247 free(mp->path);
248 free(mp);
249 } else
250 mapp = & mp->next;
251 }
252 }
253
254 void map_remove(struct map_ent **mapp, int devnum)
255 {
256 if (devnum == NoMdDev)
257 return;
258
259 map_delete(mapp, devnum);
260 map_write(*mapp);
261 map_free(*mapp);
262 }
263
264 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
265 {
266 struct map_ent *mp;
267 if (!*map)
268 map_read(map);
269
270 for (mp = *map ; mp ; mp = mp->next) {
271 if (memcmp(uuid, mp->uuid, 16) != 0)
272 continue;
273 if (!mddev_busy(mp->devnum)) {
274 mp->bad = 1;
275 continue;
276 }
277 return mp;
278 }
279 return NULL;
280 }
281
282 struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
283 {
284 struct map_ent *mp;
285 if (!*map)
286 map_read(map);
287
288 for (mp = *map ; mp ; mp = mp->next) {
289 if (mp->devnum != devnum)
290 continue;
291 if (!mddev_busy(mp->devnum)) {
292 mp->bad = 1;
293 continue;
294 }
295 return mp;
296 }
297 return NULL;
298 }
299
300 struct map_ent *map_by_name(struct map_ent **map, char *name)
301 {
302 struct map_ent *mp;
303 if (!*map)
304 map_read(map);
305
306 for (mp = *map ; mp ; mp = mp->next) {
307 if (!mp->path)
308 continue;
309 if (strncmp(mp->path, "/dev/md/", 8) != 0)
310 continue;
311 if (strcmp(mp->path+8, name) != 0)
312 continue;
313 if (!mddev_busy(mp->devnum)) {
314 mp->bad = 1;
315 continue;
316 }
317 return mp;
318 }
319 return NULL;
320 }
321
322 /* sets the proper subarray and container_dev according to the metadata
323 * version super_by_fd does this automatically, this routine is meant as
324 * a supplement for guess_super()
325 */
326 static void set_member_info(struct supertype *st, struct mdstat_ent *ent)
327 {
328
329 st->subarray[0] = '\0';
330
331 if (ent->metadata_version == NULL ||
332 strncmp(ent->metadata_version, "external:", 9) != 0)
333 return;
334
335 if (is_subarray(&ent->metadata_version[9])) {
336 char version[strlen(ent->metadata_version)+1];
337 char *subarray;
338 char *name = &version[10];
339
340 strcpy(version, ent->metadata_version);
341 subarray = strrchr(version, '/');
342 name = &version[10];
343
344 if (!subarray)
345 return;
346 *subarray++ = '\0';
347
348 st->container_dev = devname2devnum(name);
349 strncpy(st->subarray, subarray, sizeof(st->subarray));
350 }
351 }
352
353 void RebuildMap(void)
354 {
355 struct mdstat_ent *mdstat = mdstat_read(0, 0);
356 struct mdstat_ent *md;
357 struct map_ent *map = NULL;
358 int mdp = get_mdp_major();
359 int require_homehost;
360 char sys_hostname[256];
361 char *homehost = conf_get_homehost(&require_homehost);
362
363 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
364 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
365 sys_hostname[sizeof(sys_hostname)-1] = 0;
366 homehost = sys_hostname;
367 }
368 }
369
370 for (md = mdstat ; md ; md = md->next) {
371 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
372 struct mdinfo *sd;
373
374 if (!sra)
375 continue;
376
377 for (sd = sra->devs ; sd ; sd = sd->next) {
378 char namebuf[100];
379 char dn[30];
380 int dfd;
381 int ok;
382 struct supertype *st;
383 char *path;
384 struct mdinfo info;
385
386 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
387 dfd = dev_open(dn, O_RDONLY);
388 if (dfd < 0)
389 continue;
390 st = guess_super(dfd);
391 if ( st == NULL)
392 ok = -1;
393 else {
394 set_member_info(st, md);
395 ok = st->ss->load_super(st, dfd, NULL);
396 }
397 close(dfd);
398 if (ok != 0)
399 continue;
400 st->ss->getinfo_super(st, &info);
401 if (md->devnum >= 0)
402 path = map_dev(MD_MAJOR, md->devnum, 0);
403 else
404 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
405 if (path == NULL ||
406 strncmp(path, "/dev/md/", 8) != 0) {
407 /* We would really like a name that provides
408 * an MD_DEVNAME for udev.
409 * The name needs to be unique both in /dev/md/
410 * and in this mapfile.
411 * It needs to match watch -I or -As would come
412 * up with.
413 * That means:
414 * Check if array is in mdadm.conf
415 * - if so use that.
416 * determine trustworthy from homehost etc
417 * find a unique name based on metadata name.
418 *
419 */
420 struct mddev_ident_s *match = conf_match(&info, st);
421 struct stat stb;
422 if (match && match->devname && match->devname[0] == '/') {
423 path = match->devname;
424 if (path[0] != '/') {
425 strcpy(namebuf, "/dev/md/");
426 strcat(namebuf, path);
427 path = namebuf;
428 }
429 } else {
430 int unum = 0;
431 char *sep = "_";
432 const char *name;
433 int conflict = 1;
434 if ((homehost == NULL ||
435 st->ss->match_home(st, homehost) != 1) &&
436 st->ss->match_home(st, "any") != 1 &&
437 (require_homehost
438 || ! conf_name_is_free(info.name)))
439 /* require a numeric suffix */
440 unum = 0;
441 else
442 /* allow name to be used as-is if no conflict */
443 unum = -1;
444 name = info.name;
445 if (!*name) {
446 name = st->ss->name;
447 if (!isdigit(name[strlen(name)-1]) &&
448 unum == -1) {
449 unum = 0;
450 sep = "";
451 }
452 }
453 if (strchr(name, ':'))
454 /* probably a uniquifying
455 * hostname prefix. Allow
456 * without a suffix
457 */
458 unum = -1;
459
460 while (conflict) {
461 if (unum >= 0)
462 sprintf(namebuf, "/dev/md/%s%s%d",
463 name, sep, unum);
464 else
465 sprintf(namebuf, "/dev/md/%s",
466 name);
467 unum++;
468 if (lstat(namebuf, &stb) != 0 &&
469 (map == NULL ||
470 !map_by_name(&map, namebuf+8)))
471 conflict = 0;
472 }
473 path = namebuf;
474 }
475 }
476 map_add(&map, md->devnum,
477 info.text_version,
478 info.uuid, path);
479 st->ss->free_super(st);
480 break;
481 }
482 sysfs_free(sra);
483 }
484 /* Only trigger a change if we wrote a new map file */
485 if (map_write(map))
486 for (md = mdstat ; md ; md = md->next) {
487 struct mdinfo *sra = sysfs_read(-1, md->devnum,
488 GET_VERSION);
489 sysfs_uevent(sra, "change");
490 sysfs_free(sra);
491 }
492 map_free(map);
493 free_mdstat(mdstat);
494 }