LCOV - code coverage report
Current view: top level - src/bin/pg_checksums - pg_checksums.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 197 253 77.9 %
Date: 2023-06-01 12:12:28 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_checksums.c
       4             :  *    Checks, enables or disables page level checksums for an offline
       5             :  *    cluster
       6             :  *
       7             :  * Copyright (c) 2010-2023, PostgreSQL Global Development Group
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/bin/pg_checksums/pg_checksums.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres_fe.h"
      16             : 
      17             : #include <dirent.h>
      18             : #include <limits.h>
      19             : #include <time.h>
      20             : #include <sys/stat.h>
      21             : #include <unistd.h>
      22             : 
      23             : #include "access/xlog_internal.h"
      24             : #include "common/controldata_utils.h"
      25             : #include "common/file_perm.h"
      26             : #include "common/file_utils.h"
      27             : #include "common/logging.h"
      28             : #include "fe_utils/option_utils.h"
      29             : #include "getopt_long.h"
      30             : #include "pg_getopt.h"
      31             : #include "storage/bufpage.h"
      32             : #include "storage/checksum.h"
      33             : #include "storage/checksum_impl.h"
      34             : 
      35             : 
      36             : static int64 files_scanned = 0;
      37             : static int64 files_written = 0;
      38             : static int64 blocks_scanned = 0;
      39             : static int64 blocks_written = 0;
      40             : static int64 badblocks = 0;
      41             : static ControlFileData *ControlFile;
      42             : 
      43             : static char *only_filenode = NULL;
      44             : static bool do_sync = true;
      45             : static bool verbose = false;
      46             : static bool showprogress = false;
      47             : 
      48             : typedef enum
      49             : {
      50             :     PG_MODE_CHECK,
      51             :     PG_MODE_DISABLE,
      52             :     PG_MODE_ENABLE
      53             : } PgChecksumMode;
      54             : 
      55             : /*
      56             :  * Filename components.
      57             :  *
      58             :  * XXX: fd.h is not declared here as frontend side code is not able to
      59             :  * interact with the backend-side definitions for the various fsync
      60             :  * wrappers.
      61             :  */
      62             : #define PG_TEMP_FILES_DIR "pgsql_tmp"
      63             : #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
      64             : 
      65             : static PgChecksumMode mode = PG_MODE_CHECK;
      66             : 
      67             : static const char *progname;
      68             : 
      69             : /*
      70             :  * Progress status information.
      71             :  */
      72             : int64       total_size = 0;
      73             : int64       current_size = 0;
      74             : static pg_time_t last_progress_report = 0;
      75             : 
      76             : static void
      77           2 : usage(void)
      78             : {
      79           2 :     printf(_("%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"), progname);
      80           2 :     printf(_("Usage:\n"));
      81           2 :     printf(_("  %s [OPTION]... [DATADIR]\n"), progname);
      82           2 :     printf(_("\nOptions:\n"));
      83           2 :     printf(_(" [-D, --pgdata=]DATADIR    data directory\n"));
      84           2 :     printf(_("  -c, --check              check data checksums (default)\n"));
      85           2 :     printf(_("  -d, --disable            disable data checksums\n"));
      86           2 :     printf(_("  -e, --enable             enable data checksums\n"));
      87           2 :     printf(_("  -f, --filenode=FILENODE  check only relation with specified filenode\n"));
      88           2 :     printf(_("  -N, --no-sync            do not wait for changes to be written safely to disk\n"));
      89           2 :     printf(_("  -P, --progress           show progress information\n"));
      90           2 :     printf(_("  -v, --verbose            output verbose messages\n"));
      91           2 :     printf(_("  -V, --version            output version information, then exit\n"));
      92           2 :     printf(_("  -?, --help               show this help, then exit\n"));
      93           2 :     printf(_("\nIf no data directory (DATADIR) is specified, "
      94             :              "the environment variable PGDATA\nis used.\n\n"));
      95           2 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
      96           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
      97           2 : }
      98             : 
      99             : /*
     100             :  * Definition of one element part of an exclusion list, used for files
     101             :  * to exclude from checksum validation.  "name" is the name of the file
     102             :  * or path to check for exclusion.  If "match_prefix" is true, any items
     103             :  * matching the name as prefix are excluded.
     104             :  */
     105             : struct exclude_list_item
     106             : {
     107             :     const char *name;
     108             :     bool        match_prefix;
     109             : };
     110             : 
     111             : /*
     112             :  * List of files excluded from checksum validation.
     113             :  *
     114             :  * Note: this list should be kept in sync with what basebackup.c includes.
     115             :  */
     116             : static const struct exclude_list_item skip[] = {
     117             :     {"pg_control", false},
     118             :     {"pg_filenode.map", false},
     119             :     {"pg_internal.init", true},
     120             :     {"PG_VERSION", false},
     121             : #ifdef EXEC_BACKEND
     122             :     {"config_exec_params", true},
     123             : #endif
     124             :     {NULL, false}
     125             : };
     126             : 
     127             : /*
     128             :  * Report current progress status.  Parts borrowed from
     129             :  * src/bin/pg_basebackup/pg_basebackup.c.
     130             :  */
     131             : static void
     132           0 : progress_report(bool finished)
     133             : {
     134             :     int         percent;
     135             :     pg_time_t   now;
     136             : 
     137             :     Assert(showprogress);
     138             : 
     139           0 :     now = time(NULL);
     140           0 :     if (now == last_progress_report && !finished)
     141           0 :         return;                 /* Max once per second */
     142             : 
     143             :     /* Save current time */
     144           0 :     last_progress_report = now;
     145             : 
     146             :     /* Adjust total size if current_size is larger */
     147           0 :     if (current_size > total_size)
     148           0 :         total_size = current_size;
     149             : 
     150             :     /* Calculate current percentage of size done */
     151           0 :     percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
     152             : 
     153           0 :     fprintf(stderr, _("%lld/%lld MB (%d%%) computed"),
     154           0 :             (long long) (current_size / (1024 * 1024)),
     155           0 :             (long long) (total_size / (1024 * 1024)),
     156             :             percent);
     157             : 
     158             :     /*
     159             :      * Stay on the same line if reporting to a terminal and we're not done
     160             :      * yet.
     161             :      */
     162           0 :     fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
     163             : }
     164             : 
     165             : static bool
     166       25670 : skipfile(const char *fn)
     167             : {
     168             :     int         excludeIdx;
     169             : 
     170      127672 :     for (excludeIdx = 0; skip[excludeIdx].name != NULL; excludeIdx++)
     171             :     {
     172      102294 :         int         cmplen = strlen(skip[excludeIdx].name);
     173             : 
     174      102294 :         if (!skip[excludeIdx].match_prefix)
     175       76764 :             cmplen++;
     176      102294 :         if (strncmp(skip[excludeIdx].name, fn, cmplen) == 0)
     177         292 :             return true;
     178             :     }
     179             : 
     180       25378 :     return false;
     181             : }
     182             : 
     183             : static void
     184       17730 : scan_file(const char *fn, int segmentno)
     185             : {
     186             :     PGIOAlignedBlock buf;
     187       17730 :     PageHeader  header = (PageHeader) buf.data;
     188             :     int         f;
     189             :     BlockNumber blockno;
     190             :     int         flags;
     191       17730 :     int64       blocks_written_in_file = 0;
     192             : 
     193             :     Assert(mode == PG_MODE_ENABLE ||
     194             :            mode == PG_MODE_CHECK);
     195             : 
     196       17730 :     flags = (mode == PG_MODE_ENABLE) ? O_RDWR : O_RDONLY;
     197       17730 :     f = open(fn, PG_BINARY | flags, 0);
     198             : 
     199       17730 :     if (f < 0)
     200           0 :         pg_fatal("could not open file \"%s\": %m", fn);
     201             : 
     202       17730 :     files_scanned++;
     203             : 
     204       17730 :     for (blockno = 0;; blockno++)
     205       51712 :     {
     206             :         uint16      csum;
     207       69442 :         int         r = read(f, buf.data, BLCKSZ);
     208             : 
     209       69442 :         if (r == 0)
     210       17714 :             break;
     211       51728 :         if (r != BLCKSZ)
     212             :         {
     213          16 :             if (r < 0)
     214           0 :                 pg_fatal("could not read block %u in file \"%s\": %m",
     215             :                          blockno, fn);
     216             :             else
     217          16 :                 pg_fatal("could not read block %u in file \"%s\": read %d of %d",
     218             :                          blockno, fn, r, BLCKSZ);
     219             :         }
     220       51712 :         blocks_scanned++;
     221             : 
     222             :         /*
     223             :          * Since the file size is counted as total_size for progress status
     224             :          * information, the sizes of all pages including new ones in the file
     225             :          * should be counted as current_size. Otherwise the progress reporting
     226             :          * calculated using those counters may not reach 100%.
     227             :          */
     228       51712 :         current_size += r;
     229             : 
     230             :         /* New pages have no checksum yet */
     231       51712 :         if (PageIsNew(buf.data))
     232           0 :             continue;
     233             : 
     234       51712 :         csum = pg_checksum_page(buf.data, blockno + segmentno * RELSEG_SIZE);
     235       51712 :         if (mode == PG_MODE_CHECK)
     236             :         {
     237       40444 :             if (csum != header->pd_checksum)
     238             :             {
     239           8 :                 if (ControlFile->data_checksum_version == PG_DATA_CHECKSUM_VERSION)
     240           8 :                     pg_log_error("checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
     241             :                                  fn, blockno, csum, header->pd_checksum);
     242           8 :                 badblocks++;
     243             :             }
     244             :         }
     245       11268 :         else if (mode == PG_MODE_ENABLE)
     246             :         {
     247             :             int         w;
     248             : 
     249             :             /*
     250             :              * Do not rewrite if the checksum is already set to the expected
     251             :              * value.
     252             :              */
     253       11268 :             if (header->pd_checksum == csum)
     254        5634 :                 continue;
     255             : 
     256        5634 :             blocks_written_in_file++;
     257             : 
     258             :             /* Set checksum in page header */
     259        5634 :             header->pd_checksum = csum;
     260             : 
     261             :             /* Seek back to beginning of block */
     262        5634 :             if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
     263           0 :                 pg_fatal("seek failed for block %u in file \"%s\": %m", blockno, fn);
     264             : 
     265             :             /* Write block with checksum */
     266        5634 :             w = write(f, buf.data, BLCKSZ);
     267        5634 :             if (w != BLCKSZ)
     268             :             {
     269           0 :                 if (w < 0)
     270           0 :                     pg_fatal("could not write block %u in file \"%s\": %m",
     271             :                              blockno, fn);
     272             :                 else
     273           0 :                     pg_fatal("could not write block %u in file \"%s\": wrote %d of %d",
     274             :                              blockno, fn, w, BLCKSZ);
     275             :             }
     276             :         }
     277             : 
     278       46078 :         if (showprogress)
     279           0 :             progress_report(false);
     280             :     }
     281             : 
     282       17714 :     if (verbose)
     283             :     {
     284           0 :         if (mode == PG_MODE_CHECK)
     285           0 :             pg_log_info("checksums verified in file \"%s\"", fn);
     286           0 :         if (mode == PG_MODE_ENABLE)
     287           0 :             pg_log_info("checksums enabled in file \"%s\"", fn);
     288             :     }
     289             : 
     290             :     /* Update write counters if any write activity has happened */
     291       17714 :     if (blocks_written_in_file > 0)
     292             :     {
     293        1560 :         files_written++;
     294        1560 :         blocks_written += blocks_written_in_file;
     295             :     }
     296             : 
     297       17714 :     close(f);
     298       17714 : }
     299             : 
     300             : /*
     301             :  * Scan the given directory for items which can be checksummed and
     302             :  * operate on each one of them.  If "sizeonly" is true, the size of
     303             :  * all the items which have checksums is computed and returned back
     304             :  * to the caller without operating on the files.  This is used to compile
     305             :  * the total size of the data directory for progress reports.
     306             :  */
     307             : static int64
     308         192 : scan_directory(const char *basedir, const char *subdir, bool sizeonly)
     309             : {
     310         192 :     int64       dirsize = 0;
     311             :     char        path[MAXPGPATH];
     312             :     DIR        *dir;
     313             :     struct dirent *de;
     314             : 
     315         192 :     snprintf(path, sizeof(path), "%s/%s", basedir, subdir);
     316         192 :     dir = opendir(path);
     317         192 :     if (!dir)
     318           0 :         pg_fatal("could not open directory \"%s\": %m", path);
     319       26370 :     while ((de = readdir(dir)) != NULL)
     320             :     {
     321             :         char        fn[MAXPGPATH];
     322             :         struct stat st;
     323             : 
     324       26194 :         if (strcmp(de->d_name, ".") == 0 ||
     325       26012 :             strcmp(de->d_name, "..") == 0)
     326        8366 :             continue;
     327             : 
     328             :         /* Skip temporary files */
     329       25830 :         if (strncmp(de->d_name,
     330             :                     PG_TEMP_FILE_PREFIX,
     331             :                     strlen(PG_TEMP_FILE_PREFIX)) == 0)
     332          62 :             continue;
     333             : 
     334             :         /* Skip temporary folders */
     335       25768 :         if (strncmp(de->d_name,
     336             :                     PG_TEMP_FILES_DIR,
     337             :                     strlen(PG_TEMP_FILES_DIR)) == 0)
     338           0 :             continue;
     339             : 
     340       25768 :         snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name);
     341       25768 :         if (lstat(fn, &st) < 0)
     342           0 :             pg_fatal("could not stat file \"%s\": %m", fn);
     343       25768 :         if (S_ISREG(st.st_mode))
     344             :         {
     345             :             char        fnonly[MAXPGPATH];
     346             :             char       *forkpath,
     347             :                        *segmentpath;
     348       25670 :             int         segmentno = 0;
     349             : 
     350       25670 :             if (skipfile(de->d_name))
     351        7940 :                 continue;
     352             : 
     353             :             /*
     354             :              * Cut off at the segment boundary (".") to get the segment number
     355             :              * in order to mix it into the checksum. Then also cut off at the
     356             :              * fork boundary, to get the filenode the file belongs to for
     357             :              * filtering.
     358             :              */
     359       25378 :             strlcpy(fnonly, de->d_name, sizeof(fnonly));
     360       25378 :             segmentpath = strchr(fnonly, '.');
     361       25378 :             if (segmentpath != NULL)
     362             :             {
     363         142 :                 *segmentpath++ = '\0';
     364         142 :                 segmentno = atoi(segmentpath);
     365         142 :                 if (segmentno == 0)
     366           0 :                     pg_fatal("invalid segment number %d in file name \"%s\"",
     367             :                              segmentno, fn);
     368             :             }
     369             : 
     370       25378 :             forkpath = strchr(fnonly, '_');
     371       25378 :             if (forkpath != NULL)
     372        6302 :                 *forkpath++ = '\0';
     373             : 
     374       25378 :             if (only_filenode && strcmp(only_filenode, fnonly) != 0)
     375             :                 /* filenode not to be included */
     376        7648 :                 continue;
     377             : 
     378       17730 :             dirsize += st.st_size;
     379             : 
     380             :             /*
     381             :              * No need to work on the file when calculating only the size of
     382             :              * the items in the data folder.
     383             :              */
     384       17730 :             if (!sizeonly)
     385       17730 :                 scan_file(fn, segmentno);
     386             :         }
     387          98 :         else if (S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
     388             :         {
     389             :             /*
     390             :              * If going through the entries of pg_tblspc, we assume to operate
     391             :              * on tablespace locations where only TABLESPACE_VERSION_DIRECTORY
     392             :              * is valid, resolving the linked locations and dive into them
     393             :              * directly.
     394             :              */
     395          98 :             if (strncmp("pg_tblspc", subdir, strlen("pg_tblspc")) == 0)
     396             :             {
     397             :                 char        tblspc_path[MAXPGPATH];
     398             :                 struct stat tblspc_st;
     399             : 
     400             :                 /*
     401             :                  * Resolve tablespace location path and check whether
     402             :                  * TABLESPACE_VERSION_DIRECTORY exists.  Not finding a valid
     403             :                  * location is unexpected, since there should be no orphaned
     404             :                  * links and no links pointing to something else than a
     405             :                  * directory.
     406             :                  */
     407          10 :                 snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s/%s",
     408          10 :                          path, de->d_name, TABLESPACE_VERSION_DIRECTORY);
     409             : 
     410          10 :                 if (lstat(tblspc_path, &tblspc_st) < 0)
     411           0 :                     pg_fatal("could not stat file \"%s\": %m",
     412             :                              tblspc_path);
     413             : 
     414             :                 /*
     415             :                  * Move backwards once as the scan needs to happen for the
     416             :                  * contents of TABLESPACE_VERSION_DIRECTORY.
     417             :                  */
     418          10 :                 snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s",
     419          10 :                          path, de->d_name);
     420             : 
     421             :                 /* Looks like a valid tablespace location */
     422          10 :                 dirsize += scan_directory(tblspc_path,
     423             :                                           TABLESPACE_VERSION_DIRECTORY,
     424             :                                           sizeonly);
     425             :             }
     426             :             else
     427             :             {
     428          88 :                 dirsize += scan_directory(path, de->d_name, sizeonly);
     429             :             }
     430             :         }
     431             :     }
     432         176 :     closedir(dir);
     433         176 :     return dirsize;
     434             : }
     435             : 
     436             : int
     437          62 : main(int argc, char *argv[])
     438             : {
     439             :     static struct option long_options[] = {
     440             :         {"check", no_argument, NULL, 'c'},
     441             :         {"pgdata", required_argument, NULL, 'D'},
     442             :         {"disable", no_argument, NULL, 'd'},
     443             :         {"enable", no_argument, NULL, 'e'},
     444             :         {"filenode", required_argument, NULL, 'f'},
     445             :         {"no-sync", no_argument, NULL, 'N'},
     446             :         {"progress", no_argument, NULL, 'P'},
     447             :         {"verbose", no_argument, NULL, 'v'},
     448             :         {NULL, 0, NULL, 0}
     449             :     };
     450             : 
     451          62 :     char       *DataDir = NULL;
     452             :     int         c;
     453             :     int         option_index;
     454             :     bool        crc_ok;
     455             : 
     456          62 :     pg_logging_init(argv[0]);
     457          62 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_checksums"));
     458          62 :     progname = get_progname(argv[0]);
     459             : 
     460          62 :     if (argc > 1)
     461             :     {
     462          62 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     463             :         {
     464           2 :             usage();
     465           2 :             exit(0);
     466             :         }
     467          60 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     468             :         {
     469           2 :             puts("pg_checksums (PostgreSQL) " PG_VERSION);
     470           2 :             exit(0);
     471             :         }
     472             :     }
     473             : 
     474         186 :     while ((c = getopt_long(argc, argv, "cdD:ef:NPv", long_options, &option_index)) != -1)
     475             :     {
     476         130 :         switch (c)
     477             :         {
     478          38 :             case 'c':
     479          38 :                 mode = PG_MODE_CHECK;
     480          38 :                 break;
     481           6 :             case 'd':
     482           6 :                 mode = PG_MODE_DISABLE;
     483           6 :                 break;
     484          56 :             case 'D':
     485          56 :                 DataDir = optarg;
     486          56 :                 break;
     487           8 :             case 'e':
     488           8 :                 mode = PG_MODE_ENABLE;
     489           8 :                 break;
     490          12 :             case 'f':
     491          12 :                 if (!option_parse_int(optarg, "-f/--filenode", 0,
     492             :                                       INT_MAX,
     493             :                                       NULL))
     494           0 :                     exit(1);
     495          12 :                 only_filenode = pstrdup(optarg);
     496          12 :                 break;
     497           8 :             case 'N':
     498           8 :                 do_sync = false;
     499           8 :                 break;
     500           0 :             case 'P':
     501           0 :                 showprogress = true;
     502           0 :                 break;
     503           0 :             case 'v':
     504           0 :                 verbose = true;
     505           0 :                 break;
     506           2 :             default:
     507             :                 /* getopt_long already emitted a complaint */
     508           2 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     509           2 :                 exit(1);
     510             :         }
     511             :     }
     512             : 
     513          56 :     if (DataDir == NULL)
     514             :     {
     515           0 :         if (optind < argc)
     516           0 :             DataDir = argv[optind++];
     517             :         else
     518           0 :             DataDir = getenv("PGDATA");
     519             : 
     520             :         /* If no DataDir was specified, and none could be found, error out */
     521           0 :         if (DataDir == NULL)
     522             :         {
     523           0 :             pg_log_error("no data directory specified");
     524           0 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     525           0 :             exit(1);
     526             :         }
     527             :     }
     528             : 
     529             :     /* Complain if any arguments remain */
     530          56 :     if (optind < argc)
     531             :     {
     532           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     533             :                      argv[optind]);
     534           0 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     535           0 :         exit(1);
     536             :     }
     537             : 
     538             :     /* filenode checking only works in --check mode */
     539          56 :     if (mode != PG_MODE_CHECK && only_filenode)
     540             :     {
     541           4 :         pg_log_error("option -f/--filenode can only be used with --check");
     542           4 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     543           4 :         exit(1);
     544             :     }
     545             : 
     546             :     /* Read the control file and check compatibility */
     547          52 :     ControlFile = get_controlfile(DataDir, &crc_ok);
     548          52 :     if (!crc_ok)
     549           0 :         pg_fatal("pg_control CRC value is incorrect");
     550             : 
     551          52 :     if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
     552           0 :         pg_fatal("cluster is not compatible with this version of pg_checksums");
     553             : 
     554          52 :     if (ControlFile->blcksz != BLCKSZ)
     555             :     {
     556           0 :         pg_log_error("database cluster is not compatible");
     557           0 :         pg_log_error_detail("The database cluster was initialized with block size %u, but pg_checksums was compiled with block size %u.",
     558             :                             ControlFile->blcksz, BLCKSZ);
     559           0 :         exit(1);
     560             :     }
     561             : 
     562             :     /*
     563             :      * Check if cluster is running.  A clean shutdown is required to avoid
     564             :      * random checksum failures caused by torn pages.  Note that this doesn't
     565             :      * guard against someone starting the cluster concurrently.
     566             :      */
     567          52 :     if (ControlFile->state != DB_SHUTDOWNED &&
     568           2 :         ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
     569           2 :         pg_fatal("cluster must be shut down");
     570             : 
     571          50 :     if (ControlFile->data_checksum_version == 0 &&
     572           8 :         mode == PG_MODE_CHECK)
     573           2 :         pg_fatal("data checksums are not enabled in cluster");
     574             : 
     575          48 :     if (ControlFile->data_checksum_version == 0 &&
     576           6 :         mode == PG_MODE_DISABLE)
     577           2 :         pg_fatal("data checksums are already disabled in cluster");
     578             : 
     579          46 :     if (ControlFile->data_checksum_version > 0 &&
     580          42 :         mode == PG_MODE_ENABLE)
     581           2 :         pg_fatal("data checksums are already enabled in cluster");
     582             : 
     583             :     /* Operate on all files if checking or enabling checksums */
     584          44 :     if (mode == PG_MODE_CHECK || mode == PG_MODE_ENABLE)
     585             :     {
     586             :         /*
     587             :          * If progress status information is requested, we need to scan the
     588             :          * directory tree twice: once to know how much total data needs to be
     589             :          * processed and once to do the real work.
     590             :          */
     591          42 :         if (showprogress)
     592             :         {
     593           0 :             total_size = scan_directory(DataDir, "global", true);
     594           0 :             total_size += scan_directory(DataDir, "base", true);
     595           0 :             total_size += scan_directory(DataDir, "pg_tblspc", true);
     596             :         }
     597             : 
     598          42 :         (void) scan_directory(DataDir, "global", false);
     599          26 :         (void) scan_directory(DataDir, "base", false);
     600          26 :         (void) scan_directory(DataDir, "pg_tblspc", false);
     601             : 
     602          26 :         if (showprogress)
     603           0 :             progress_report(true);
     604             : 
     605          26 :         printf(_("Checksum operation completed\n"));
     606          26 :         printf(_("Files scanned:   %lld\n"), (long long) files_scanned);
     607          26 :         printf(_("Blocks scanned:  %lld\n"), (long long) blocks_scanned);
     608          26 :         if (mode == PG_MODE_CHECK)
     609             :         {
     610          22 :             printf(_("Bad checksums:  %lld\n"), (long long) badblocks);
     611          22 :             printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
     612             : 
     613          22 :             if (badblocks > 0)
     614           8 :                 exit(1);
     615             :         }
     616           4 :         else if (mode == PG_MODE_ENABLE)
     617             :         {
     618           4 :             printf(_("Files written:  %lld\n"), (long long) files_written);
     619           4 :             printf(_("Blocks written: %lld\n"), (long long) blocks_written);
     620             :         }
     621             :     }
     622             : 
     623             :     /*
     624             :      * Finally make the data durable on disk if enabling or disabling
     625             :      * checksums.  Flush first the data directory for safety, and then update
     626             :      * the control file to keep the switch consistent.
     627             :      */
     628          20 :     if (mode == PG_MODE_ENABLE || mode == PG_MODE_DISABLE)
     629             :     {
     630           6 :         ControlFile->data_checksum_version =
     631           6 :             (mode == PG_MODE_ENABLE) ? PG_DATA_CHECKSUM_VERSION : 0;
     632             : 
     633           6 :         if (do_sync)
     634             :         {
     635           2 :             pg_log_info("syncing data directory");
     636           2 :             fsync_pgdata(DataDir, PG_VERSION_NUM);
     637             :         }
     638             : 
     639           6 :         pg_log_info("updating control file");
     640           6 :         update_controlfile(DataDir, ControlFile, do_sync);
     641             : 
     642           6 :         if (verbose)
     643           0 :             printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
     644           6 :         if (mode == PG_MODE_ENABLE)
     645           4 :             printf(_("Checksums enabled in cluster\n"));
     646             :         else
     647           2 :             printf(_("Checksums disabled in cluster\n"));
     648             :     }
     649             : 
     650          20 :     return 0;
     651             : }

Generated by: LCOV version 1.14