]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
Only signal a udev change event if we actually write a mapfile in RebuildMap
[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 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
255 {
256 struct map_ent *mp;
257 if (!*map)
258 map_read(map);
259
260 for (mp = *map ; mp ; mp = mp->next) {
261 if (memcmp(uuid, mp->uuid, 16) != 0)
262 continue;
263 if (!mddev_busy(mp->devnum)) {
264 mp->bad = 1;
265 continue;
266 }
267 return mp;
268 }
269 return NULL;
270 }
271
272 struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
273 {
274 struct map_ent *mp;
275 if (!*map)
276 map_read(map);
277
278 for (mp = *map ; mp ; mp = mp->next) {
279 if (mp->devnum != devnum)
280 continue;
281 if (!mddev_busy(mp->devnum)) {
282 mp->bad = 1;
283 continue;
284 }
285 return mp;
286 }
287 return NULL;
288 }
289
290 struct map_ent *map_by_name(struct map_ent **map, char *name)
291 {
292 struct map_ent *mp;
293 if (!*map)
294 map_read(map);
295
296 for (mp = *map ; mp ; mp = mp->next) {
297 if (!mp->path)
298 continue;
299 if (strncmp(mp->path, "/dev/md/", 8) != 0)
300 continue;
301 if (strcmp(mp->path+8, name) != 0)
302 continue;
303 if (!mddev_busy(mp->devnum)) {
304 mp->bad = 1;
305 continue;
306 }
307 return mp;
308 }
309 return NULL;
310 }
311
312 /* sets the proper subarray and container_dev according to the metadata
313 * version super_by_fd does this automatically, this routine is meant as
314 * a supplement for guess_super()
315 */
316 static void set_member_info(struct supertype *st, struct mdstat_ent *ent)
317 {
318
319 st->subarray[0] = '\0';
320
321 if (ent->metadata_version == NULL ||
322 strncmp(ent->metadata_version, "external:", 9) != 0)
323 return;
324
325 if (is_subarray(&ent->metadata_version[9])) {
326 char version[strlen(ent->metadata_version)+1];
327 char *subarray;
328 char *name = &version[10];
329
330 strcpy(version, ent->metadata_version);
331 subarray = strrchr(version, '/');
332 name = &version[10];
333
334 if (!subarray)
335 return;
336 *subarray++ = '\0';
337
338 st->container_dev = devname2devnum(name);
339 strncpy(st->subarray, subarray, sizeof(st->subarray));
340 }
341 }
342
343 void RebuildMap(void)
344 {
345 struct mdstat_ent *mdstat = mdstat_read(0, 0);
346 struct mdstat_ent *md;
347 struct map_ent *map = NULL;
348 int mdp = get_mdp_major();
349 int require_homehost;
350 char sys_hostname[256];
351 char *homehost = conf_get_homehost(&require_homehost);
352
353 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
354 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
355 sys_hostname[sizeof(sys_hostname)-1] = 0;
356 homehost = sys_hostname;
357 }
358 }
359
360 for (md = mdstat ; md ; md = md->next) {
361 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
362 struct mdinfo *sd;
363
364 if (!sra)
365 continue;
366
367 for (sd = sra->devs ; sd ; sd = sd->next) {
368 char namebuf[100];
369 char dn[30];
370 int dfd;
371 int ok;
372 struct supertype *st;
373 char *path;
374 struct mdinfo info;
375
376 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
377 dfd = dev_open(dn, O_RDONLY);
378 if (dfd < 0)
379 continue;
380 st = guess_super(dfd);
381 if ( st == NULL)
382 ok = -1;
383 else {
384 set_member_info(st, md);
385 ok = st->ss->load_super(st, dfd, NULL);
386 }
387 close(dfd);
388 if (ok != 0)
389 continue;
390 st->ss->getinfo_super(st, &info);
391 if (md->devnum >= 0)
392 path = map_dev(MD_MAJOR, md->devnum, 0);
393 else
394 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
395 if (path == NULL ||
396 strncmp(path, "/dev/md/", 8) != 0) {
397 /* We would really like a name that provides
398 * an MD_DEVNAME for udev.
399 * The name needs to be unique both in /dev/md/
400 * and in this mapfile.
401 * It needs to match watch -I or -As would come
402 * up with.
403 * That means:
404 * Check if array is in mdadm.conf
405 * - if so use that.
406 * determine trustworthy from homehost etc
407 * find a unique name based on metadata name.
408 *
409 */
410 struct mddev_ident_s *match = conf_match(&info, st);
411 struct stat stb;
412 if (match && match->devname && match->devname[0] == '/') {
413 path = match->devname;
414 if (path[0] != '/') {
415 strcpy(namebuf, "/dev/md/");
416 strcat(namebuf, path);
417 path = namebuf;
418 }
419 } else {
420 int unum = 0;
421 char *sep = "_";
422 const char *name;
423 int conflict = 1;
424 if ((homehost == NULL ||
425 st->ss->match_home(st, homehost) != 1) &&
426 st->ss->match_home(st, "any") != 1 &&
427 (require_homehost
428 || ! conf_name_is_free(info.name)))
429 /* require a numeric suffix */
430 unum = 0;
431 else
432 /* allow name to be used as-is if no conflict */
433 unum = -1;
434 name = info.name;
435 if (!*name) {
436 name = st->ss->name;
437 if (!isdigit(name[strlen(name)-1]) &&
438 unum == -1) {
439 unum = 0;
440 sep = "";
441 }
442 }
443 if (strchr(name, ':'))
444 /* probably a uniquifying
445 * hostname prefix. Allow
446 * without a suffix
447 */
448 unum = -1;
449
450 while (conflict) {
451 if (unum >= 0)
452 sprintf(namebuf, "/dev/md/%s%s%d",
453 name, sep, unum);
454 else
455 sprintf(namebuf, "/dev/md/%s",
456 name);
457 unum++;
458 if (lstat(namebuf, &stb) != 0 &&
459 (map == NULL ||
460 !map_by_name(&map, namebuf+8)))
461 conflict = 0;
462 }
463 path = namebuf;
464 }
465 }
466 map_add(&map, md->devnum,
467 info.text_version,
468 info.uuid, path);
469 st->ss->free_super(st);
470 break;
471 }
472 sysfs_free(sra);
473 }
474 /* Only trigger a change if we wrote a new map file */
475 if (map_write(map))
476 for (md = mdstat ; md ; md = md->next) {
477 struct mdinfo *sra = sysfs_read(-1, md->devnum,
478 GET_VERSION);
479 sysfs_uevent(sra, "change");
480 sysfs_free(sra);
481 }
482 map_free(map);
483 free_mdstat(mdstat);
484 }