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