]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - repair/xfs_repair.c
xfs_repair: don't leak buffer on xattr remote buf verifier error
[thirdparty/xfsprogs-dev.git] / repair / xfs_repair.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
dfc130f3 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18 7 * published by the Free Software Foundation.
dfc130f3 8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
dfc130f3 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18
NS
17 */
18
6b803e5a
CH
19#include "libxfs.h"
20#include "libxlog.h"
12be365e 21#include <sys/resource.h>
4a32b9e9 22#include "xfs_multidisk.h"
2bd0ea18
NS
23#include "avl.h"
24#include "avl64.h"
25#include "globals.h"
26#include "versions.h"
27#include "agheader.h"
28#include "protos.h"
29#include "incore.h"
30#include "err_protos.h"
cb5b3ef4 31#include "prefetch.h"
3b6ac903 32#include "threads.h"
06fbdda9 33#include "progress.h"
beed0dc8 34#include "dinode.h"
9e0f480e
DW
35#include "slab.h"
36#include "rmap.h"
2bd0ea18
NS
37
38#define rounddown(x, y) (((x)/(y))*(y))
39
2bd0ea18
NS
40#define XR_MAX_SECT_SIZE (64 * 1024)
41
42/*
43 * option tables for getsubopt calls
44 */
45
46/*
4af916f8 47 * -o: user-supplied override options
2bd0ea18 48 */
98884b66
DW
49enum o_opt_nums {
50 ASSUME_XFS = 0,
51 IHASH_SIZE,
52 BHASH_SIZE,
53 AG_STRIDE,
54 FORCE_GEO,
55 PHASE2_THREADS,
56 O_MAX_OPTS,
57};
58
8b8a6b02 59static char *o_opts[] = {
98884b66
DW
60 [ASSUME_XFS] = "assume_xfs",
61 [IHASH_SIZE] = "ihash",
62 [BHASH_SIZE] = "bhash",
63 [AG_STRIDE] = "ag_stride",
64 [FORCE_GEO] = "force_geometry",
65 [PHASE2_THREADS] = "phase2_threads",
66 [O_MAX_OPTS] = NULL,
2bd0ea18
NS
67};
68
4af916f8
BN
69/*
70 * -c: conversion options
71 */
98884b66
DW
72enum c_opt_nums {
73 CONVERT_LAZY_COUNT = 0,
74 C_MAX_OPTS,
75};
76
8b8a6b02 77static char *c_opts[] = {
98884b66
DW
78 [CONVERT_LAZY_COUNT] = "lazycount",
79 [C_MAX_OPTS] = NULL,
4af916f8
BN
80};
81
82
2556c98b 83static int bhash_option_used;
12be365e 84static long max_mem_specified; /* in megabytes */
364a126c 85static int phase2_threads = 32;
7c3e94a3 86static bool report_corrected;
2556c98b 87
2bd0ea18
NS
88static void
89usage(void)
90{
4af916f8
BN
91 do_warn(_(
92"Usage: %s [options] device\n"
93"\n"
94"Options:\n"
95" -f The device is a file\n"
96" -L Force log zeroing. Do this as a last resort.\n"
97" -l logdev Specifies the device where the external log resides.\n"
98" -m maxmem Maximum amount of memory to be used in megabytes.\n"
99" -n No modify mode, just checks the filesystem for damage.\n"
7c3e94a3 100" (Cannot be used together with -e.)\n"
4af916f8
BN
101" -P Disables prefetching.\n"
102" -r rtdev Specifies the device where the realtime section resides.\n"
103" -v Verbose output.\n"
104" -c subopts Change filesystem parameters - use xfs_admin.\n"
105" -o subopts Override default behaviour, refer to man page.\n"
79e106f0 106" -t interval Reporting interval in seconds.\n"
4af916f8 107" -d Repair dangerously.\n"
7c3e94a3
JT
108" -e Exit with a non-zero code if any errors were repaired.\n"
109" (Cannot be used together with -n.)\n"
4af916f8 110" -V Reports version and exits.\n"), progname);
2bd0ea18
NS
111 exit(1);
112}
113
2bd0ea18
NS
114char *
115err_string(int err_code)
116{
507f4e33
NS
117 static char *err_message[XR_BAD_ERR_CODE];
118 static int done;
119
120 if (!done) {
121 err_message[XR_OK] = _("no error");
122 err_message[XR_BAD_MAGIC] = _("bad magic number");
123 err_message[XR_BAD_BLOCKSIZE] = _("bad blocksize field");
124 err_message[XR_BAD_BLOCKLOG] = _("bad blocksize log field");
4af916f8 125 err_message[XR_BAD_VERSION] = _("bad or unsupported version");
507f4e33
NS
126 err_message[XR_BAD_INPROGRESS] =
127 _("filesystem mkfs-in-progress bit set");
128 err_message[XR_BAD_FS_SIZE_DATA] =
129 _("inconsistent filesystem geometry information");
130 err_message[XR_BAD_INO_SIZE_DATA] =
131 _("bad inode size or inconsistent with number of inodes/block"),
132 err_message[XR_BAD_SECT_SIZE_DATA] = _("bad sector size");
133 err_message[XR_AGF_GEO_MISMATCH] =
134 _("AGF geometry info conflicts with filesystem geometry");
135 err_message[XR_AGI_GEO_MISMATCH] =
136 _("AGI geometry info conflicts with filesystem geometry");
137 err_message[XR_SB_GEO_MISMATCH] =
138 _("AG superblock geometry info conflicts with filesystem geometry");
139 err_message[XR_EOF] = _("attempted to perform I/O beyond EOF");
140 err_message[XR_BAD_RT_GEO_DATA] =
141 _("inconsistent filesystem geometry in realtime filesystem component");
142 err_message[XR_BAD_INO_MAX_PCT] =
143 _("maximum indicated percentage of inodes > 100%");
144 err_message[XR_BAD_INO_ALIGN] =
145 _("inconsistent inode alignment value");
146 err_message[XR_INSUFF_SEC_SB] =
147 _("not enough secondary superblocks with matching geometry");
148 err_message[XR_BAD_SB_UNIT] =
149 _("bad stripe unit in superblock");
150 err_message[XR_BAD_SB_WIDTH] =
151 _("bad stripe width in superblock");
152 err_message[XR_BAD_SVN] =
153 _("bad shared version number in superblock");
88f364a9
DC
154 err_message[XR_BAD_CRC] =
155 _("bad CRC in superblock");
02b56f87
DW
156 err_message[XR_BAD_DIR_SIZE_DATA] =
157 _("inconsistent directory geometry information");
507f4e33
NS
158 done = 1;
159 }
160
2bd0ea18 161 if (err_code < XR_OK || err_code >= XR_BAD_ERR_CODE)
507f4e33 162 do_abort(_("bad error code - %d\n"), err_code);
2bd0ea18
NS
163
164 return(err_message[err_code]);
165}
166
167static void
168noval(char opt, char *tbl[], int idx)
169{
507f4e33 170 do_warn(_("-%c %s option cannot have a value\n"), opt, tbl[idx]);
2bd0ea18
NS
171 usage();
172}
173
174static void
175respec(char opt, char *tbl[], int idx)
176{
177 do_warn("-%c ", opt);
178 if (tbl)
179 do_warn("%s ", tbl[idx]);
507f4e33 180 do_warn(_("option respecified\n"));
2bd0ea18
NS
181 usage();
182}
183
184static void
185unknown(char opt, char *s)
186{
507f4e33 187 do_warn(_("unknown option -%c %s\n"), opt, s);
2bd0ea18
NS
188 usage();
189}
190
191/*
192 * sets only the global argument flags and variables
193 */
8b8a6b02 194static void
2bd0ea18
NS
195process_args(int argc, char **argv)
196{
197 char *p;
198 int c;
199
200 log_spec = 0;
201 fs_is_dirty = 0;
202 verbose = 0;
203 no_modify = 0;
c781939c 204 dangerously = 0;
2bd0ea18 205 isa_file = 0;
d321ceac 206 zap_log = 0;
2bd0ea18 207 dumpcore = 0;
0f012a4c 208 full_ino_ex_data = 0;
2bd0ea18
NS
209 force_geo = 0;
210 assume_xfs = 0;
6bf4721d 211 copied_sunit = 0;
2bd0ea18
NS
212 sb_inoalignmt = 0;
213 sb_unit = 0;
214 sb_width = 0;
add3cb90 215 ag_stride = 0;
2556c98b 216 thread_count = 1;
06fbdda9 217 report_interval = PROG_RPT_DEFAULT;
7c3e94a3 218 report_corrected = false;
2bd0ea18
NS
219
220 /*
221 * XXX have to add suboption processing here
222 * attributes, quotas, nlinks, aligned_inos, sb_fbits
223 */
7c3e94a3 224 while ((c = getopt(argc, argv, "c:o:fl:m:r:LnDvVdPet:")) != EOF) {
2bd0ea18
NS
225 switch (c) {
226 case 'D':
227 dumpcore = 1;
228 break;
229 case 'o':
230 p = optarg;
231 while (*p != '\0') {
232 char *val;
233
ab870d0e 234 switch (getsubopt(&p, o_opts, &val)) {
2bd0ea18
NS
235 case ASSUME_XFS:
236 if (val)
237 noval('o', o_opts, ASSUME_XFS);
238 if (assume_xfs)
239 respec('o', o_opts, ASSUME_XFS);
240 assume_xfs = 1;
241 break;
9f38f08d 242 case IHASH_SIZE:
3a19fb7d
CH
243 do_warn(
244 _("-o ihash option has been removed and will be ignored\n"));
9f38f08d
MV
245 break;
246 case BHASH_SIZE:
12be365e
BN
247 if (max_mem_specified)
248 do_abort(
3a19fb7d 249 _("-o bhash option cannot be used with -m option\n"));
5e656dbb 250 libxfs_bhash_size = (int)strtol(val, NULL, 0);
2556c98b 251 bhash_option_used = 1;
cb5b3ef4 252 break;
add3cb90 253 case AG_STRIDE:
5e656dbb 254 ag_stride = (int)strtol(val, NULL, 0);
3b6ac903 255 break;
d4dd6ab5
CH
256 case FORCE_GEO:
257 if (val)
258 noval('o', o_opts, FORCE_GEO);
259 if (force_geo)
260 respec('o', o_opts, FORCE_GEO);
261 force_geo = 1;
262 break;
364a126c
DC
263 case PHASE2_THREADS:
264 phase2_threads = (int)strtol(val, NULL, 0);
265 break;
2bd0ea18
NS
266 default:
267 unknown('o', val);
268 break;
269 }
270 }
271 break;
4af916f8
BN
272 case 'c':
273 p = optarg;
274 while (*p) {
275 char *val;
276
ab870d0e 277 switch (getsubopt(&p, c_opts, &val)) {
4af916f8 278 case CONVERT_LAZY_COUNT:
5e656dbb 279 lazy_count = (int)strtol(val, NULL, 0);
4af916f8
BN
280 convert_lazy_count = 1;
281 break;
282 default:
283 unknown('c', val);
284 break;
285 }
286 }
287 break;
2bd0ea18
NS
288 case 'l':
289 log_name = optarg;
290 log_spec = 1;
291 break;
42a564ab
ES
292 case 'r':
293 rt_name = optarg;
294 rt_spec = 1;
295 break;
2bd0ea18
NS
296 case 'f':
297 isa_file = 1;
298 break;
12be365e
BN
299 case 'm':
300 if (bhash_option_used)
301 do_abort(_("-m option cannot be used with "
302 "-o bhash option\n"));
5e656dbb 303 max_mem_specified = strtol(optarg, NULL, 0);
12be365e 304 break;
d321ceac
NS
305 case 'L':
306 zap_log = 1;
307 break;
2bd0ea18
NS
308 case 'n':
309 no_modify = 1;
310 break;
6089b6f0
NS
311 case 'd':
312 dangerously = 1;
313 break;
2bd0ea18 314 case 'v':
3b6ac903 315 verbose++;
2bd0ea18
NS
316 break;
317 case 'V':
507f4e33 318 printf(_("%s version %s\n"), progname, VERSION);
3d98fe63 319 exit(0);
cb5b3ef4 320 case 'P':
2556c98b 321 do_prefetch = 0;
3b6ac903 322 break;
06fbdda9 323 case 't':
5e656dbb 324 report_interval = (int)strtol(optarg, NULL, 0);
06fbdda9 325 break;
7c3e94a3
JT
326 case 'e':
327 report_corrected = true;
328 break;
2bd0ea18
NS
329 case '?':
330 usage();
331 }
332 }
333
334 if (argc - optind != 1)
335 usage();
336
337 if ((fs_name = argv[optind]) == NULL)
338 usage();
7c3e94a3
JT
339
340 if (report_corrected && no_modify)
341 usage();
2bd0ea18
NS
342}
343
b1559967 344void __attribute__((noreturn))
2bd0ea18
NS
345do_error(char const *msg, ...)
346{
347 va_list args;
348
507f4e33 349 fprintf(stderr, _("\nfatal error -- "));
2bd0ea18
NS
350
351 va_start(args, msg);
079afa09
CH
352 vfprintf(stderr, msg, args);
353 if (dumpcore)
354 abort();
355 exit(1);
2bd0ea18
NS
356}
357
358/*
359 * like do_error, only the error is internal, no system
360 * error so no oserror processing
361 */
b1559967 362void __attribute__((noreturn))
2bd0ea18
NS
363do_abort(char const *msg, ...)
364{
365 va_list args;
366
367 va_start(args, msg);
079afa09
CH
368 vfprintf(stderr, msg, args);
369 if (dumpcore)
370 abort();
371 exit(1);
2bd0ea18
NS
372}
373
374void
375do_warn(char const *msg, ...)
376{
377 va_list args;
378
379 fs_is_dirty = 1;
380
381 va_start(args, msg);
079afa09 382 vfprintf(stderr, msg, args);
2bd0ea18
NS
383 va_end(args);
384}
385
386/* no formatting */
387
388void
389do_log(char const *msg, ...)
390{
391 va_list args;
392
393 va_start(args, msg);
079afa09 394 vfprintf(stderr, msg, args);
2bd0ea18
NS
395 va_end(args);
396}
397
8b8a6b02 398static void
2bd0ea18
NS
399calc_mkfs(xfs_mount_t *mp)
400{
401 xfs_agblock_t fino_bno;
402 int do_inoalign;
403
404 do_inoalign = mp->m_sinoalign;
405
406 /*
7b370905
BF
407 * Pre-calculate the geometry of ag 0. We know what it looks like
408 * because we know what mkfs does: 2 allocation btree roots (by block
409 * and by size), the inode allocation btree root, the free inode
410 * allocation btree root (if enabled) and some number of blocks to
411 * prefill the agfl.
de046644
DC
412 *
413 * Because the current shape of the btrees may differ from the current
414 * shape, we open code the mkfs freelist block count here. mkfs creates
415 * single level trees, so the calculation is pertty straight forward for
7ddb50f8 416 * the trees that use the AGFL.
2bd0ea18
NS
417 */
418 bnobt_root = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
419 bcntbt_root = bnobt_root + 1;
420 inobt_root = bnobt_root + 2;
de046644 421 fino_bno = inobt_root + (2 * min(2, mp->m_ag_maxlevels)) + 1;
7b370905
BF
422 if (xfs_sb_version_hasfinobt(&mp->m_sb))
423 fino_bno++;
7ddb50f8
DW
424 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
425 fino_bno += min(2, mp->m_rmap_maxlevels); /* agfl blocks */
0f94fa4b 426 fino_bno++;
7ddb50f8 427 }
18c44aa9
DW
428 if (xfs_sb_version_hasreflink(&mp->m_sb))
429 fino_bno++;
2bd0ea18 430
d4dd6ab5 431 /*
649bfa9a
CH
432 * If the log is allocated in the first allocation group we need to
433 * add the number of blocks used by the log to the above calculation.
434 *
435 * This can happens with filesystems that only have a single
436 * allocation group, or very odd geometries created by old mkfs
437 * versions on very small filesystems.
d4dd6ab5 438 */
649bfa9a
CH
439 if (mp->m_sb.sb_logstart &&
440 XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == 0) {
441
d4dd6ab5
CH
442 /*
443 * XXX(hch): verify that sb_logstart makes sense?
444 */
445 fino_bno += mp->m_sb.sb_logblocks;
446 }
447
2bd0ea18
NS
448 /*
449 * ditto the location of the first inode chunks in the fs ('/')
450 */
5e656dbb 451 if (xfs_sb_version_hasdalign(&mp->m_sb) && do_inoalign) {
2bd0ea18
NS
452 first_prealloc_ino = XFS_OFFBNO_TO_AGINO(mp, roundup(fino_bno,
453 mp->m_sb.sb_unit), 0);
5e656dbb 454 } else if (xfs_sb_version_hasalign(&mp->m_sb) &&
2bd0ea18
NS
455 mp->m_sb.sb_inoalignmt > 1) {
456 first_prealloc_ino = XFS_OFFBNO_TO_AGINO(mp,
457 roundup(fino_bno,
458 mp->m_sb.sb_inoalignmt),
459 0);
460 } else {
461 first_prealloc_ino = XFS_OFFBNO_TO_AGINO(mp, fino_bno, 0);
462 }
463
ff105f75 464 ASSERT(mp->m_ialloc_blks > 0);
2bd0ea18 465
ff105f75 466 if (mp->m_ialloc_blks > 1)
2bd0ea18
NS
467 last_prealloc_ino = first_prealloc_ino + XFS_INODES_PER_CHUNK;
468 else
469 last_prealloc_ino = XFS_OFFBNO_TO_AGINO(mp, fino_bno + 1, 0);
470
471 /*
472 * now the first 3 inodes in the system
473 */
474 if (mp->m_sb.sb_rootino != first_prealloc_ino) {
475 do_warn(
5d1b7f0f 476_("sb root inode value %" PRIu64 " %sinconsistent with calculated value %u\n"),
507f4e33
NS
477 mp->m_sb.sb_rootino,
478 (mp->m_sb.sb_rootino == NULLFSINO ? "(NULLFSINO) ":""),
479 first_prealloc_ino);
2bd0ea18
NS
480
481 if (!no_modify)
482 do_warn(
5d1b7f0f 483 _("resetting superblock root inode pointer to %u\n"),
2bd0ea18
NS
484 first_prealloc_ino);
485 else
486 do_warn(
5d1b7f0f 487 _("would reset superblock root inode pointer to %u\n"),
2bd0ea18
NS
488 first_prealloc_ino);
489
490 /*
491 * just set the value -- safe since the superblock
492 * doesn't get flushed out if no_modify is set
493 */
494 mp->m_sb.sb_rootino = first_prealloc_ino;
495 }
496
497 if (mp->m_sb.sb_rbmino != first_prealloc_ino + 1) {
498 do_warn(
5d1b7f0f 499_("sb realtime bitmap inode %" PRIu64 " %sinconsistent with calculated value %u\n"),
507f4e33
NS
500 mp->m_sb.sb_rbmino,
501 (mp->m_sb.sb_rbmino == NULLFSINO ? "(NULLFSINO) ":""),
502 first_prealloc_ino + 1);
2bd0ea18
NS
503
504 if (!no_modify)
505 do_warn(
5d1b7f0f 506 _("resetting superblock realtime bitmap ino pointer to %u\n"),
2bd0ea18
NS
507 first_prealloc_ino + 1);
508 else
509 do_warn(
5d1b7f0f 510 _("would reset superblock realtime bitmap ino pointer to %u\n"),
2bd0ea18
NS
511 first_prealloc_ino + 1);
512
513 /*
514 * just set the value -- safe since the superblock
515 * doesn't get flushed out if no_modify is set
516 */
517 mp->m_sb.sb_rbmino = first_prealloc_ino + 1;
518 }
519
520 if (mp->m_sb.sb_rsumino != first_prealloc_ino + 2) {
521 do_warn(
5d1b7f0f
CH
522_("sb realtime summary inode %" PRIu64 " %sinconsistent with calculated value %u\n"),
523 mp->m_sb.sb_rsumino,
524 (mp->m_sb.sb_rsumino == NULLFSINO ? "(NULLFSINO) ":""),
525 first_prealloc_ino + 2);
2bd0ea18
NS
526
527 if (!no_modify)
528 do_warn(
5d1b7f0f 529 _("resetting superblock realtime summary ino pointer to %u\n"),
2bd0ea18
NS
530 first_prealloc_ino + 2);
531 else
532 do_warn(
5d1b7f0f 533 _("would reset superblock realtime summary ino pointer to %u\n"),
2bd0ea18
NS
534 first_prealloc_ino + 2);
535
536 /*
537 * just set the value -- safe since the superblock
538 * doesn't get flushed out if no_modify is set
539 */
540 mp->m_sb.sb_rsumino = first_prealloc_ino + 2;
541 }
542
543}
544
1926558d
BF
545/*
546 * v5 superblock metadata track the LSN of last modification and thus require
547 * that the current LSN is always moving forward. The current LSN is reset if
548 * the log has been cleared, which puts the log behind parts of the filesystem
549 * on-disk and can disrupt log recovery.
550 *
551 * We have tracked the maximum LSN of every piece of metadata that has been read
552 * in via the read verifiers. Compare the max LSN with the log and if the log is
553 * behind, bump the cycle number and reformat the log.
554 */
555static void
556format_log_max_lsn(
557 struct xfs_mount *mp)
558{
559 struct xlog *log = mp->m_log;
560 int max_cycle;
561 int max_block;
562 int new_cycle;
563 xfs_daddr_t logstart;
564 xfs_daddr_t logblocks;
565 int logversion;
566
567 if (!xfs_sb_version_hascrc(&mp->m_sb))
568 return;
569
570 /*
571 * If the log is ahead of the highest metadata LSN we've seen, we're
572 * safe and there's nothing to do.
573 */
574 max_cycle = CYCLE_LSN(libxfs_max_lsn);
575 max_block = BLOCK_LSN(libxfs_max_lsn);
576 if (max_cycle < log->l_curr_cycle ||
577 (max_cycle == log->l_curr_cycle && max_block < log->l_curr_block))
578 return;
579
580 /*
581 * Going to the next cycle should be sufficient but we bump by a few
582 * counts to help cover any metadata LSNs we could have missed.
583 */
584 new_cycle = max_cycle + 3;
585 logstart = XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart);
586 logblocks = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
587 logversion = xfs_sb_version_haslogv2(&mp->m_sb) ? 2 : 1;
588
589 do_warn(_("Maximum metadata LSN (%d:%d) is ahead of log (%d:%d).\n"),
590 max_cycle, max_block, log->l_curr_cycle, log->l_curr_block);
591
592 if (no_modify) {
593 do_warn(_("Would format log to cycle %d.\n"), new_cycle);
594 return;
595 }
596
597 do_warn(_("Format log to cycle %d.\n"), new_cycle);
1c12a814
BF
598 libxfs_log_clear(log->l_dev, NULL, logstart, logblocks,
599 &mp->m_sb.sb_uuid, logversion, mp->m_sb.sb_logsunit,
571a78a7 600 XLOG_FMT, new_cycle, true);
1926558d
BF
601}
602
4a32b9e9
DC
603/*
604 * mkfs increases the AG count for "multidisk" configurations, we want
605 * to target these for an increase in thread count. Hence check the superlock
606 * geometry information to determine if mkfs considered this a multidisk
607 * configuration.
608 */
609static bool
610is_multidisk_filesystem(
611 struct xfs_mount *mp)
612{
613 struct xfs_sb *sbp = &mp->m_sb;
614
615 /* High agcount filesystems are always considered "multidisk" */
616 if (sbp->sb_agcount >= XFS_MULTIDISK_AGCOUNT)
617 return true;
618
619 /*
620 * If it doesn't have a sunit/swidth, mkfs didn't consider it a
621 * multi-disk array, so we don't either.
622 */
623 if (!sbp->sb_unit)
624 return false;
625
626 ASSERT(sbp->sb_width);
627 return true;
628}
629
28a0a30f
ZL
630/*
631 * if the sector size of the filesystem we are trying to repair is
632 * smaller than that of the underlying filesystem (i.e. we are repairing
633 * an image), the we have to turn off direct IO because we cannot do IO
634 * smaller than the host filesystem's sector size.
635 */
636static void
637check_fs_vs_host_sectsize(
638 struct xfs_sb *sb)
639{
640 int fd;
641 long old_flags;
642 struct xfs_fsop_geom_v1 geom = { 0 };
643
644 fd = libxfs_device_to_fd(x.ddev);
645
646 if (ioctl(fd, XFS_IOC_FSGEOMETRY_V1, &geom) < 0) {
647 do_log(_("Cannot get host filesystem geometry.\n"
648 "Repair may fail if there is a sector size mismatch between\n"
649 "the image and the host filesystem.\n"));
650 geom.sectsize = BBSIZE;
651 }
652
653 if (sb->sb_sectsize < geom.sectsize) {
654 old_flags = fcntl(fd, F_GETFL, 0);
655 if (fcntl(fd, F_SETFL, old_flags & ~O_DIRECT) < 0) {
656 do_warn(_(
657 "Sector size on host filesystem larger than image sector size.\n"
658 "Cannot turn off direct IO, so exiting.\n"));
659 exit(1);
660 }
661 }
662}
663
2bd0ea18
NS
664int
665main(int argc, char **argv)
666{
2bd0ea18
NS
667 xfs_mount_t *temp_mp;
668 xfs_mount_t *mp;
5e656dbb 669 xfs_dsb_t *dsb;
2bd0ea18
NS
670 xfs_buf_t *sbp;
671 xfs_mount_t xfs_m;
1d6cb115 672 struct xlog log = {0};
06fbdda9 673 char *msgbuf;
88f364a9
DC
674 struct xfs_sb psb;
675 int rval;
2bd0ea18
NS
676
677 progname = basename(argv[0]);
507f4e33
NS
678 setlocale(LC_ALL, "");
679 bindtextdomain(PACKAGE, LOCALEDIR);
680 textdomain(PACKAGE);
beed0dc8 681 dinode_bmbt_translation_init();
2bd0ea18
NS
682
683 temp_mp = &xfs_m;
684 setbuf(stdout, NULL);
685
686 process_args(argc, argv);
d321ceac 687 xfs_init(&x);
2bd0ea18 688
2556c98b
BN
689 msgbuf = malloc(DURATION_BUF_SIZE);
690
06fbdda9
MV
691 timestamp(PHASE_START, 0, NULL);
692 timestamp(PHASE_END, 0, NULL);
693
28a0a30f
ZL
694 /* -f forces this, but let's be nice and autodetect it, as well. */
695 if (!isa_file) {
696 int fd = libxfs_device_to_fd(x.ddev);
697 struct stat statbuf;
698
699 if (fstat(fd, &statbuf) < 0)
700 do_warn(_("%s: couldn't stat \"%s\"\n"),
701 progname, fs_name);
702 else if (S_ISREG(statbuf.st_mode))
703 isa_file = 1;
704 }
705
706 if (isa_file) {
707 /* Best effort attempt to validate fs vs host sector size */
708 rval = get_sb(&psb, 0, XFS_MAX_SECTORSIZE, 0);
709 if (rval == XR_OK)
710 check_fs_vs_host_sectsize(&psb);
711 }
712
2bd0ea18
NS
713 /* do phase1 to make sure we have a superblock */
714 phase1(temp_mp);
06fbdda9 715 timestamp(PHASE_END, 1, NULL);
2bd0ea18
NS
716
717 if (no_modify && primary_sb_modified) {
507f4e33
NS
718 do_warn(_("Primary superblock would have been modified.\n"
719 "Cannot proceed further in no_modify mode.\n"
720 "Exiting now.\n"));
2bd0ea18
NS
721 exit(1);
722 }
723
88f364a9
DC
724 rval = get_sb(&psb, 0, XFS_MAX_SECTORSIZE, 0);
725 if (rval != XR_OK) {
726 do_warn(_("Primary superblock bad after phase 1!\n"
727 "Exiting now.\n"));
728 exit(1);
729 }
2bd0ea18 730
f63fd268 731 /*
28a0a30f
ZL
732 * Now that we have completely validated the superblock, geometry may
733 * have changed; re-check geometry vs the host filesystem geometry
f63fd268 734 */
28a0a30f
ZL
735 if (isa_file)
736 check_fs_vs_host_sectsize(&psb);
88f364a9 737
1d6cb115
BF
738 /*
739 * Prepare the mount structure. Point the log reference to our local
740 * copy so it's available to the various phases. The log bits are
741 * initialized in phase 2.
742 */
88f364a9
DC
743 memset(&xfs_m, 0, sizeof(xfs_mount_t));
744 mp = libxfs_mount(&xfs_m, &psb, x.ddev, x.logdev, x.rtdev, 0);
2bd0ea18
NS
745
746 if (!mp) {
507f4e33
NS
747 fprintf(stderr,
748 _("%s: cannot repair this filesystem. Sorry.\n"),
2bd0ea18
NS
749 progname);
750 exit(1);
751 }
1d6cb115 752 mp->m_log = &log;
2bd0ea18 753
23639f77
ES
754 /* Spit out function & line on these corruption macros */
755 if (verbose > 2)
756 mp->m_flags |= LIBXFS_MOUNT_WANT_CORRUPTED;
757
2bd0ea18
NS
758 /*
759 * set XFS-independent status vars from the mount/sb structure
760 */
761 glob_agcount = mp->m_sb.sb_agcount;
762
763 chunks_pblock = mp->m_sb.sb_inopblock / XFS_INODES_PER_CHUNK;
5a707ca1 764 max_symlink_blocks = libxfs_symlink_blocks(mp, XFS_SYMLINK_MAXLEN);
edf3f9d0 765 inodes_per_cluster = MAX(mp->m_sb.sb_inopblock,
ff105f75 766 mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog);
2bd0ea18 767
0cce4aa1
DC
768 /*
769 * Automatic striding for high agcount filesystems.
770 *
771 * More AGs indicates that the filesystem is either large or can handle
772 * more IO parallelism. Either way, we should try to process multiple
773 * AGs at a time in such a configuration to try to saturate the
774 * underlying storage and speed the repair process. Only do this if
775 * prefetching is enabled.
776 *
777 * Given mkfs defaults for 16AGs for "multidisk" configurations, we want
778 * to target these for an increase in thread count. Hence a stride value
779 * of 15 is chosen to ensure we get at least 2 AGs being scanned at once
780 * on such filesystems.
12b55baf
DC
781 *
782 * Limit the maximum thread count based on the available CPU power that
783 * is available. If we use too many threads, we might run out of memory
784 * and CPU power before we run out of IO concurrency. We limit to 8
785 * threads/CPU as this is enough threads to saturate a CPU on fast
786 * devices, yet few enough that it will saturate but won't overload slow
787 * devices.
4a32b9e9
DC
788 *
789 * Multidisk filesystems can handle more IO parallelism so we should try
790 * to process multiple AGs at a time in such a configuration to try to
791 * saturate the underlying storage and speed the repair process. Only do
792 * this if prefetching is enabled.
0cce4aa1 793 */
4a32b9e9
DC
794 if (!ag_stride && do_prefetch && is_multidisk_filesystem(mp)) {
795 /*
796 * For small agcount multidisk systems, just double the
797 * parallelism. For larger AG count filesystems (32 and above)
798 * use more parallelism, and linearly increase the parallelism
799 * with the number of AGs.
800 */
801 ag_stride = min(glob_agcount, XFS_MULTIDISK_AGCOUNT / 2) - 1;
802 }
0cce4aa1 803
add3cb90 804 if (ag_stride) {
12b55baf
DC
805 int max_threads = platform_nproc() * 8;
806
2556c98b 807 thread_count = (glob_agcount + ag_stride - 1) / ag_stride;
12b55baf
DC
808 while (thread_count > max_threads) {
809 ag_stride *= 2;
810 thread_count = (glob_agcount + ag_stride - 1) /
811 ag_stride;
812 }
813 if (thread_count > 0)
814 thread_init();
815 else {
816 thread_count = 1;
817 ag_stride = 0;
818 }
add3cb90
BN
819 }
820
2556c98b 821 if (ag_stride && report_interval) {
06fbdda9 822 init_progress_rpt();
06fbdda9
MV
823 if (msgbuf) {
824 do_log(_(" - reporting progress in intervals of %s\n"),
825 duration(report_interval, msgbuf));
06fbdda9
MV
826 }
827 }
828
2556c98b
BN
829 /*
830 * Adjust libxfs cache sizes based on system memory,
831 * filesystem size and inode count.
832 *
833 * We'll set the cache size based on 3/4s the memory minus
834 * space used by the inode AVL tree and block usage map.
835 *
836 * Inode AVL tree space is approximately 4 bytes per inode,
837 * block usage map is currently 1 byte for 2 blocks.
838 *
839 * We assume most blocks will be inode clusters.
840 *
841 * Calculations are done in kilobyte units.
842 */
843
12be365e 844 if (!bhash_option_used || max_mem_specified) {
2556c98b 845 unsigned long mem_used;
12be365e
BN
846 unsigned long max_mem;
847 struct rlimit rlim;
2556c98b 848
2556c98b 849 libxfs_bcache_purge();
2556c98b
BN
850 cache_destroy(libxfs_bcache);
851
852 mem_used = (mp->m_sb.sb_icount >> (10 - 2)) +
12be365e
BN
853 (mp->m_sb.sb_dblocks >> (10 + 1)) +
854 50000; /* rough estimate of 50MB overhead */
855 max_mem = max_mem_specified ? max_mem_specified * 1024 :
856 libxfs_physmem() * 3 / 4;
857
858 if (getrlimit(RLIMIT_AS, &rlim) != -1 &&
859 rlim.rlim_cur != RLIM_INFINITY) {
860 rlim.rlim_cur = rlim.rlim_max;
861 setrlimit(RLIMIT_AS, &rlim);
862 /* use approximately 80% of rlimit to avoid overrun */
863 max_mem = MIN(max_mem, rlim.rlim_cur / 1280);
864 } else
865 max_mem = MIN(max_mem, (LONG_MAX >> 10) + 1);
2556c98b
BN
866
867 if (verbose > 1)
5d1b7f0f
CH
868 do_log(
869 _(" - max_mem = %lu, icount = %" PRIu64 ", imem = %" PRIu64 ", dblock = %" PRIu64 ", dmem = %" PRIu64 "\n"),
12be365e
BN
870 max_mem, mp->m_sb.sb_icount,
871 mp->m_sb.sb_icount >> (10 - 2),
872 mp->m_sb.sb_dblocks,
873 mp->m_sb.sb_dblocks >> (10 + 1));
874
875 if (max_mem <= mem_used) {
0335a835
DC
876 if (max_mem_specified) {
877 do_abort(
878 _("Required memory for repair is greater that the maximum specified\n"
879 "with the -m option. Please increase it to at least %lu.\n"),
12be365e 880 mem_used / 1024);
0335a835 881 }
70a4820f 882 do_log(
61510437
DC
883 _("Memory available for repair (%luMB) may not be sufficient.\n"
884 "At least %luMB is needed to repair this filesystem efficiently\n"
885 "If repair fails due to lack of memory, please\n"),
886 max_mem / 1024, mem_used / 1024);
887 if (do_prefetch)
70a4820f 888 do_log(
61510437
DC
889 _("turn prefetching off (-P) to reduce the memory footprint.\n"));
890 else
70a4820f 891 do_log(
61510437
DC
892 _("increase system RAM and/or swap space to at least %luMB.\n"),
893 mem_used * 2 / 1024);
894
895 max_mem = mem_used;
2556c98b
BN
896 }
897
61510437
DC
898 max_mem -= mem_used;
899 if (max_mem >= (1 << 30))
900 max_mem = 1 << 30;
901 libxfs_bhash_size = max_mem / (HASH_CACHE_RATIO *
902 (mp->m_inode_cluster_size >> 10));
903 if (libxfs_bhash_size < 512)
904 libxfs_bhash_size = 512;
905
2556c98b
BN
906 if (verbose)
907 do_log(_(" - block cache size set to %d entries\n"),
908 libxfs_bhash_size * HASH_CACHE_RATIO);
909
ba9ecd40 910 libxfs_bcache = cache_init(0, libxfs_bhash_size,
2556c98b
BN
911 &libxfs_bcache_operations);
912 }
913
2bd0ea18
NS
914 /*
915 * calculate what mkfs would do to this filesystem
916 */
917 calc_mkfs(mp);
918
919 /*
c1f7a46c 920 * initialize block alloc map
2bd0ea18 921 */
c1f7a46c
BN
922 init_bmaps(mp);
923 incore_ino_init(mp);
924 incore_ext_init(mp);
2d273771 925 rmaps_init(mp);
c1f7a46c
BN
926
927 /* initialize random globals now that we know the fs geometry */
928 inodes_per_block = mp->m_sb.sb_inopblock;
2bd0ea18
NS
929
930 if (parse_sb_version(&mp->m_sb)) {
931 do_warn(
507f4e33 932 _("Found unsupported filesystem features. Exiting now.\n"));
2bd0ea18
NS
933 return(1);
934 }
935
936 /* make sure the per-ag freespace maps are ok so we can mount the fs */
364a126c 937 phase2(mp, phase2_threads);
06fbdda9 938 timestamp(PHASE_END, 2, NULL);
2bd0ea18 939
2556c98b
BN
940 if (do_prefetch)
941 init_prefetch(mp);
942
8100dd79 943 phase3(mp, phase2_threads);
06fbdda9 944 timestamp(PHASE_END, 3, NULL);
2bd0ea18
NS
945
946 phase4(mp);
06fbdda9 947 timestamp(PHASE_END, 4, NULL);
2bd0ea18
NS
948
949 if (no_modify)
507f4e33 950 printf(_("No modify flag set, skipping phase 5\n"));
3b6ac903 951 else {
2bd0ea18 952 phase5(mp);
3b6ac903 953 }
06fbdda9 954 timestamp(PHASE_END, 5, NULL);
2bd0ea18 955
c1f7a46c
BN
956 /*
957 * Done with the block usage maps, toss them...
958 */
2d273771 959 rmaps_free(mp);
c1f7a46c
BN
960 free_bmaps(mp);
961
2bd0ea18
NS
962 if (!bad_ino_btree) {
963 phase6(mp);
06fbdda9 964 timestamp(PHASE_END, 6, NULL);
2bd0ea18 965
e161d4a8 966 phase7(mp, phase2_threads);
06fbdda9 967 timestamp(PHASE_END, 7, NULL);
2bd0ea18
NS
968 } else {
969 do_warn(
507f4e33 970_("Inode allocation btrees are too corrupted, skipping phases 6 and 7\n"));
2bd0ea18
NS
971 }
972
0340d706 973 if (lost_quotas && !have_uquotino && !have_gquotino && !have_pquotino) {
2bd0ea18
NS
974 if (!no_modify) {
975 do_warn(
507f4e33 976_("Warning: no quota inodes were found. Quotas disabled.\n"));
2bd0ea18
NS
977 } else {
978 do_warn(
507f4e33 979_("Warning: no quota inodes were found. Quotas would be disabled.\n"));
2bd0ea18
NS
980 }
981 } else if (lost_quotas) {
982 if (!no_modify) {
983 do_warn(
507f4e33 984_("Warning: quota inodes were cleared. Quotas disabled.\n"));
2bd0ea18
NS
985 } else {
986 do_warn(
507f4e33 987_("Warning: quota inodes would be cleared. Quotas would be disabled.\n"));
2bd0ea18
NS
988 }
989 } else {
990 if (lost_uquotino) {
991 if (!no_modify) {
992 do_warn(
507f4e33
NS
993_("Warning: user quota information was cleared.\n"
994 "User quotas can not be enforced until limit information is recreated.\n"));
2bd0ea18
NS
995 } else {
996 do_warn(
507f4e33
NS
997_("Warning: user quota information would be cleared.\n"
998 "User quotas could not be enforced until limit information was recreated.\n"));
2bd0ea18
NS
999 }
1000 }
1001
b36eef04 1002 if (lost_gquotino) {
2bd0ea18
NS
1003 if (!no_modify) {
1004 do_warn(
507f4e33
NS
1005_("Warning: group quota information was cleared.\n"
1006 "Group quotas can not be enforced until limit information is recreated.\n"));
2bd0ea18
NS
1007 } else {
1008 do_warn(
507f4e33
NS
1009_("Warning: group quota information would be cleared.\n"
1010 "Group quotas could not be enforced until limit information was recreated.\n"));
9b27bdbb
NS
1011 }
1012 }
1013
1014 if (lost_pquotino) {
1015 if (!no_modify) {
1016 do_warn(
1017_("Warning: project quota information was cleared.\n"
1018 "Project quotas can not be enforced until limit information is recreated.\n"));
1019 } else {
1020 do_warn(
1021_("Warning: project quota information would be cleared.\n"
1022 "Project quotas could not be enforced until limit information was recreated.\n"));
2bd0ea18
NS
1023 }
1024 }
1025 }
1026
2556c98b 1027 if (ag_stride && report_interval)
06fbdda9 1028 stop_progress_rpt();
9f38f08d 1029
2bd0ea18 1030 if (no_modify) {
1926558d
BF
1031 /*
1032 * Warn if the current LSN is problematic and the log requires a
1033 * reformat.
1034 */
1035 format_log_max_lsn(mp);
1036
2bd0ea18 1037 do_log(
507f4e33 1038 _("No modify flag set, skipping filesystem flush and exiting.\n"));
3b6ac903 1039 if (verbose)
06fbdda9 1040 summary_report();
2bd0ea18
NS
1041 if (fs_is_dirty)
1042 return(1);
1043
1044 return(0);
1045 }
1046
1047 /*
1048 * Clear the quota flags if they're on.
1049 */
1050 sbp = libxfs_getsb(mp, 0);
1051 if (!sbp)
507f4e33 1052 do_error(_("couldn't get superblock\n"));
2bd0ea18 1053
5e656dbb 1054 dsb = XFS_BUF_TO_SBP(sbp);
2bd0ea18 1055
342aef1e 1056 if (be16_to_cpu(dsb->sb_qflags) & XFS_ALL_QUOTA_CHKD) {
5e656dbb
BN
1057 do_warn(_("Note - quota info will be regenerated on next "
1058 "quota mount.\n"));
342aef1e 1059 dsb->sb_qflags &= cpu_to_be16(~XFS_ALL_QUOTA_CHKD);
2bd0ea18
NS
1060 }
1061
6bf4721d 1062 if (copied_sunit) {
2bd0ea18 1063 do_warn(
6bf4721d
ES
1064_("Note - stripe unit (%d) and width (%d) were copied from a backup superblock.\n"
1065 "Please reset with mount -o sunit=<value>,swidth=<value> if necessary\n"),
5e656dbb 1066 be32_to_cpu(dsb->sb_unit), be32_to_cpu(dsb->sb_width));
dfc130f3 1067 }
2bd0ea18
NS
1068
1069 libxfs_writebuf(sbp, 0);
1070
2556c98b 1071 /*
1926558d
BF
1072 * Done. Flush all cached buffers and inodes first to ensure all
1073 * verifiers are run (where we discover the max metadata LSN), reformat
1074 * the log if necessary and unmount.
2556c98b
BN
1075 */
1076 libxfs_bcache_flush();
1926558d 1077 format_log_max_lsn(mp);
2bd0ea18 1078 libxfs_umount(mp);
1926558d 1079
d321ceac
NS
1080 if (x.rtdev)
1081 libxfs_device_close(x.rtdev);
1082 if (x.logdev && x.logdev != x.ddev)
1083 libxfs_device_close(x.logdev);
1084 libxfs_device_close(x.ddev);
2ce8bff5 1085 libxfs_destroy();
2bd0ea18 1086
06fbdda9
MV
1087 if (verbose)
1088 summary_report();
507f4e33 1089 do_log(_("done\n"));
3ae81520
ES
1090
1091 if (dangerously && !no_modify)
1092 do_warn(
1093_("Repair of readonly mount complete. Immediate reboot encouraged.\n"));
1094
4c0a98ae
BN
1095 pftrace_done();
1096
0a223eb8
ES
1097 free(msgbuf);
1098
7c3e94a3
JT
1099 if (fs_is_dirty && report_corrected)
1100 return (4);
3b6ac903
MV
1101 return (0);
1102}