mdadm-1.4.0
[thirdparty/mdadm.git] / util.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
64c4757e
NB
31#include "md_p.h"
32#include <sys/utsname.h>
33
34/*
35 * Parse a 128 bit uuid in 4 integers
36 * format is 32 hexx nibbles with options :.<space> separator
37 * If not exactly 32 hex digits are found, return 0
38 * else return 1
39 */
40int parse_uuid(char *str, int uuid[4])
41{
42 int hit = 0; /* number of Hex digIT */
43 int i;
44 char c;
45 for (i=0; i<4; i++) uuid[i]=0;
46
47 while ((c= *str++)) {
48 int n;
49 if (c>='0' && c<='9')
50 n = c-'0';
51 else if (c>='a' && c <= 'f')
52 n = 10 + c - 'a';
53 else if (c>='A' && c <= 'F')
54 n = 10 + c - 'A';
55 else if (strchr(":. -", c))
56 continue;
57 else return 0;
58
82b27616
NB
59 if (hit<32) {
60 uuid[hit/8] <<= 4;
61 uuid[hit/8] += n;
62 }
64c4757e
NB
63 hit++;
64 }
65 if (hit == 32)
66 return 1;
67 return 0;
68
69}
70
71
72/*
73 * Get the md version number.
74 * We use the RAID_VERSION ioctl if it is supported
75 * If not, but we have a block device with major '9', we assume
76 * 0.36.0
77 *
78 * Return version number as 24 but number - assume version parts
79 * always < 255
80 */
81
82int md_get_version(int fd)
83{
84 struct stat stb;
85 mdu_version_t vers;
86
87 if (fstat(fd, &stb)<0)
88 return -1;
89 if ((S_IFMT&stb.st_mode) != S_IFBLK)
90 return -1;
91
92 if (ioctl(fd, RAID_VERSION, &vers) == 0)
682c7051 93 return (vers.major*10000) + (vers.minor*100) + vers.patchlevel;
5787fa49
NB
94 if (errno == EACCES)
95 return -1;
64c4757e 96 if (MAJOR(stb.st_rdev) == MD_MAJOR)
682c7051 97 return (3600);
64c4757e
NB
98 return -1;
99}
100
101
102int get_linux_version()
103{
104 struct utsname name;
105 int a,b,c;
106 if (uname(&name) <0)
107 return -1;
108
109 if (sscanf(name.release, "%d.%d.%d", &a,&b,&c)!= 3)
110 return -1;
682c7051 111 return (a*1000000)+(b*1000)+c;
64c4757e
NB
112}
113
114int enough(int level, int raid_disks, int avail_disks)
115{
116 switch (level) {
e0d19036
NB
117 case -4:
118 return avail_disks>= 1;
64c4757e
NB
119 case -1:
120 case 0:
121 return avail_disks == raid_disks;
122 case 1:
123 return avail_disks >= 1;
124 case 4:
125 case 5:
126 return avail_disks >= raid_disks-1;
127 default:
128 return 0;
129 }
130}
131
132int same_uuid(int a[4], int b[4])
133{
134 if (a[0]==b[0] &&
135 a[1]==b[1] &&
136 a[2]==b[2] &&
137 a[3]==b[3])
138 return 1;
139 return 0;
140}
141
142void uuid_from_super(int uuid[4], mdp_super_t *super)
143{
144 uuid[0] = super->set_uuid0;
145 if (super->minor_version >= 90) {
146 uuid[1] = super->set_uuid1;
147 uuid[2] = super->set_uuid2;
148 uuid[3] = super->set_uuid3;
149 } else {
150 uuid[1] = 0;
151 uuid[2] = 0;
152 uuid[3] = 0;
153 }
154}
155
156int compare_super(mdp_super_t *first, mdp_super_t *second)
157{
158 /*
159 * return:
160 * 0 same, or first was empty, and second was copied
161 * 1 second had wrong number
162 * 2 wrong uuid
163 * 3 wrong other info
164 */
165 int uuid1[4], uuid2[4];
166 if (second->md_magic != MD_SB_MAGIC)
167 return 1;
168 if (first-> md_magic != MD_SB_MAGIC) {
169 memcpy(first, second, sizeof(*first));
170 return 0;
171 }
172
173 uuid_from_super(uuid1, first);
174 uuid_from_super(uuid2, second);
175 if (!same_uuid(uuid1, uuid2))
176 return 2;
177 if (first->major_version != second->major_version ||
178 first->minor_version != second->minor_version ||
179 first->patch_version != second->patch_version ||
180 first->gvalid_words != second->gvalid_words ||
181 first->ctime != second->ctime ||
182 first->level != second->level ||
183 first->size != second->size ||
184 first->raid_disks != second->raid_disks )
185 return 3;
186
187 return 0;
188}
189
190int load_super(int fd, mdp_super_t *super)
191{
192 /* try to read in the superblock
193 *
194 * return
195 * 0 - success
196 * 1 - no block size
197 * 2 - too small
198 * 3 - no seek
199 * 4 - no read
200 * 5 - no magic
201 * 6 - wrong major version
202 */
e0d19036
NB
203 unsigned long size;
204 unsigned long long offset;
64c4757e
NB
205
206 if (ioctl(fd, BLKGETSIZE, &size))
207 return 1;
208
209 if (size < MD_RESERVED_SECTORS*2)
210 return 2;
211
212 offset = MD_NEW_SIZE_SECTORS(size);
213
214 offset *= 512;
215
5787fa49
NB
216 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
217
64c4757e
NB
218 if (lseek64(fd, offset, 0)< 0LL)
219 return 3;
220
682c7051 221 if (read(fd, super, sizeof(*super)) != sizeof(*super))
64c4757e
NB
222 return 4;
223
224 if (super->md_magic != MD_SB_MAGIC)
225 return 5;
226
227 if (super->major_version != 0)
228 return 6;
229 return 0;
230}
231
82b27616
NB
232int store_super(int fd, mdp_super_t *super)
233{
234 long size;
235 long long offset;
236
237 if (ioctl(fd, BLKGETSIZE, &size))
238 return 1;
239
240 if (size < MD_RESERVED_SECTORS*2)
241 return 2;
242
243 offset = MD_NEW_SIZE_SECTORS(size);
244
245 offset *= 512;
246
247 if (lseek64(fd, offset, 0)< 0LL)
248 return 3;
249
250 if (write(fd, super, sizeof(*super)) != sizeof(*super))
251 return 4;
252
253 return 0;
254}
255
256
682c7051
NB
257
258int check_ext2(int fd, char *name)
259{
260 /*
261 * Check for an ext2fs file system.
262 * Superblock is always 1K at 1K offset
263 *
264 * s_magic is le16 at 56 == 0xEF53
265 * report mtime - le32 at 44
266 * blocks - le32 at 4
267 * logblksize - le32 at 24
268 */
269 unsigned char sb[1024];
270 time_t mtime;
271 int size, bsize;
272 if (lseek(fd, 1024,0)!= 1024)
273 return 0;
274 if (read(fd, sb, 1024)!= 1024)
275 return 0;
276 if (sb[56] != 0x53 || sb[57] != 0xef)
277 return 0;
278
279 mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8;
280 bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8;
281 size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
282 fprintf(stderr, Name ": %s appears to contain an ext2fs file system\n",
283 name);
284 fprintf(stderr," size=%dK mtime=%s",
285 size*(1<<bsize), ctime(&mtime));
286 return 1;
287}
288
289int check_reiser(int fd, char *name)
290{
291 /*
292 * superblock is at 64K
293 * size is 1024;
294 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
295 *
296 */
297 unsigned char sb[1024];
298 int size;
299 if (lseek(fd, 64*1024, 0) != 64*1024)
300 return 0;
301 if (read(fd, sb, 1024) != 1024)
302 return 0;
303 if (strncmp(sb+52, "ReIsErFs",8)!=0 &&
304 strncmp(sb+52, "ReIsEr2Fs",9)!=0)
305 return 0;
306 fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
307 size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
308 fprintf(stderr, " size = %dK\n", size*4);
309
310 return 1;
311}
312
313int check_raid(int fd, char *name)
314{
315 mdp_super_t super;
316 time_t crtime;
317 if (load_super(fd, &super))
318 return 0;
319 /* Looks like a raid array .. */
5787fa49 320 fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
682c7051
NB
321 name);
322 crtime = super.ctime;
b83d95f3 323 fprintf(stderr, " level=%d devices=%d ctime=%s",
682c7051
NB
324 super.level, super.raid_disks, ctime(&crtime));
325 return 1;
326}
327
328
329int ask(char *mesg)
330{
331 char *add = "";
332 int i;
333 for (i=0; i<5; i++) {
334 char buf[100];
335 fprintf(stderr, "%s%s", mesg, add);
336 fflush(stderr);
337 if (fgets(buf, 100, stdin)==NULL)
338 return 0;
339 if (buf[0]=='y' || buf[0]=='Y')
340 return 1;
341 if (buf[0]=='n' || buf[0]=='N')
342 return 0;
343 add = "(y/n) ";
344 }
345 fprintf(stderr, Name ": assuming 'no'\n");
346 return 0;
347}
348
349char *map_num(mapping_t *map, int num)
350{
351 while (map->name) {
352 if (map->num == num)
353 return map->name;
354 map++;
355 }
356 return NULL;
357}
358
359int map_name(mapping_t *map, char *name)
360{
361 while (map->name) {
362 if (strcmp(map->name, name)==0)
363 return map->num;
364 map++;
365 }
366 return -10;
367}
82b27616
NB
368
369/*
370 * convert a major/minor pair for a block device into a name in /dev, if possible.
371 * On the first call, walk /dev collecting name.
372 * Put them in a simple linked listfor now.
373 */
374struct devmap {
375 int major, minor;
376 char *name;
377 struct devmap *next;
378} *devlist = NULL;
379int devlist_ready = 0;
380
5787fa49
NB
381#ifdef UCLIBC
382char *map_dev(int major, int minor)
383{
384#if 0
385 fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
386 major, minor);
387#endif
388 return NULL;
389}
390#else
82b27616
NB
391#define __USE_XOPEN_EXTENDED
392#include <ftw.h>
393
394
395int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
396{
397 if ((stb->st_mode&S_IFMT)== S_IFBLK) {
398 char *n = strdup(name);
399 struct devmap *dm = malloc(sizeof(*dm));
400 if (dm) {
401 dm->major = MAJOR(stb->st_rdev);
402 dm->minor = MINOR(stb->st_rdev);
403 dm->name = n;
404 dm->next = devlist;
405 devlist = dm;
406 }
407 }
408 return 0;
409}
410
411char *map_dev(int major, int minor)
412{
413 struct devmap *p;
414 if (!devlist_ready) {
415 nftw("/dev", add_dev, 10, FTW_PHYS);
416 devlist_ready=1;
417 }
418
419 for (p=devlist; p; p=p->next)
420 if (p->major == major &&
421 p->minor == minor)
422 return p->name;
423 return NULL;
424}
425
5787fa49 426#endif
82b27616
NB
427
428int calc_sb_csum(mdp_super_t *super)
429{
430 unsigned int oldcsum = super->sb_csum;
56eb10c0 431 unsigned long long newcsum = 0;
82b27616
NB
432 unsigned long csum;
433 int i;
434 unsigned int *superc = (int*) super;
435 super->sb_csum = 0;
436
437 for(i=0; i<MD_SB_BYTES/4; i++)
438 newcsum+= superc[i];
439 csum = (newcsum& 0xffffffff) + (newcsum>>32);
440 super->sb_csum = oldcsum;
441 return csum;
442}
cd29a5c8 443
56eb10c0 444char *human_size(long long bytes)
cd29a5c8
NB
445{
446 static char buf[30];
56eb10c0 447
cd29a5c8 448
56eb10c0 449 if (bytes < 5000*1024)
cd29a5c8 450 buf[0]=0;
56eb10c0 451 else if (bytes < 2*1024LL*1024LL*1024LL)
e0d19036 452 sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
56eb10c0 453 (long)(bytes>>20),
bd526cee 454 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 455 (long)(bytes/1000/1000),
bd526cee 456 (long)(((bytes%1000000)+5000)/10000)
56eb10c0 457 );
cd29a5c8 458 else
e0d19036 459 sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
56eb10c0 460 (long)(bytes>>30),
bd526cee 461 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100),
56eb10c0 462 (long)(bytes/1000LL/1000LL/1000LL),
5787fa49 463 (long)((((bytes/1000)%1000000)+5000)/10000)
56eb10c0 464 );
cd29a5c8
NB
465 return buf;
466}
e0d19036
NB
467
468char *human_size_brief(long long bytes)
469{
470 static char buf[30];
471
472
473 if (bytes < 5000*1024)
474 sprintf(buf, "%ld.%02ldKiB",
bd526cee 475 (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
e0d19036
NB
476 );
477 else if (bytes < 2*1024LL*1024LL*1024LL)
478 sprintf(buf, "%ld.%02ldMiB",
479 (long)(bytes>>20),
bd526cee 480 (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
481 );
482 else
483 sprintf(buf, "%ld.%02ldGiB",
484 (long)(bytes>>30),
bd526cee 485 (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
e0d19036
NB
486 );
487 return buf;
488}
489
490
491#define MD_MAJOR 9
492char *get_md_name(int dev)
493{
494 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
495 static char devname[50];
496 struct stat stb;
497 dev_t rdev = MKDEV(MD_MAJOR, dev);
498
499 sprintf(devname, "/dev/md%d", dev);
500 if (stat(devname, &stb) == 0
501 && (S_IFMT&stb.st_mode) == S_IFBLK
502 && (stb.st_rdev == rdev))
503 return devname;
504
505 sprintf(devname, "/dev/md/%d", dev);
506 if (stat(devname, &stb) == 0
507 && (S_IFMT&stb.st_mode) == S_IFBLK
508 && (stb.st_rdev == rdev))
509 return devname;
510
511 sprintf(devname, "/dev/.tmp.md%d", dev);
512 if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
513 return NULL;
514
515 if (stat(devname, &stb) == 0
516 && (S_IFMT&stb.st_mode) == S_IFBLK
517 && (stb.st_rdev == rdev))
518 return devname;
519 unlink(devname);
520 return NULL;
521}
522
523void put_md_name(char *name)
524{
525 if (strncmp(name, "/dev/.tmp.md", 12)==0)
526 unlink(name);
527}