]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mapfile.c
mdadm: load default sysfs attributes after assemblation
[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 if (*melp)
147 map_free(*melp);
148 lf = NULL;
149 }
150
151 void map_fork(void)
152 {
153 /* We are forking, so must close the lock file.
154 * Don't risk flushing anything though.
155 */
156 if (lf) {
157 close(fileno(lf));
158 fclose(lf);
159 lf = NULL;
160 }
161 }
162
163 void map_add(struct map_ent **melp,
164 char * devnm, char *metadata, int uuid[4], char *path)
165 {
166 struct map_ent *me = xmalloc(sizeof(*me));
167
168 strcpy(me->devnm, devnm);
169 strcpy(me->metadata, metadata);
170 memcpy(me->uuid, uuid, 16);
171 me->path = path ? xstrdup(path) : NULL;
172 me->next = *melp;
173 me->bad = 0;
174 *melp = me;
175 }
176
177 void map_read(struct map_ent **melp)
178 {
179 FILE *f;
180 char buf[8192];
181 char path[201];
182 int uuid[4];
183 char devnm[32];
184 char metadata[30];
185
186 *melp = NULL;
187
188 f = open_map(MAP_READ);
189 if (!f) {
190 RebuildMap();
191 f = open_map(MAP_READ);
192 }
193 if (!f)
194 return;
195
196 while (fgets(buf, sizeof(buf), f)) {
197 path[0] = 0;
198 if (sscanf(buf, " %s %s %x:%x:%x:%x %200s",
199 devnm, metadata, uuid, uuid+1,
200 uuid+2, uuid+3, path) >= 7) {
201 map_add(melp, devnm, metadata, uuid, path);
202 }
203 }
204 fclose(f);
205 }
206
207 void map_free(struct map_ent *map)
208 {
209 while (map) {
210 struct map_ent *mp = map;
211 map = mp->next;
212 free(mp->path);
213 free(mp);
214 }
215 }
216
217 int map_update(struct map_ent **mpp, char *devnm, char *metadata,
218 int *uuid, char *path)
219 {
220 struct map_ent *map, *mp;
221 int rv;
222
223 if (mpp && *mpp)
224 map = *mpp;
225 else
226 map_read(&map);
227
228 for (mp = map ; mp ; mp=mp->next)
229 if (strcmp(mp->devnm, devnm) == 0) {
230 strcpy(mp->metadata, metadata);
231 memcpy(mp->uuid, uuid, 16);
232 free(mp->path);
233 mp->path = path ? xstrdup(path) : NULL;
234 mp->bad = 0;
235 break;
236 }
237 if (!mp)
238 map_add(&map, devnm, metadata, uuid, path);
239 if (mpp)
240 *mpp = NULL;
241 rv = map_write(map);
242 map_free(map);
243 return rv;
244 }
245
246 void map_delete(struct map_ent **mapp, char *devnm)
247 {
248 struct map_ent *mp;
249
250 if (*mapp == NULL)
251 map_read(mapp);
252
253 for (mp = *mapp; mp; mp = *mapp) {
254 if (strcmp(mp->devnm, devnm) == 0) {
255 *mapp = mp->next;
256 free(mp->path);
257 free(mp);
258 } else
259 mapp = & mp->next;
260 }
261 }
262
263 void map_remove(struct map_ent **mapp, char *devnm)
264 {
265 if (devnm[0] == 0)
266 return;
267
268 map_delete(mapp, devnm);
269 map_write(*mapp);
270 map_free(*mapp);
271 *mapp = NULL;
272 }
273
274 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
275 {
276 struct map_ent *mp;
277 if (!*map)
278 map_read(map);
279
280 for (mp = *map ; mp ; mp = mp->next) {
281 if (memcmp(uuid, mp->uuid, 16) != 0)
282 continue;
283 if (!mddev_busy(mp->devnm)) {
284 mp->bad = 1;
285 continue;
286 }
287 return mp;
288 }
289 return NULL;
290 }
291
292 struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
293 {
294 struct map_ent *mp;
295 if (!*map)
296 map_read(map);
297
298 for (mp = *map ; mp ; mp = mp->next) {
299 if (strcmp(mp->devnm, devnm) != 0)
300 continue;
301 if (!mddev_busy(mp->devnm)) {
302 mp->bad = 1;
303 continue;
304 }
305 return mp;
306 }
307 return NULL;
308 }
309
310 struct map_ent *map_by_name(struct map_ent **map, char *name)
311 {
312 struct map_ent *mp;
313 if (!*map)
314 map_read(map);
315
316 for (mp = *map ; mp ; mp = mp->next) {
317 if (!mp->path)
318 continue;
319 if (strncmp(mp->path, "/dev/md/", 8) != 0)
320 continue;
321 if (strcmp(mp->path+8, name) != 0)
322 continue;
323 if (!mddev_busy(mp->devnm)) {
324 mp->bad = 1;
325 continue;
326 }
327 return mp;
328 }
329 return NULL;
330 }
331
332 /* sets the proper subarray and container_dev according to the metadata
333 * version super_by_fd does this automatically, this routine is meant as
334 * a supplement for guess_super()
335 */
336 static char *get_member_info(struct mdstat_ent *ent)
337 {
338
339 if (ent->metadata_version == NULL ||
340 strncmp(ent->metadata_version, "external:", 9) != 0)
341 return NULL;
342
343 if (is_subarray(&ent->metadata_version[9])) {
344 char *subarray;
345
346 subarray = strrchr(ent->metadata_version, '/');
347 return subarray + 1;
348 }
349 return NULL;
350 }
351
352 void RebuildMap(void)
353 {
354 struct mdstat_ent *mdstat = mdstat_read(0, 0);
355 struct mdstat_ent *md;
356 struct map_ent *map = NULL;
357 int require_homehost;
358 char sys_hostname[256];
359 char *homehost = conf_get_homehost(&require_homehost);
360
361 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
362 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
363 sys_hostname[sizeof(sys_hostname)-1] = 0;
364 homehost = sys_hostname;
365 }
366 }
367
368 for (md = mdstat ; md ; md = md->next) {
369 struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS);
370 struct mdinfo *sd;
371
372 if (!sra)
373 continue;
374
375 for (sd = sra->devs ; sd ; sd = sd->next) {
376 char namebuf[100];
377 char dn[30];
378 int dfd;
379 int ok;
380 dev_t devid;
381 struct supertype *st;
382 char *subarray = NULL;
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 subarray = get_member_info(md);
395 ok = st->ss->load_super(st, dfd, NULL);
396 }
397 close(dfd);
398 if (ok != 0)
399 continue;
400 if (subarray)
401 info = st->ss->container_content(st, subarray);
402 else {
403 info = xmalloc(sizeof(*info));
404 st->ss->getinfo_super(st, info, NULL);
405 }
406 if (!info)
407 continue;
408
409 devid = devnm2devid(md->devnm);
410 path = map_dev(major(devid), minor(devid), 0);
411 if (path == NULL ||
412 strncmp(path, "/dev/md/", 8) != 0) {
413 /* We would really like a name that provides
414 * an MD_DEVNAME for udev.
415 * The name needs to be unique both in /dev/md/
416 * and in this mapfile.
417 * It needs to match what -I or -As would come
418 * up with.
419 * That means:
420 * Check if array is in mdadm.conf
421 * - if so use that.
422 * determine trustworthy from homehost etc
423 * find a unique name based on metadata name.
424 *
425 */
426 struct mddev_ident *match = conf_match(st, info,
427 NULL, 0,
428 NULL);
429 struct stat stb;
430 if (match && match->devname && match->devname[0] == '/') {
431 path = match->devname;
432 if (path[0] != '/') {
433 strcpy(namebuf, "/dev/md/");
434 strcat(namebuf, path);
435 path = namebuf;
436 }
437 } else {
438 int unum = 0;
439 char *sep = "_";
440 const char *name;
441 int conflict = 1;
442 if ((homehost == NULL ||
443 st->ss->match_home(st, homehost) != 1) &&
444 st->ss->match_home(st, "any") != 1 &&
445 (require_homehost ||
446 !conf_name_is_free(info->name)))
447 /* require a numeric suffix */
448 unum = 0;
449 else
450 /* allow name to be used as-is if no conflict */
451 unum = -1;
452 name = info->name;
453 if (!*name) {
454 name = st->ss->name;
455 if (!isdigit(name[strlen(name)-1]) &&
456 unum == -1) {
457 unum = 0;
458 sep = "";
459 }
460 }
461 if (strchr(name, ':')) {
462 /* Probably a uniquifying
463 * hostname prefix. Allow
464 * without a suffix, and strip
465 * hostname if it is us.
466 */
467 if (homehost && unum == -1 &&
468 strncmp(name, homehost,
469 strlen(homehost)) == 0 &&
470 name[strlen(homehost)] == ':')
471 name += strlen(homehost)+1;
472 unum = -1;
473 }
474
475 while (conflict) {
476 if (unum >= 0)
477 sprintf(namebuf, "/dev/md/%s%s%d",
478 name, sep, unum);
479 else
480 sprintf(namebuf, "/dev/md/%s",
481 name);
482 unum++;
483 if (lstat(namebuf, &stb) != 0 &&
484 (map == NULL ||
485 !map_by_name(&map, namebuf+8)))
486 conflict = 0;
487 }
488 path = namebuf;
489 }
490 }
491 map_add(&map, md->devnm,
492 info->text_version,
493 info->uuid, path);
494 st->ss->free_super(st);
495 free(info);
496 break;
497 }
498 sysfs_free(sra);
499 }
500 /* Only trigger a change if we wrote a new map file */
501 if (map_write(map))
502 for (md = mdstat ; md ; md = md->next) {
503 struct mdinfo *sra = sysfs_read(-1, md->devnm,
504 GET_VERSION);
505 if (sra)
506 sysfs_uevent(sra, "change");
507 sysfs_free(sra);
508 }
509 map_free(map);
510 free_mdstat(mdstat);
511 }