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