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