]>
Commit | Line | Data |
---|---|---|
8382f19b NB |
1 | /* |
2 | * mapfile - manage /var/run/mdadm.map. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006 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 | * more. 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: | |
1522c538 NB |
36 | * Device id - mdX or mdpX where X is a number. |
37 | * metadata - 0.90 1.0 1.1 1.2 ddf ... | |
8382f19b NB |
38 | * UUID - uuid of the array |
39 | * path - path where device created: /dev/md/home | |
40 | * | |
41 | */ | |
42 | ||
43 | ||
44 | #include "mdadm.h" | |
45 | ||
46 | ||
47 | int map_write(struct map_ent *mel) | |
48 | { | |
49 | FILE *f; | |
50 | int err; | |
51 | int subdir = 1; | |
52 | ||
53 | f = fopen("/var/run/mdadm/map.new", "w"); | |
54 | if (!f) { | |
55 | f = fopen("/var/run/mdadm.map.new", "w"); | |
35e3446b | 56 | subdir = 0; |
8382f19b NB |
57 | } |
58 | if (!f) | |
59 | return 0; | |
195254b8 N |
60 | for (; mel; mel = mel->next) { |
61 | if (mel->bad) | |
62 | continue; | |
8382f19b NB |
63 | if (mel->devnum < 0) |
64 | fprintf(f, "mdp%d ", -1-mel->devnum); | |
65 | else | |
66 | fprintf(f, "md%d ", mel->devnum); | |
1522c538 | 67 | fprintf(f, "%s ", mel->metadata); |
8382f19b NB |
68 | fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0], |
69 | mel->uuid[1], mel->uuid[2], mel->uuid[3]); | |
70 | fprintf(f, "%s\n", mel->path); | |
8382f19b NB |
71 | } |
72 | fflush(f); | |
73 | err = ferror(f); | |
74 | fclose(f); | |
75 | if (err) { | |
76 | if (subdir) | |
77 | unlink("/var/run/mdadm/map.new"); | |
78 | else | |
79 | unlink("/var/run/mdadm.map.new"); | |
80 | return 0; | |
81 | } | |
82 | if (subdir) | |
83 | return rename("/var/run/mdadm/map.new", | |
84 | "/var/run/mdadm/map") == 0; | |
85 | else | |
86 | return rename("/var/run/mdadm.map.new", | |
87 | "/var/run/mdadm.map") == 0; | |
88 | } | |
89 | ||
ad5bc697 N |
90 | |
91 | static int lfd = -1; | |
92 | static int lsubdir = 0; | |
93 | int map_lock(struct map_ent **melp) | |
94 | { | |
95 | if (lfd < 0) { | |
96 | lfd = open("/var/run/mdadm/map.lock", O_CREAT|O_RDWR, 0600); | |
97 | if (lfd < 0) { | |
98 | lfd = open("/var/run/mdadm.map.lock", O_CREAT|O_RDWR, 0600); | |
99 | lsubdir = 0; | |
100 | } else | |
101 | lsubdir = 1; | |
102 | if (lfd < 0) | |
103 | return -1; | |
104 | if (lockf(lfd, F_LOCK, 0) != 0) { | |
105 | close(lfd); | |
106 | lfd = -1; | |
107 | return -1; | |
108 | } | |
109 | } | |
110 | if (*melp) | |
111 | map_free(*melp); | |
112 | map_read(melp); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | void map_unlock(struct map_ent **melp) | |
117 | { | |
118 | if (lfd >= 0) | |
119 | close(lfd); | |
120 | if (lsubdir) | |
121 | unlink("/var/run/mdadm/map.lock"); | |
122 | else | |
123 | unlink("/var/run/mdadm.map.lock"); | |
124 | lfd = -1; | |
125 | } | |
126 | ||
8382f19b | 127 | void map_add(struct map_ent **melp, |
1522c538 | 128 | int devnum, char *metadata, int uuid[4], char *path) |
8382f19b NB |
129 | { |
130 | struct map_ent *me = malloc(sizeof(*me)); | |
131 | ||
132 | me->devnum = devnum; | |
1522c538 | 133 | strcpy(me->metadata, metadata); |
8382f19b NB |
134 | memcpy(me->uuid, uuid, 16); |
135 | me->path = strdup(path); | |
136 | me->next = *melp; | |
195254b8 | 137 | me->bad = 0; |
8382f19b NB |
138 | *melp = me; |
139 | } | |
140 | ||
141 | void map_read(struct map_ent **melp) | |
142 | { | |
143 | FILE *f; | |
144 | char buf[8192]; | |
145 | char path[200]; | |
1522c538 NB |
146 | int devnum, uuid[4]; |
147 | char metadata[30]; | |
8382f19b NB |
148 | char nam[4]; |
149 | ||
150 | *melp = NULL; | |
151 | ||
152 | f = fopen("/var/run/mdadm/map", "r"); | |
3a56f223 N |
153 | if (!f) |
154 | f = fopen("/var/run/mdadm.map", "r"); | |
155 | if (!f) { | |
156 | RebuildMap(); | |
157 | f = fopen("/var/run/mdadm/map", "r"); | |
158 | } | |
8382f19b NB |
159 | if (!f) |
160 | f = fopen("/var/run/mdadm.map", "r"); | |
161 | if (!f) | |
162 | return; | |
163 | ||
164 | while (fgets(buf, sizeof(buf), f)) { | |
c5afc314 | 165 | if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s", |
1522c538 | 166 | nam, &devnum, metadata, uuid, uuid+1, |
c5afc314 N |
167 | uuid+2, uuid+3, path) == 8) { |
168 | if (strncmp(nam, "md", 2) != 0) | |
169 | continue; | |
170 | if (nam[2] == 'p') | |
8382f19b | 171 | devnum = -1 - devnum; |
1522c538 | 172 | map_add(melp, devnum, metadata, uuid, path); |
8382f19b NB |
173 | } |
174 | } | |
175 | fclose(f); | |
176 | } | |
177 | ||
178 | void map_free(struct map_ent *map) | |
179 | { | |
180 | while (map) { | |
181 | struct map_ent *mp = map; | |
182 | map = mp->next; | |
183 | free(mp->path); | |
184 | free(mp); | |
185 | } | |
186 | } | |
187 | ||
1522c538 | 188 | int map_update(struct map_ent **mpp, int devnum, char *metadata, |
8382f19b NB |
189 | int *uuid, char *path) |
190 | { | |
191 | struct map_ent *map, *mp; | |
192 | int rv; | |
193 | ||
194 | if (mpp && *mpp) | |
195 | map = *mpp; | |
196 | else | |
197 | map_read(&map); | |
198 | ||
199 | for (mp = map ; mp ; mp=mp->next) | |
200 | if (mp->devnum == devnum) { | |
1522c538 | 201 | strcpy(mp->metadata, metadata); |
8382f19b NB |
202 | memcpy(mp->uuid, uuid, 16); |
203 | free(mp->path); | |
204 | mp->path = strdup(path); | |
205 | break; | |
206 | } | |
207 | if (!mp) | |
1522c538 | 208 | map_add(&map, devnum, metadata, uuid, path); |
a04d5763 N |
209 | if (mpp) |
210 | *mpp = NULL; | |
8382f19b NB |
211 | rv = map_write(map); |
212 | map_free(map); | |
213 | return rv; | |
214 | } | |
215 | ||
216 | void map_delete(struct map_ent **mapp, int devnum) | |
217 | { | |
218 | struct map_ent *mp; | |
219 | ||
220 | if (*mapp == NULL) | |
221 | map_read(mapp); | |
222 | ||
223 | for (mp = *mapp; mp; mp = *mapp) { | |
224 | if (mp->devnum == devnum) { | |
225 | *mapp = mp->next; | |
226 | free(mp->path); | |
227 | free(mp); | |
228 | } else | |
229 | mapp = & mp->next; | |
230 | } | |
231 | } | |
232 | ||
233 | struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4]) | |
234 | { | |
235 | struct map_ent *mp; | |
236 | if (!*map) | |
237 | map_read(map); | |
238 | ||
195254b8 N |
239 | for (mp = *map ; mp ; mp = mp->next) { |
240 | if (memcmp(uuid, mp->uuid, 16) != 0) | |
241 | continue; | |
242 | if (!mddev_busy(mp->devnum)) { | |
243 | mp->bad = 1; | |
244 | continue; | |
245 | } | |
246 | return mp; | |
247 | } | |
8382f19b | 248 | return NULL; |
4ccad7b1 | 249 | } |
8382f19b | 250 | |
4ccad7b1 N |
251 | struct map_ent *map_by_devnum(struct map_ent **map, int devnum) |
252 | { | |
253 | struct map_ent *mp; | |
254 | if (!*map) | |
255 | map_read(map); | |
256 | ||
195254b8 N |
257 | for (mp = *map ; mp ; mp = mp->next) { |
258 | if (mp->devnum != devnum) | |
259 | continue; | |
260 | if (!mddev_busy(mp->devnum)) { | |
261 | mp->bad = 1; | |
262 | continue; | |
263 | } | |
264 | return mp; | |
265 | } | |
4ccad7b1 | 266 | return NULL; |
8382f19b | 267 | } |
f2e55ecc N |
268 | |
269 | struct map_ent *map_by_name(struct map_ent **map, char *name) | |
270 | { | |
271 | struct map_ent *mp; | |
272 | if (!*map) | |
273 | map_read(map); | |
274 | ||
275 | for (mp = *map ; mp ; mp = mp->next) { | |
276 | if (strncmp(mp->path, "/dev/md/", 8) != 0) | |
277 | continue; | |
195254b8 N |
278 | if (strcmp(mp->path+8, name) != 0) |
279 | continue; | |
280 | if (!mddev_busy(mp->devnum)) { | |
281 | mp->bad = 1; | |
282 | continue; | |
283 | } | |
284 | return mp; | |
f2e55ecc N |
285 | } |
286 | return NULL; | |
287 | } | |
3a56f223 N |
288 | |
289 | void RebuildMap(void) | |
290 | { | |
291 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
292 | struct mdstat_ent *md; | |
293 | struct map_ent *map = NULL; | |
294 | int mdp = get_mdp_major(); | |
295 | ||
296 | for (md = mdstat ; md ; md = md->next) { | |
297 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); | |
298 | struct mdinfo *sd; | |
299 | ||
300 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
301 | char dn[30]; | |
302 | int dfd; | |
303 | int ok; | |
304 | struct supertype *st; | |
305 | char *path; | |
306 | struct mdinfo info; | |
307 | ||
308 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); | |
309 | dfd = dev_open(dn, O_RDONLY); | |
310 | if (dfd < 0) | |
311 | continue; | |
312 | st = guess_super(dfd); | |
313 | if ( st == NULL) | |
314 | ok = -1; | |
315 | else | |
316 | ok = st->ss->load_super(st, dfd, NULL); | |
317 | close(dfd); | |
318 | if (ok != 0) | |
319 | continue; | |
320 | st->ss->getinfo_super(st, &info); | |
321 | if (md->devnum > 0) | |
322 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
323 | else | |
324 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
8a659c33 N |
325 | map_add(&map, md->devnum, |
326 | info.text_version, | |
3a56f223 N |
327 | info.uuid, path ? : "/unknown"); |
328 | st->ss->free_super(st); | |
329 | break; | |
330 | } | |
8a659c33 | 331 | sysfs_free(sra); |
3a56f223 N |
332 | } |
333 | map_write(map); | |
334 | map_free(map); | |
8a659c33 N |
335 | for (md = mdstat ; md ; md = md->next) { |
336 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION); | |
337 | sysfs_uevent(sra, "change"); | |
338 | sysfs_free(sra); | |
339 | } | |
78fbcc10 | 340 | free_mdstat(mdstat); |
3a56f223 | 341 | } |