forked from neilbrown/mdadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dump.c
311 lines (293 loc) · 7.32 KB
/
Dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2013 Neil Brown <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* Author: Neil Brown
* Email: <[email protected]>
*/
#include "mdadm.h"
#include <sys/dir.h>
int Dump_metadata(char *dev, char *dir, struct context *c,
struct supertype *st)
{
/* create a new file in 'dir' named for the basename of 'dev'.
* Truncate to the same size as 'dev' and ask the metadata
* handler to copy metadata there.
* For every name in /dev/disk/by-id that points to this device,
* create a hardlink in 'dir'.
* Complain if any of those hardlinks cannot be created.
*/
int fd, fl;
struct stat stb, dstb;
char *base;
char *fname = NULL;
unsigned long long size;
DIR *dirp;
struct dirent *de;
if (stat(dir, &stb) != 0 ||
(S_IFMT & stb.st_mode) != S_IFDIR) {
pr_err("--dump requires an existing directory, not: %s\n",
dir);
return 16;
}
fd = dev_open(dev, O_RDONLY);
if (fd < 0) {
pr_err("Cannot open %s to dump metadata: %s\n",
dev, strerror(errno));
return 1;
}
if (!get_dev_size(fd, dev, &size)) {
close(fd);
return 1;
}
if (st == NULL)
st = guess_super_type(fd, guess_array);
if (!st) {
pr_err("Cannot find RAID metadata on %s\n", dev);
close(fd);
return 1;
}
st->ignore_hw_compat = 1;
if (st->ss->load_super(st, fd, NULL) != 0) {
pr_err("No %s metadata found on %s\n",
st->ss->name, dev);
close(fd);
return 1;
}
if (st->ss->copy_metadata == NULL) {
pr_err("%s metadata on %s cannot be copied\n",
st->ss->name, dev);
close(fd);
return 1;
}
base = strrchr(dev, '/');
if (base)
base++;
else
base = dev;
xasprintf(&fname, "%s/%s", dir, base);
fl = open(fname, O_RDWR|O_CREAT|O_EXCL, 0666);
if (fl < 0) {
pr_err("Cannot create dump file %s: %s\n",
fname, strerror(errno));
close(fd);
free(fname);
return 1;
}
if (ftruncate(fl, size) < 0) {
pr_err("failed to set size of dump file: %s\n",
strerror(errno));
close(fd);
close(fl);
free(fname);
return 1;
}
if (st->ss->copy_metadata(st, fd, fl) != 0) {
pr_err("Failed to copy metadata from %s to %s\n",
dev, fname);
close(fd);
close(fl);
unlink(fname);
free(fname);
return 1;
}
if (c->verbose >= 0)
printf("%s saved as %s.\n", dev, fname);
fstat(fd, &dstb);
close(fd);
close(fl);
if ((dstb.st_mode & S_IFMT) != S_IFBLK) {
/* Not a block device, so cannot create links */
free(fname);
return 0;
}
/* mostly done: just want to find some other names */
dirp = opendir("/dev/disk/by-id");
if (!dirp) {
free(fname);
return 0;
}
while ((de = readdir(dirp)) != NULL) {
char *p = NULL;
if (de->d_name[0] == '.')
continue;
xasprintf(&p, "/dev/disk/by-id/%s", de->d_name);
if (stat(p, &stb) != 0 ||
(stb.st_mode & S_IFMT) != S_IFBLK ||
stb.st_rdev != dstb.st_rdev) {
/* Not this one */
free(p);
continue;
}
free(p);
xasprintf(&p, "%s/%s", dir, de->d_name);
if (link(fname, p) == 0) {
if (c->verbose >= 0)
printf("%s also saved as %s.\n",
dev, p);
} else {
pr_err("Could not save %s as %s!!\n",
dev, p);
}
free(p);
}
closedir(dirp);
free(fname);
return 0;
}
int Restore_metadata(char *dev, char *dir, struct context *c,
struct supertype *st, int only)
{
/* If 'dir' really is a directory we choose a name
* from it that matches a suitable name in /dev/disk/by-id,
* and copy metadata from the file to the device.
* If two names from by-id match and aren't both the same
* inode, we fail. If none match and basename of 'dev'
* can be found in dir, use that.
* If 'dir' is really a file then it is only permitted if
* 'only' is set (meaning there was only one device given)
* and the metadata is restored irrespective of file names.
*/
int fd, fl;
struct stat stb, dstb;
char *fname = NULL;
unsigned long long size;
if (stat(dir, &stb) != 0) {
pr_err("%s does not exist: cannot restore from there.\n",
dir);
return 16;
} else if ((S_IFMT & stb.st_mode) != S_IFDIR && !only) {
pr_err("--restore requires a directory when multiple devices given\n");
return 16;
}
fd = dev_open(dev, O_RDWR);
if (fd < 0) {
pr_err("Cannot open %s to restore metadata: %s\n",
dev, strerror(errno));
return 1;
}
if (!get_dev_size(fd, dev, &size)) {
close(fd);
return 1;
}
if ((S_IFMT & stb.st_mode) == S_IFDIR) {
/* choose one name from the directory. */
DIR *d = opendir(dir);
struct dirent *de;
char *chosen = NULL;
unsigned int chosen_inode = 0;
fstat(fd, &dstb);
while (d && (de = readdir(d)) != NULL) {
if (de->d_name[0] == '.')
continue;
xasprintf(&fname, "/dev/disk/by-id/%s", de->d_name);
if (stat(fname, &stb) != 0) {
free(fname);
continue;
}
free(fname);
if ((S_IFMT & stb.st_mode) != S_IFBLK)
continue;
if (stb.st_rdev != dstb.st_rdev)
continue;
/* This file is a good match for our device. */
xasprintf(&fname, "%s/%s", dir, de->d_name);
if (stat(fname, &stb) != 0) {
/* Weird! */
free(fname);
continue;
}
if (chosen == NULL) {
chosen = fname;
chosen_inode = stb.st_ino;
continue;
}
if (chosen_inode == stb.st_ino) {
/* same, no need to change */
free(fname);
continue;
}
/* Oh dear, two names both match. Must give up. */
pr_err("Both %s and %s seem suitable for %s. Please choose one.\n",
chosen, fname, dev);
free(fname);
free(chosen);
close(fd);
closedir(d);
return 1;
}
closedir(d);
if (!chosen) {
/* One last chance: try basename of device */
char *base = strrchr(dev, '/');
if (base)
base++;
else
base = dev;
xasprintf(&fname, "%s/%s", dir, base);
if (stat(fname, &stb) == 0)
chosen = fname;
else
free(fname);
}
fname = chosen;
} else
fname = strdup(dir);
if (!fname) {
pr_err("Cannot find suitable file in %s for %s\n",
dir, dev);
close(fd);
return 1;
}
fl = open(fname, O_RDONLY);
if (!fl) {
pr_err("Could not open %s for --restore.\n",
fname);
goto err;
}
if (((unsigned long long)stb.st_size) != size) {
pr_err("%s is not the same size as %s - cannot restore.\n",
fname, dev);
goto err;
}
if (st == NULL)
st = guess_super_type(fl, guess_array);
if (!st) {
pr_err("Cannot find metadata on %s\n", fname);
goto err;
}
st->ignore_hw_compat = 1;
if (st->ss->load_super(st, fl, NULL) != 0) {
pr_err("No %s metadata found on %s\n",
st->ss->name, fname);
goto err;
}
if (st->ss->copy_metadata == NULL) {
pr_err("%s metadata on %s cannot be copied\n",
st->ss->name, dev);
goto err;
}
if (st->ss->copy_metadata(st, fl, fd) != 0) {
pr_err("Failed to copy metadata from %s to %s\n",
fname, dev);
goto err;
}
if (c->verbose >= 0)
printf("%s restored from %s.\n", dev, fname);
return 0;
err:
close(fd);
close(fl);
free(fname);
return 1;
}