]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
3cc4d0db | 2 | * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved. |
dfc130f3 | 3 | * |
2bd0ea18 NS |
4 | * This program is free software; you can redistribute it and/or modify it |
5 | * under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
dfc130f3 | 7 | * |
2bd0ea18 NS |
8 | * This program is distributed in the hope that it would be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
dfc130f3 | 11 | * |
2bd0ea18 NS |
12 | * Further, this software is distributed without any warranty that it is |
13 | * free of the rightful claim of any third person regarding infringement | |
14 | * or the like. Any license provided herein, whether implied or | |
15 | * otherwise, applies only to this software file. Patent licenses, if | |
16 | * any, provided herein do not apply to combinations of this program with | |
17 | * other software, or any other product whatsoever. | |
dfc130f3 | 18 | * |
2bd0ea18 NS |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | |
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
dfc130f3 | 22 | * |
2bd0ea18 NS |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
24 | * Mountain View, CA 94043, or: | |
dfc130f3 RC |
25 | * |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
2bd0ea18 NS |
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
31 | */ | |
32 | ||
2bd0ea18 | 33 | #include <libxfs.h> |
2bd0ea18 NS |
34 | #include "agheader.h" |
35 | #include "globals.h" | |
36 | #include "protos.h" | |
37 | #include "err_protos.h" | |
38 | ||
92217f0c NS |
39 | #define BSIZE (1024 * 1024) |
40 | ||
41 | #define XFS_AG_BYTES(bblog) ((long long)BBSIZE << (bblog)) | |
42 | #define XFS_AG_MIN_BYTES ((XFS_AG_BYTES(15))) /* 16 MB */ | |
2bd0ea18 NS |
43 | |
44 | /* | |
45 | * copy the fields of a superblock that are present in primary and | |
46 | * secondaries -- preserve fields that are different in the primary. | |
47 | */ | |
48 | void | |
49 | copy_sb(xfs_sb_t *source, xfs_sb_t *dest) | |
50 | { | |
51 | xfs_ino_t rootino; | |
52 | xfs_ino_t rbmino; | |
53 | xfs_ino_t rsumino; | |
54 | xfs_ino_t uquotino; | |
b36eef04 | 55 | xfs_ino_t gquotino; |
2bd0ea18 NS |
56 | __uint16_t versionnum; |
57 | ||
58 | rootino = dest->sb_rootino; | |
59 | rbmino = dest->sb_rbmino; | |
60 | rsumino = dest->sb_rsumino; | |
61 | uquotino = dest->sb_uquotino; | |
b36eef04 | 62 | gquotino = dest->sb_gquotino; |
2bd0ea18 NS |
63 | |
64 | versionnum = dest->sb_versionnum; | |
65 | ||
66 | *dest = *source; | |
67 | ||
68 | dest->sb_rootino = rootino; | |
69 | dest->sb_rbmino = rbmino; | |
70 | dest->sb_rsumino = rsumino; | |
71 | dest->sb_uquotino = uquotino; | |
b36eef04 | 72 | dest->sb_gquotino = gquotino; |
2bd0ea18 NS |
73 | |
74 | dest->sb_versionnum = versionnum; | |
75 | ||
76 | /* | |
77 | * copy over version bits that are stamped into all | |
78 | * secondaries and cannot be changed at run time in | |
79 | * the primary superblock | |
80 | */ | |
81 | if (XFS_SB_VERSION_HASDALIGN(source)) | |
82 | XFS_SB_VERSION_ADDDALIGN(dest); | |
83 | if (XFS_SB_VERSION_HASEXTFLGBIT(source)) | |
84 | XFS_SB_VERSION_ADDEXTFLGBIT(dest); | |
85 | ||
86 | /* | |
87 | * these are all supposed to be zero or will get reset anyway | |
88 | */ | |
89 | dest->sb_icount = 0; | |
90 | dest->sb_ifree = 0; | |
91 | dest->sb_fdblocks = 0; | |
92 | dest->sb_frextents = 0; | |
93 | ||
94 | bzero(source->sb_fname, 12); | |
95 | } | |
96 | ||
2bd0ea18 NS |
97 | /* |
98 | * find a secondary superblock, copy it into the sb buffer | |
99 | */ | |
100 | int | |
101 | find_secondary_sb(xfs_sb_t *rsb) | |
102 | { | |
103 | xfs_off_t off; | |
104 | xfs_sb_t *sb; | |
105 | xfs_sb_t bufsb; | |
106 | char *c_bufsb; | |
107 | int done; | |
108 | int i; | |
109 | int dirty; | |
110 | int retval; | |
111 | int bsize; | |
112 | ||
507f4e33 | 113 | do_warn(_("\nattempting to find secondary superblock...\n")); |
2bd0ea18 NS |
114 | |
115 | sb = (xfs_sb_t *) memalign(MEM_ALIGN, BSIZE); | |
116 | if (!sb) { | |
117 | do_error( | |
507f4e33 | 118 | _("error finding secondary superblock -- failed to memalign buffer\n")); |
2bd0ea18 NS |
119 | exit(1); |
120 | } | |
121 | ||
122 | bzero(&bufsb, sizeof(xfs_sb_t)); | |
123 | retval = 0; | |
124 | dirty = 0; | |
125 | bsize = 0; | |
126 | ||
127 | /* | |
128 | * skip first sector since we know that's bad | |
129 | */ | |
130 | for (done = 0, off = XFS_AG_MIN_BYTES; !done ; off += bsize) { | |
131 | /* | |
132 | * read disk 1 MByte at a time. | |
133 | */ | |
134 | if (lseek64(fs_fd, off, SEEK_SET) != off) { | |
135 | done = 1; | |
136 | } | |
137 | ||
138 | if (!done && (bsize = read(fs_fd, sb, BSIZE)) == 0) { | |
139 | done = 1; | |
140 | } | |
141 | ||
142 | do_warn("."); | |
143 | ||
144 | /* | |
145 | * check the buffer 512 bytes at a time since | |
146 | * we don't know how big the sectors really are. | |
147 | */ | |
148 | for (i = 0; !done && i < bsize; i += BBSIZE) { | |
149 | c_bufsb = (char *) sb + i; | |
46eca962 | 150 | libxfs_xlate_sb(c_bufsb, &bufsb, 1, XFS_SB_ALL_BITS); |
2bd0ea18 NS |
151 | |
152 | if (verify_sb(&bufsb, 0) != XR_OK) | |
153 | continue; | |
154 | ||
507f4e33 | 155 | do_warn(_("found candidate secondary superblock...\n")); |
2bd0ea18 NS |
156 | |
157 | /* | |
158 | * found one. now verify it by looking | |
159 | * for other secondaries. | |
160 | */ | |
8ebd0722 | 161 | bcopy(&bufsb, rsb, sizeof(xfs_sb_t)); |
2bd0ea18 NS |
162 | rsb->sb_inprogress = 0; |
163 | clear_sunit = 1; | |
164 | ||
165 | if (verify_set_primary_sb(rsb, 0, &dirty) == XR_OK) { | |
507f4e33 NS |
166 | do_warn( |
167 | _("verified secondary superblock...\n")); | |
2bd0ea18 NS |
168 | done = 1; |
169 | retval = 1; | |
170 | } else { | |
171 | do_warn( | |
507f4e33 | 172 | _("unable to verify superblock, continuing...\n")); |
2bd0ea18 NS |
173 | } |
174 | } | |
175 | } | |
176 | ||
177 | free(sb); | |
178 | return(retval); | |
179 | } | |
180 | ||
181 | /* | |
182 | * calculate what inode alignment field ought to be | |
183 | * based on internal superblock info | |
184 | */ | |
185 | int | |
186 | calc_ino_align(xfs_sb_t *sb) | |
187 | { | |
188 | xfs_extlen_t align; | |
189 | ||
190 | align = XFS_INODE_BIG_CLUSTER_SIZE >> sb->sb_blocklog; | |
191 | ||
192 | return(align); | |
193 | } | |
194 | ||
195 | /* | |
196 | * verify a superblock -- does not verify root inode # | |
197 | * can only check that geometry info is internally | |
198 | * consistent. because of growfs, that's no guarantee | |
199 | * of correctness (e.g. geometry may have changed) | |
200 | * | |
201 | * fields verified or consistency checked: | |
202 | * | |
203 | * sb_magicnum | |
204 | * | |
205 | * sb_versionnum | |
206 | * | |
207 | * sb_inprogress | |
208 | * | |
209 | * sb_blocksize (as a group) | |
210 | * sb_blocklog | |
211 | * | |
212 | * geometry info - sb_dblocks (as a group) | |
213 | * sb_agcount | |
214 | * sb_agblocks | |
215 | * sb_agblklog | |
216 | * | |
217 | * inode info - sb_inodesize (x-checked with geo info) | |
218 | * sb_inopblock | |
219 | * | |
220 | * sector size info - | |
221 | * sb_sectsize | |
222 | * sb_sectlog | |
3cc4d0db NS |
223 | * sb_logsectsize |
224 | * sb_logsectlog | |
2bd0ea18 NS |
225 | * |
226 | * not checked here - | |
227 | * sb_rootino | |
228 | * sb_fname | |
229 | * sb_fpack | |
230 | * sb_logstart | |
231 | * sb_uuid | |
232 | * | |
233 | * ALL real-time fields | |
234 | * final 4 summary counters | |
235 | */ | |
236 | ||
237 | int | |
238 | verify_sb(xfs_sb_t *sb, int is_primary_sb) | |
239 | { | |
240 | __uint32_t bsize; | |
241 | xfs_extlen_t align; | |
242 | int i; | |
dfc130f3 | 243 | |
2bd0ea18 NS |
244 | /* check magic number and version number */ |
245 | ||
246 | if (sb->sb_magicnum != XFS_SB_MAGIC) | |
247 | return(XR_BAD_MAGIC); | |
248 | ||
249 | if (!XFS_SB_GOOD_VERSION(sb)) | |
250 | return(XR_BAD_VERSION); | |
251 | ||
252 | /* does sb think mkfs really finished ? */ | |
253 | ||
254 | if (is_primary_sb && sb->sb_inprogress == 1) | |
255 | return(XR_BAD_INPROGRESS); | |
256 | ||
257 | /* check to make sure blocksize is legal 2^N, 9 <= N <= 16 */ | |
258 | ||
259 | if (sb->sb_blocksize == 0) | |
260 | return(XR_BAD_BLOCKSIZE); | |
261 | ||
262 | bsize = 1; | |
263 | ||
3cc4d0db NS |
264 | for (i = 0; bsize < sb->sb_blocksize && |
265 | i < sizeof(sb->sb_blocksize) * NBBY; i++) | |
2bd0ea18 | 266 | bsize <<= 1; |
2bd0ea18 | 267 | |
3cc4d0db | 268 | if (i < XFS_MIN_BLOCKSIZE_LOG || i > XFS_MAX_BLOCKSIZE_LOG) |
2bd0ea18 NS |
269 | return(XR_BAD_BLOCKSIZE); |
270 | ||
271 | /* check sb blocksize field against sb blocklog field */ | |
272 | ||
273 | if (i != sb->sb_blocklog) | |
274 | return(XR_BAD_BLOCKLOG); | |
275 | ||
276 | /* sanity check ag count, size fields against data size field */ | |
277 | ||
278 | if (sb->sb_dblocks == 0 || | |
3cc4d0db | 279 | sb->sb_dblocks > |
601e7662 | 280 | ((__uint64_t)sb->sb_agcount * sb->sb_agblocks) || |
3cc4d0db | 281 | sb->sb_dblocks < |
601e7662 | 282 | ((__uint64_t)(sb->sb_agcount - 1) * sb->sb_agblocks |
3cc4d0db | 283 | + XFS_MIN_AG_BLOCKS)) |
2bd0ea18 NS |
284 | return(XR_BAD_FS_SIZE_DATA); |
285 | ||
286 | if (sb->sb_agblklog != (__uint8_t)libxfs_log2_roundup(sb->sb_agblocks)) | |
287 | return(XR_BAD_FS_SIZE_DATA); | |
288 | ||
289 | if (sb->sb_inodesize < XFS_DINODE_MIN_SIZE || | |
290 | sb->sb_inodesize > XFS_DINODE_MAX_SIZE || | |
291 | sb->sb_inopblock != howmany(sb->sb_blocksize,sb->sb_inodesize)) | |
292 | return(XR_BAD_INO_SIZE_DATA); | |
293 | ||
3cc4d0db NS |
294 | /* check to make sure sectorsize is legal 2^N, 9 <= N <= 15 */ |
295 | ||
296 | if (sb->sb_sectsize == 0) | |
297 | return(XR_BAD_SECT_SIZE_DATA); | |
2bd0ea18 NS |
298 | |
299 | bsize = 1; | |
300 | ||
3cc4d0db NS |
301 | for (i = 0; bsize < sb->sb_sectsize && |
302 | i < sizeof(sb->sb_sectsize) * NBBY; i++) { | |
2bd0ea18 NS |
303 | bsize <<= 1; |
304 | } | |
305 | ||
3cc4d0db NS |
306 | if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG) |
307 | return(XR_BAD_SECT_SIZE_DATA); | |
308 | ||
309 | /* check sb sectorsize field against sb sectlog field */ | |
310 | ||
311 | if (i != sb->sb_sectlog) | |
2bd0ea18 NS |
312 | return(XR_BAD_SECT_SIZE_DATA); |
313 | ||
3cc4d0db NS |
314 | if (XFS_SB_VERSION_HASSECTOR(sb)) { |
315 | ||
316 | /* check to make sure log sector is legal 2^N, 9 <= N <= 15 */ | |
317 | ||
318 | if (sb->sb_logsectsize == 0) | |
319 | return(XR_BAD_SECT_SIZE_DATA); | |
320 | ||
321 | bsize = 1; | |
322 | ||
323 | for (i = 0; bsize < sb->sb_logsectsize && | |
324 | i < sizeof(sb->sb_logsectsize) * NBBY; i++) { | |
325 | bsize <<= 1; | |
326 | } | |
327 | ||
328 | if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG) | |
329 | return(XR_BAD_SECT_SIZE_DATA); | |
330 | ||
331 | /* check sb log sectorsize field against sb log sectlog field */ | |
332 | ||
333 | if (i != sb->sb_logsectlog) | |
334 | return(XR_BAD_SECT_SIZE_DATA); | |
335 | } | |
336 | ||
2bd0ea18 NS |
337 | /* |
338 | * real-time extent size is always set | |
339 | */ | |
340 | if (sb->sb_rextsize * sb->sb_blocksize > XFS_MAX_RTEXTSIZE) | |
341 | return(XR_BAD_RT_GEO_DATA); | |
342 | ||
343 | if (sb->sb_rextsize * sb->sb_blocksize < XFS_MIN_RTEXTSIZE) | |
344 | return(XR_BAD_RT_GEO_DATA); | |
345 | ||
346 | if (sb->sb_rblocks == 0) { | |
347 | if (sb->sb_rextents != 0) | |
348 | return(XR_BAD_RT_GEO_DATA); | |
349 | ||
350 | if (sb->sb_rbmblocks != 0) | |
351 | return(XR_BAD_RT_GEO_DATA); | |
352 | ||
353 | if (sb->sb_rextslog != 0) | |
354 | return(XR_BAD_RT_GEO_DATA); | |
355 | ||
356 | if (sb->sb_frextents != 0) | |
357 | return(XR_BAD_RT_GEO_DATA); | |
358 | } else { | |
359 | /* | |
360 | * if we have a real-time partition, sanity-check geometry | |
361 | */ | |
362 | if (sb->sb_rblocks / sb->sb_rextsize != sb->sb_rextents) | |
363 | return(XR_BAD_RT_GEO_DATA); | |
364 | ||
365 | if (sb->sb_rextslog != | |
366 | libxfs_highbit32((unsigned int)sb->sb_rextents)) | |
367 | return(XR_BAD_RT_GEO_DATA); | |
368 | ||
369 | if (sb->sb_rbmblocks != (xfs_extlen_t) howmany(sb->sb_rextents, | |
370 | NBBY * sb->sb_blocksize)) | |
371 | return(XR_BAD_RT_GEO_DATA); | |
372 | } | |
373 | ||
374 | /* | |
375 | * verify correctness of inode alignment if it's there | |
376 | */ | |
377 | if (XFS_SB_VERSION_HASALIGN(sb)) { | |
378 | align = calc_ino_align(sb); | |
379 | ||
380 | if (align != sb->sb_inoalignmt) | |
381 | return(XR_BAD_INO_ALIGN); | |
382 | } | |
383 | ||
384 | /* | |
385 | * verify max. % of inodes (sb_imax_pct) | |
386 | */ | |
387 | if (sb->sb_imax_pct > 100) | |
388 | return(XR_BAD_INO_MAX_PCT); | |
389 | ||
390 | /* | |
391 | * verify stripe alignment fields if present | |
392 | */ | |
393 | if (XFS_SB_VERSION_HASDALIGN(sb)) { | |
dfc130f3 RC |
394 | if ((!sb->sb_unit && sb->sb_width) || |
395 | (sb->sb_unit && sb->sb_agblocks % sb->sb_unit)) | |
2bd0ea18 NS |
396 | return(XR_BAD_SB_UNIT); |
397 | if ((sb->sb_unit && !sb->sb_width) || | |
398 | (sb->sb_width && sb->sb_unit && sb->sb_width % sb->sb_unit)) | |
399 | return(XR_BAD_SB_WIDTH); | |
400 | } | |
401 | ||
402 | /* | |
403 | * if shared bit is set, verify that the version number is sane | |
404 | */ | |
405 | if (XFS_SB_VERSION_HASSHARED(sb)) { | |
406 | if (sb->sb_shared_vn > XFS_SB_MAX_SHARED_VN) | |
407 | return(XR_BAD_SVN); | |
408 | } | |
409 | ||
410 | /* | |
411 | * mkfs's that stamped a feature bit besides the ones in the | |
412 | * mask below could leave garbage in the secondary superblock | |
413 | * sectors. Anything stamping the shared fs bit or better into | |
414 | * the secondaries is ok and should generate clean secondary | |
415 | * superblock sectors. | |
416 | * | |
417 | * check primary and clean secondary superblocks more strictly | |
418 | */ | |
419 | if (is_primary_sb || sb->sb_versionnum & XR_PART_SECSB_VNMASK) { | |
420 | /* | |
421 | * return errors if shared vn or alignment fields | |
422 | * are set without their feature bits being set | |
423 | */ | |
27527004 NS |
424 | if ((!pre_65_beta && (sb->sb_versionnum & XR_PART_SECSB_VNMASK)) || |
425 | (pre_65_beta && (sb->sb_versionnum & XR_ALPHA_SECSB_VNMASK))) { | |
2bd0ea18 NS |
426 | /* |
427 | * shared version # and inode alignment fields | |
428 | * should be valid | |
429 | */ | |
430 | if (sb->sb_shared_vn && !XFS_SB_VERSION_HASSHARED(sb)) | |
431 | return(XR_BAD_SVN); | |
432 | if (sb->sb_inoalignmt && !XFS_SB_VERSION_HASALIGN(sb)) | |
433 | return(XR_BAD_INO_ALIGN); | |
434 | } | |
435 | if ((!pre_65_beta && | |
436 | (sb->sb_versionnum & XR_GOOD_SECSB_VNMASK)) || | |
437 | (pre_65_beta && | |
438 | (sb->sb_versionnum & XFS_SB_VERSION_DALIGNBIT))) { | |
439 | /* | |
440 | * stripe alignment values should be valid | |
441 | */ | |
442 | if (sb->sb_unit && !XFS_SB_VERSION_HASDALIGN(sb)) | |
443 | return(XR_BAD_SB_UNIT); | |
444 | if (sb->sb_width && !XFS_SB_VERSION_HASDALIGN(sb)) | |
445 | return(XR_BAD_SB_WIDTH); | |
446 | } | |
447 | ||
448 | #if 0 | |
449 | /* | |
450 | * checks involving later superblock fields get added here... | |
451 | */ | |
452 | if (sb->sb_versionnum & XR_GOOD_SECSB_VNMASK) { | |
453 | } | |
454 | #endif | |
455 | } | |
456 | ||
457 | return(XR_OK); | |
458 | } | |
459 | ||
460 | void | |
461 | write_primary_sb(xfs_sb_t *sbp, int size) | |
462 | { | |
dfc130f3 RC |
463 | void *buf; |
464 | ||
2bd0ea18 NS |
465 | if (no_modify) |
466 | return; | |
dfc130f3 RC |
467 | |
468 | if ((buf = calloc(size, 1)) == NULL) { | |
507f4e33 | 469 | do_error(_("failed to malloc superblock buffer\n")); |
dfc130f3 | 470 | return; |
2bd0ea18 NS |
471 | } |
472 | ||
473 | if (lseek64(fs_fd, 0LL, SEEK_SET) != 0LL) { | |
dfc130f3 | 474 | free(buf); |
507f4e33 | 475 | do_error(_("couldn't seek to offset 0 in filesystem\n")); |
dfc130f3 RC |
476 | } |
477 | ||
46eca962 | 478 | libxfs_xlate_sb(buf, sbp, -1, XFS_SB_ALL_BITS); |
2bd0ea18 NS |
479 | |
480 | if (write(fs_fd, buf, size) != size) { | |
dfc130f3 | 481 | free(buf); |
507f4e33 | 482 | do_error(_("primary superblock write failed!\n")); |
dfc130f3 | 483 | } |
2bd0ea18 | 484 | |
dfc130f3 | 485 | free(buf); |
2bd0ea18 NS |
486 | } |
487 | ||
488 | /* | |
489 | * get a possible superblock -- don't check for internal consistency | |
490 | */ | |
491 | int | |
492 | get_sb(xfs_sb_t *sbp, xfs_off_t off, int size, xfs_agnumber_t agno) | |
493 | { | |
494 | int error, rval; | |
dfc130f3 RC |
495 | void *buf; |
496 | ||
497 | if ((buf = calloc(size, 1)) == NULL) { | |
2bd0ea18 | 498 | do_error( |
507f4e33 | 499 | _("error reading superblock %u -- failed to malloc buffer\n"), |
2bd0ea18 NS |
500 | agno, off); |
501 | exit(1); | |
502 | } | |
503 | ||
504 | /* try and read it first */ | |
505 | ||
506 | if (lseek64(fs_fd, off, SEEK_SET) != off) { | |
507 | do_warn( | |
507f4e33 | 508 | _("error reading superblock %u -- seek to offset %lld failed\n"), |
2bd0ea18 NS |
509 | agno, off); |
510 | return(XR_EOF); | |
511 | } | |
512 | ||
513 | if ((rval = read(fs_fd, buf, size)) != size) { | |
514 | error = errno; | |
515 | do_warn( | |
507f4e33 | 516 | _("superblock read failed, offset %lld, size %d, ag %u, rval %d\n"), |
2bd0ea18 NS |
517 | off, size, rval, agno); |
518 | do_error("%s\n", strerror(error)); | |
519 | } | |
46eca962 | 520 | libxfs_xlate_sb(buf, sbp, 1, XFS_SB_ALL_BITS); |
2bd0ea18 NS |
521 | free(buf); |
522 | ||
523 | return (verify_sb(sbp, 0)); | |
524 | } | |
525 | ||
2bd0ea18 NS |
526 | /* returns element on list with highest reference count */ |
527 | ||
528 | fs_geo_list_t * | |
529 | get_best_geo(fs_geo_list_t *list) | |
530 | { | |
531 | int cnt = 0; | |
532 | fs_geo_list_t *current, *rval = NULL; | |
533 | ||
534 | current = list; | |
535 | ||
536 | while (current != NULL) { | |
537 | if (current->refs > cnt) { | |
538 | rval = current; | |
539 | cnt = current->refs; | |
540 | } | |
541 | current = current->next; | |
542 | } | |
543 | ||
544 | return(rval); | |
545 | } | |
546 | ||
547 | /* adds geometry info to linked list. returns (sometimes new) head of list */ | |
548 | ||
549 | fs_geo_list_t * | |
550 | add_geo(fs_geo_list_t *list, fs_geometry_t *geo_p, int index) | |
551 | { | |
552 | fs_geo_list_t *current = list; | |
dfc130f3 | 553 | |
2bd0ea18 NS |
554 | while (current != NULL) { |
555 | if (memcmp(geo_p, ¤t->geo, sizeof(fs_geometry_t)) == 0) { | |
556 | current->refs++; | |
557 | return(list); | |
558 | } | |
559 | ||
560 | current = current->next; | |
561 | } | |
562 | ||
563 | if ((current = malloc(sizeof(fs_geo_list_t))) == NULL) { | |
507f4e33 | 564 | do_error(_("couldn't malloc geometry structure\n")); |
2bd0ea18 NS |
565 | exit(1); |
566 | } | |
567 | ||
568 | current->geo = *geo_p; | |
569 | current->refs = 1; | |
570 | current->next = list; | |
571 | current->index = index; | |
572 | ||
573 | return(current); | |
574 | } | |
575 | ||
576 | void | |
577 | free_geo(fs_geo_list_t *list) | |
578 | { | |
579 | fs_geo_list_t *next; | |
580 | fs_geo_list_t *current; | |
581 | ||
582 | current = list; | |
583 | ||
584 | for (current = list; current != NULL; current = next) { | |
585 | next = current->next; | |
586 | free(current); | |
587 | } | |
588 | } | |
589 | ||
590 | void | |
591 | get_sb_geometry(fs_geometry_t *geo, xfs_sb_t *sbp) | |
592 | { | |
593 | bzero(geo, sizeof(fs_geometry_t)); | |
594 | ||
595 | /* | |
596 | * blindly set fields that we know are always good | |
597 | */ | |
598 | geo->sb_blocksize = sbp->sb_blocksize; | |
599 | geo->sb_dblocks = sbp->sb_dblocks; | |
600 | geo->sb_rblocks = sbp->sb_rblocks; | |
601 | geo->sb_rextents = sbp->sb_rextents; | |
602 | geo->sb_logstart = sbp->sb_logstart; | |
603 | geo->sb_rextsize = sbp->sb_rextsize; | |
604 | geo->sb_agblocks = sbp->sb_agblocks; | |
605 | geo->sb_agcount = sbp->sb_agcount; | |
606 | geo->sb_rbmblocks = sbp->sb_rbmblocks; | |
607 | geo->sb_logblocks = sbp->sb_logblocks; | |
608 | geo->sb_sectsize = sbp->sb_sectsize; | |
609 | geo->sb_inodesize = sbp->sb_inodesize; | |
610 | ||
611 | if (XFS_SB_VERSION_HASALIGN(sbp)) | |
612 | geo->sb_ialignbit = 1; | |
613 | ||
614 | if (XFS_SB_VERSION_HASSHARED(sbp) || | |
615 | sbp->sb_versionnum & XR_PART_SECSB_VNMASK) | |
616 | geo->sb_sharedbit = 1; | |
617 | ||
618 | if (XFS_SB_VERSION_HASDALIGN(sbp)) | |
619 | geo->sb_salignbit = 1; | |
620 | ||
621 | if (XFS_SB_VERSION_HASEXTFLGBIT(sbp)) | |
622 | geo->sb_extflgbit = 1; | |
623 | ||
624 | /* | |
625 | * protect against pre-6.5 mkfs-generated garbaged | |
626 | * fields in the secondary superblocks. pay attention | |
627 | * to those fields if and only if their corresponding | |
628 | * feature bits are set in the feature bits of the | |
629 | * version number or we can deduce from the version bits | |
630 | * that are set that our field was properly initialized | |
631 | * because a field after the field we care about was | |
632 | * properly initialized as well. | |
633 | */ | |
634 | ||
635 | /* | |
636 | * inode alignment field lives before the data alignment field | |
637 | */ | |
27527004 NS |
638 | if ((!pre_65_beta && (sbp->sb_versionnum & XR_PART_SECSB_VNMASK)) || |
639 | (pre_65_beta && (sbp->sb_versionnum & XR_ALPHA_SECSB_VNMASK))) | |
2bd0ea18 NS |
640 | geo->sb_inoalignmt = sbp->sb_inoalignmt; |
641 | ||
27527004 NS |
642 | if ((!pre_65_beta && (sbp->sb_versionnum & XR_GOOD_SECSB_VNMASK)) || |
643 | (pre_65_beta && XFS_SB_VERSION_HASDALIGN(sbp))) { | |
2bd0ea18 NS |
644 | geo->sb_unit = sbp->sb_unit; |
645 | geo->sb_width = sbp->sb_width; | |
646 | } | |
647 | ||
648 | /* | |
649 | * shared vn always set if either ino or data alignment is on | |
650 | * since that field lives between the quota and inode alignment | |
651 | * fields | |
652 | */ | |
653 | if (sbp->sb_versionnum & XR_PART_SECSB_VNMASK) | |
654 | geo->sb_shared_vn = sbp->sb_shared_vn; | |
655 | ||
656 | /* | |
657 | * superblock fields located after sb_widthfields get set | |
658 | * into the geometry structure only if we can determine | |
659 | * from the features enabled in this superblock whether | |
660 | * or not the sector was bzero'd at mkfs time. | |
661 | */ | |
27527004 NS |
662 | if ((!pre_65_beta && (sbp->sb_versionnum & XR_GOOD_SECSB_VNMASK)) || |
663 | (pre_65_beta && (sbp->sb_versionnum & XR_ALPHA_SECSB_VNMASK))) { | |
2bd0ea18 NS |
664 | geo->sb_fully_zeroed = 1; |
665 | } | |
666 | } | |
667 | ||
668 | /* | |
669 | * the way to verify that a primary sb is consistent with the | |
670 | * filesystem is find the secondaries given the info in the | |
671 | * primary and compare the geometries in the secondaries against | |
672 | * the geometry indicated by the primary. | |
673 | * | |
674 | * returns 1 if bad, 0 if ok | |
675 | */ | |
676 | int | |
677 | verify_set_primary_sb(xfs_sb_t *rsb, | |
678 | int sb_index, | |
679 | int *sb_modified) | |
680 | { | |
507f4e33 | 681 | xfs_off_t off; |
2bd0ea18 NS |
682 | fs_geometry_t geo; |
683 | xfs_sb_t *sb; | |
684 | fs_geo_list_t *list; | |
685 | fs_geo_list_t *current; | |
686 | char *checked; | |
687 | xfs_agnumber_t agno; | |
688 | int num_sbs; | |
689 | int skip; | |
690 | int size; | |
691 | int num_ok; | |
692 | int retval; | |
693 | int round; | |
694 | ||
695 | /* | |
696 | * select the number of secondaries to try for | |
697 | */ | |
698 | num_sbs = MIN(NUM_SBS, rsb->sb_agcount); | |
699 | skip = howmany(num_sbs, rsb->sb_agcount); | |
700 | size = NUM_AGH_SECTS * rsb->sb_sectsize; | |
701 | retval = 0; | |
702 | list = NULL; | |
703 | num_ok = 0; | |
704 | *sb_modified = 0; | |
705 | ||
706 | sb = (xfs_sb_t *) alloc_ag_buf(size); | |
707 | checked = calloc(rsb->sb_agcount, sizeof(char)); | |
708 | if (!checked) { | |
507f4e33 | 709 | do_error(_("calloc failed in verify_set_primary_sb\n")); |
2bd0ea18 NS |
710 | exit(1); |
711 | } | |
712 | ||
713 | /* | |
714 | * put the primary sb geometry info onto the geometry list | |
715 | */ | |
716 | checked[sb_index] = 1; | |
717 | get_sb_geometry(&geo, rsb); | |
718 | list = add_geo(list, &geo, sb_index); | |
719 | ||
720 | /* | |
721 | * grab N secondaries. check them off as we get them | |
722 | * so we only process each one once | |
723 | */ | |
724 | for (round = 0; round < skip; round++) { | |
725 | for (agno = round; agno < rsb->sb_agcount; agno += skip) { | |
726 | if (checked[agno]) | |
727 | continue; | |
728 | ||
729 | off = (xfs_off_t)agno * rsb->sb_agblocks << rsb->sb_blocklog; | |
730 | ||
731 | checked[agno] = 1; | |
732 | ||
733 | if (get_sb(sb, off, size, agno) == XR_EOF) { | |
734 | retval = 1; | |
735 | goto out; | |
736 | } | |
737 | ||
738 | if (verify_sb(sb, 0) == XR_OK) { | |
739 | /* | |
740 | * save away geometry info. | |
741 | * don't bother checking the sb | |
742 | * against the agi/agf as the odds | |
743 | * of the sb being corrupted in a way | |
744 | * that it is internally consistent | |
745 | * but not consistent with the rest | |
746 | * of the filesystem is really really low. | |
747 | */ | |
748 | get_sb_geometry(&geo, sb); | |
749 | list = add_geo(list, &geo, agno); | |
750 | num_ok++; | |
751 | } | |
752 | } | |
753 | } | |
754 | ||
755 | /* | |
756 | * see if we have enough superblocks to bother with | |
757 | */ | |
758 | if (num_ok < num_sbs / 2) | |
759 | return(XR_INSUFF_SEC_SB); | |
760 | ||
761 | current = get_best_geo(list); | |
762 | ||
763 | /* | |
764 | * check that enough sbs agree that we're willing to | |
765 | * go with this geometry. if not, print out the | |
766 | * geometry and a message about the force option. | |
767 | */ | |
768 | switch (num_sbs) { | |
769 | case 2: | |
770 | /* | |
771 | * all them have to be right. if not, report geometry | |
772 | * and get out unless force option is in effect (-F) | |
773 | */ | |
774 | if (current->refs != 2) { | |
775 | if (!force_geo) { | |
507f4e33 NS |
776 | do_warn( |
777 | _("Only two AGs detected and they do not match - cannot proceed.\n")); | |
2bd0ea18 NS |
778 | exit(1); |
779 | } | |
780 | } | |
781 | break; | |
782 | case 1: | |
783 | /* | |
784 | * just report the geometry info and get out. | |
785 | * refuse to run further unless the force (-F) | |
786 | * option is in effect. | |
787 | */ | |
788 | if (!force_geo) { | |
507f4e33 | 789 | do_warn(_("Only one AG detected - cannot proceed.\n")); |
2bd0ea18 NS |
790 | exit(1); |
791 | } | |
792 | default: | |
793 | /* | |
794 | * at least half of the probed superblocks have | |
795 | * to agree. if they don't, this fs is probably | |
796 | * too far gone anyway considering the fact that | |
797 | * XFS normally doesn't alter the secondary superblocks. | |
798 | */ | |
799 | if (current->refs < num_sbs / 2) { | |
507f4e33 NS |
800 | do_warn( |
801 | _("Not enough matching superblocks - cannot proceed.\n")); | |
2bd0ea18 NS |
802 | exit(1); |
803 | } | |
804 | } | |
805 | ||
806 | /* | |
807 | * set the geometry into primary superblock if necessary. | |
808 | */ | |
809 | ||
810 | if (current->index != sb_index) { | |
811 | *sb_modified = 1; | |
dfc130f3 | 812 | off = current->index * current->geo.sb_agblocks |
2bd0ea18 NS |
813 | * current->geo.sb_blocksize; |
814 | if (get_sb(sb, off, current->geo.sb_sectsize, | |
815 | current->index) != XR_OK) | |
507f4e33 | 816 | do_error(_("could not read superblock\n")); |
2bd0ea18 NS |
817 | |
818 | copy_sb(sb, rsb); | |
819 | ||
820 | /* | |
821 | * turn off inprogress bit since this is the primary. | |
822 | * also save away values that we need to ensure are | |
823 | * consistent in the other secondaries. | |
824 | */ | |
825 | rsb->sb_inprogress = 0; | |
826 | sb_inoalignmt = sb->sb_inoalignmt; | |
827 | sb_unit = sb->sb_unit; | |
828 | sb_width = sb->sb_width; | |
829 | } | |
830 | ||
831 | free_geo(list); | |
832 | out: | |
833 | free(sb); | |
834 | free(checked); | |
835 | return(retval); | |
836 | } |