1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
|
#!/usr/bin/perl
# BackupPC_refCountUpdate: Pool reference count updater
#
# DESCRIPTION
#
# BackupPC_refCountUpdate checks the pool reference counts
#
# Usage: BackupPC_refCountUpdate
#
# AUTHOR
# Craig Barratt <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
# Copyright (C) 2001-2020 Craig Barratt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================
use strict;
no utf8;
use lib "__INSTALLDIR__/lib";
use Getopt::Std;
use Fcntl qw(:mode);
use File::Path;
use Data::Dumper;
use BackupPC::Lib;
use BackupPC::XS;
use BackupPC::DirOps qw( :BPC_DT_ALL );
select(STDOUT); $| = 1;
my %opts;
if ( !getopts("cFfnmpsvh:r:P:o:", \%opts) || @ARGV != 0 || !(defined($opts{h}) || $opts{m}) ) {
print <<EOF;
Usage:
BackupPC_refCountUpdate -h HOST [-c] [-f] [-F] [-o N] [-p] [-v]
With no other args, updates count db on backups with poolCntDelta files
and computes the host's total reference counts. Also builds refCnt for
any >=4.0 backups without refCnts.
-f - do an fsck on this HOST, which involves a rebuild of the
last two backup refCnts. poolCntDelta files are ignored.
Also forces fsck if requested by needFsck flag files
in TopDir/pc/HOST/refCnt. Equivalent to -o 2.
-F - rebuild all the >=4.0 per-backup refCnt files for this
host. Equivalent to -o 3.
-c - compare current count db to new db before replacing
-o N - override \$Conf{RefCntFsck}.
-p - don't show progress
-v - verbose
Notes: in case there are legacy (ie: <=4.0.0alpha3) unapplied poolCntDelta
files in TopDir/pc/HOST/refCnt then the -f flag is turned on.
BackupPC_refCountUpdate -m [-f] [-p] [-c] [-r N-M] [-s] [-v] [-P phase]
-m Updates main count db, based on each HOST
-f - do an fsck on all the hosts, ignoring poolCntDelta files,
and replacing each host's count db. Will wait for backups
to finish if any are running.
-F - rebuild all the >=4.0 per-backup refCnt files.
-p - don't show progress
-c - clean pool files
-r N-M - process a subset of the main count db, 0 <= N <= M <= 255
-s - prints stats
-v - verbose
-P phase Phase from 0..15 each time we run BackupPC_nightly. Used
to compute exact pool size for portions of the pool based
on the phase and \$Conf{PoolSizeNightlyUpdatePeriod}.
EOF
exit(1);
}
my $ErrorCnt = 0;
my $refCntStart = 0;
my $refCntEnd = 127;
my $EmptyMD5 = pack("H*", "d41d8cd98f00b204e9800998ecf8427e");
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my $Hosts = $bpc->HostInfoRead();
my @Hosts = sort(keys(%$Hosts));
if ( $opts{h} ne "" ) {
if ( !defined($Hosts->{$opts{h}}) ) {
print(STDERR "BackupPC_refCountUpdate: host $opts{h} doesn't exist\n");
exit(1);
}
$bpc->ConfigRead($opts{h});
}
my %Conf = $bpc->Conf();
my $PoolStats = {};
if ( $opts{v} ) {
$Conf{XferLogLevel} = 8;
BackupPC::XS::Lib::logLevelSet(8);
}
#
# Write new-style attrib files (<= 4.0.0beta3 uses old-style), which are 0-length
# files with the digest encoded in the file name (eg: attrib_md5HexDigest). We
# can still read the old-style files, and we upgrade them as we go.
#
BackupPC::XS::Attrib::backwardCompat(0, 0);
print("__bpc_pidStart__ $$\n") if ( !$opts{p} );
if ( defined($opts{h}) ) {
updateHostPoolCnt($opts{h}, $opts{f}, $opts{F}, $opts{c});
} elsif ( $opts{m} ) {
if ( $opts{r} =~ /^(\d+)-(\d+)$/ && 0 <= $1 && $1 <= $2 && $2 <= 255 ) {
$refCntStart = int($1 / 2);
$refCntEnd = int($2 / 2);
} elsif ( defined($opts{r}) ) {
print(STDERR "BackupPC_refCountUpdate: -r arguments should be N-M where 0 <= N <= M <= 255\n");
print("__bpc_pidEnd__ $$\n") if ( !$opts{p} );
exit(1);
}
#
# update all the pool count changes for each host
#
if ( $opts{f} || $opts{F} ) {
updateAllHostPoolCnt($opts{f}, $opts{F});
}
#
# rebuild the overall pool count database
#
updateTotalRefCnt();
#
# clean files from the pool that are no longer referenced,
# and mark candidate files for future cleaning. This prints
# stats too, so if not cleaning, we need to print stats, if
# requested.
#
if ( $opts{c} ) {
cleanPoolFiles();
} elsif ( $opts{s} ) {
statsPrint();
}
}
print("BackupPC_refCountUpdate total errors: $ErrorCnt\n") if ( $ErrorCnt > 0 || $Conf{XferLogLevel} >= 2 );
print("__bpc_pidEnd__ $$\n") if ( !$opts{p} );
exit($ErrorCnt ? 1 : 0);
#
# For a given host, either update the poolCnt files using the poolCntDelta files,
# or if $forceFsck, completely rebuild the poolCnt files from scratch.
#
sub updateHostPoolCnt
{
my($host, $forceLast2Fsck, $forceFullFsck, $compareCurr) = @_;
my $pcDir = "$TopDir/pc/$host";
my $refCntDir = "$pcDir/refCnt";
my $errorCntSave = $ErrorCnt;
my @fsckFiles;
my @Backups = $bpc->BackupInfoRead($host);
my $startClock = time;
mkdir($refCntDir, 0770) if ( !-d $refCntDir );
#
# Grab the lock to make sure no dumps start. Try a non-blocking request
# first, so we can warn the user if we have to wait.
#
my $lockFd = BackupPC::XS::DirOps::lockRangeFile("$refCntDir/LOCK", 0, 1, 0);
if ( $lockFd < 0 ) {
print("Backup running on host $host.... waiting\n");
$lockFd = BackupPC::XS::DirOps::lockRangeFile("$refCntDir/LOCK", 0, 1, 1);
}
if ( $lockFd < 0 ) {
print("Unable to get lock on $refCntDir/LOCK... skipping $host\n");
$ErrorCnt++;
return;
}
#
# Clean up the HOST/refCnt directory to see if an fsck is needed
#
foreach my $e ( @{BackupPC::DirOps::dirRead($bpc, $refCntDir)} ) {
unlink("$refCntDir/$e->{name}") if ( $e->{name} =~ /^poolCntNew/ );
if ( $e->{name} =~ /^t?poolCntDelta/ ) {
#
# legacy <= 4.0.0beta3 code left poolCntDelta files in the host's refCnt
# directory, so remove them and do a last2Fsck
#
unlink("$refCntDir/$e->{name}");
$forceLast2Fsck = 1;
}
next if ( $e->{name} !~ /^needFsck/ );
unlink("$refCntDir/$e->{name}");
$forceLast2Fsck = 1;
}
#
# Apply policy in $Conf{RefCntFsck} (potentially overridden by -o N)
# 0: no additional fsck
# 1: do an fsck on the last backup if it is from a full backup (handled below)
# 2: do an fsck on the last two backups always
# 3: do a full fsck on all the backups
#
my $ConfRefCntFsck = $opts{o} =~ /^\d+$/ ? $opts{o} : $Conf{RefCntFsck};
if ( $ConfRefCntFsck == 2 ) {
print("BackupPC_refCountUpdate: doing fsck on last two backups on $host since \$ConfRefCntFsck == 2\n");
$forceLast2Fsck = 1;
} elsif ( $ConfRefCntFsck == 3 ) {
print("BackupPC_refCountUpdate: doing fsck on all backups on $host since \$ConfRefCntFsck == 3\n");
$forceFullFsck = 1;
}
#
# Figure out which backups to rebuild or apply to deltas
#
my $bkupList = [];
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
my $bkupNum = $Backups[$i]{num};
my $compress = $Backups[$i]{compress};
my $bkupRefCntDir = "$pcDir/$bkupNum/refCnt";
my($gotFsck, $gotDelta, $gotPoolCnt, $gotNoPoolCntOk);
next if ( $Backups[$i]{version} eq "" || ($Backups[$i]{version} =~ /^[23]\./ && !-d $bkupRefCntDir) );
if ( !-d $bkupRefCntDir ) {
print("BackupPC_refCountUpdate: doing fsck on $host #$bkupNum since $bkupRefCntDir doesn't exist\n");
mkdir($bkupRefCntDir, 0770);
push(@$bkupList, {bkupNum => $bkupNum, fsck => 1, compress => $compress});
next;
}
my $entries = BackupPC::DirOps::dirRead($bpc, $bkupRefCntDir);
foreach my $e ( @$entries ) {
unlink("$bkupRefCntDir/$e->{name}") if ( $e->{name} =~ /^poolCntNew/ );
if ( $e->{name} =~ /^needFsck/ || $e->{name} =~ /^tpoolCntDelta/ ) {
unlink("$bkupRefCntDir/$e->{name}");
$gotFsck = 1;
}
$gotDelta = 1 if ( $e->{name} =~ /^poolCntDelta/ );
$gotPoolCnt = 1 if ( $e->{name} =~ /^poolCnt\./ );
$gotNoPoolCntOk = 1 if ( $e->{name} =~ /^noPoolCntOk/ );
}
print( "BackupPC_refCountUpdate: host $host #$bkupNum: gotFsck = $gotFsck, gotDelta = $gotDelta,"
. " gotPoolCnt = $gotPoolCnt, gotNoPoolCntOk = $gotNoPoolCntOk\n")
if ( $Conf{XferLogLevel} >= 4 );
#
# Apply policy in $ConfRefCntFsck:
# 0: no additional fsck
# 1: do an fsck on the last backup if it is from a full backup
# 2: do an fsck on the last two backups always (handled above)
# 3: do a full fsck on all the backups (handled above)
#
if ( $ConfRefCntFsck == 1 && $i == @Backups - 1 && $Backups[$i]{type} eq "full" ) {
print("BackupPC_refCountUpdate: doing fsck on $host #$bkupNum (full) since \$ConfRefCntFsck == 1\n");
$gotFsck = 1;
}
if ( !$gotPoolCnt && !$gotFsck && !$gotNoPoolCntOk ) {
#
# This shouldn't happen - we should have some pool files, so force an fsck
#
print("BackupPC_refCountUpdate: doing fsck on $host #$bkupNum since there are no poolCnt files\n");
$gotFsck = 1;
}
if ( $forceFullFsck || ($forceLast2Fsck && $i >= @Backups - 2) || $gotFsck ) {
push(@$bkupList, {bkupNum => $bkupNum, fsck => 1, compress => $compress});
} elsif ( $gotDelta ) {
push(@$bkupList, {bkupNum => $bkupNum, fsck => 0, compress => $compress});
}
}
while ( (my $b = shift(@$bkupList)) ) {
my $errorCntSave0 = $ErrorCnt;
if ( processOneHostBackup($host, $b->{bkupNum}, $b->{fsck}, $b->{compress}) && !$b->{fsck} ) {
print(
"BackupPC_refCountUpdate: given errors, redoing host $host #$b->{bkupNum} with fsck (reset errorCnt to $errorCntSave0)\n"
);
$ErrorCnt = $errorCntSave0;
unshift(@$bkupList, {bkupNum => $b->{bkupNum}, fsck => 1, compress => $b->{compress}});
}
}
#
# Now add up all the per-backup counts to get the per-host totals
#
my $errorCntSave0 = $ErrorCnt;
print("BackupPC_refCountUpdate: computing totals for host $host\n") if ( $Conf{XferLogLevel} >= 2 );
my $needFsck = updateHostRefCounts($host);
my $bkupRedoDone = {};
while ( $needFsck ) {
my $listText = join(", ", @$needFsck);
print(
"BackupPC_refCountUpdate: due to errors, doing fsck on host $host backups $listText; (reset errorCnt to $errorCntSave0)\n"
);
$ErrorCnt = $errorCntSave0;
foreach my $bkupNum ( @$needFsck ) {
next if ( $bkupRedoDone->{$bkupNum} );
my $compress = 1;
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
next if ( $Backups[$i]{num} != $bkupNum );
$compress = $Backups[$i]{compress};
last;
}
processOneHostBackup($host, $bkupNum, 1, $compress);
$bkupRedoDone->{$bkupNum} = 1;
}
$needFsck = updateHostRefCounts($host);
}
#
# and rename the new count files to replace the current ones
#
renameNewCntFiles($host, $refCntDir, $compareCurr, "total");
#
# Now give the lock back
#
BackupPC::XS::DirOps::unlockRangeFile($lockFd);
printf(
"BackupPC_refCountUpdate: host %s got %d errors (took %d secs)\n",
$host,
$ErrorCnt - $errorCntSave,
time - $startClock
);
$bpc->flushXSLibMesgs();
}
sub processOneHostBackup
{
my($host, $bkupNum, $fsck, $compress) = @_;
my $pcDir = "$TopDir/pc/$host";
my $bkupRefCntDir = "$pcDir/$bkupNum/refCnt";
my $errorCntSave0 = $ErrorCnt;
my $fd;
#
# add a placeholder fsck request, in case we die unexpectedly
#
open($fd, ">", "$bkupRefCntDir/needFsck.refCountUpdate") && close($fd);
print("BackupPC_refCountUpdate: processing host $host #$bkupNum (fsck = $fsck)\n") if ( $Conf{XferLogLevel} >= 2 );
if ( $fsck ) {
my $entries = BackupPC::DirOps::dirRead($bpc, $bkupRefCntDir);
#
# Remove any delta files since we are rebuilding
#
foreach my $e ( @$entries ) {
unlink("$bkupRefCntDir/$e->{name}") if ( $e->{name} =~ /^t?poolCntDelta/ || $e->{name} =~ /^poolCntNew/ );
}
my $deltaInfo = BackupPC::XS::DeltaRefCnt::new("$pcDir/$bkupNum");
print("__bpc_progress_state__ refCnt #$bkupNum\n") if ( !$opts{p} );
my($err, $inodeMax) = BackupPC::XS::DirOps::refCountAllInodeMax("$pcDir/$bkupNum", $compress, 1, $deltaInfo);
$ErrorCnt += $err;
$deltaInfo->flush();
$bpc->flushXSLibMesgs();
#
# Update inodeLast for this backup if it is larger than what's stored in backups
#
my @backups = $bpc->BackupInfoRead($host);
for ( my $i = 0 ; $i < @backups ; $i++ ) {
next if ( $backups[$i]{num} != $bkupNum || $backups[$i]{inodeLast} > $inodeMax );
$inodeMax += 2;
print(
"BackupPC_refCountUpdate: $host #$bkupNum inodeLast set to $inodeMax (was $backups[$i]{inodeLast})\n");
$backups[$i]{inodeLast} = $inodeMax;
BackupPC::Storage->backupInfoWrite($pcDir, $bkupNum, $backups[$i], 1);
$bpc->BackupInfoWrite($host, @backups);
last;
}
}
print("__bpc_progress_state__ cntUpdate #$bkupNum\n") if ( !$opts{p} );
updateHostDelta2Cnt($host, $bkupRefCntDir, $fsck, 0, $bkupNum);
$bpc->flushXSLibMesgs();
#
# delete the noPoolCntOk if there are pool files, or create one
# if there are none
#
my($gotNoPoolCntOk, $gotPoolCnt);
foreach my $e ( @{BackupPC::DirOps::dirRead($bpc, $bkupRefCntDir)} ) {
$gotPoolCnt = 1 if ( $e->{name} =~ /^poolCnt\./ );
$gotNoPoolCntOk = 1 if ( $e->{name} =~ /^noPoolCntOk/ );
}
if ( $gotPoolCnt && $gotNoPoolCntOk ) {
unlink("$bkupRefCntDir/noPoolCntOk");
} elsif ( !$gotPoolCnt ) {
open(my $fd, ">", "$bkupRefCntDir/noPoolCntOk") && close($fd);
}
unlink("$bkupRefCntDir/needFsck.refCountUpdate");
return 1 if ( $errorCntSave0 != $ErrorCnt );
}
#
# Process all the per-backup ref counts to create the total ref count for the host.
# Returns undef on success, or a listref of backup numbers that need fscks.
#
sub updateHostRefCounts
{
my($host) = @_;
my @Backups = $bpc->BackupInfoRead($host);
my $refCntDestDir = "$TopDir/pc/$host/refCnt";
my $needFsck = {};
print("__bpc_progress_state__ sumUpdate\n") if ( !$opts{p} );
for ( my $refCntFile = 0 ; $refCntFile < 128 ; $refCntFile++ ) {
print("__bpc_progress_fileCnt__ $refCntFile/128\n") if ( !$opts{p} && ($refCntFile & 0x7) == 0 );
for ( my $compress = 0 ; $compress < 2 ; $compress++ ) {
my $count = BackupPC::XS::PoolRefCnt::new();
my $countDirty;
my $poolCntFileNew = sprintf("%s/poolCntNew.%d.%02x", $refCntDestDir, $compress, $refCntFile * 2);
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
my $bkupNum = $Backups[$i]{num};
my $refCntBkupDir = "$TopDir/pc/$host/$bkupNum/refCnt";
next if ( $Backups[$i]{version} eq "" || ($Backups[$i]{version} =~ /^[23]\./ && !-d $refCntBkupDir) );
my $poolCntHostFile = sprintf("%s/poolCnt.%d.%02x", $refCntBkupDir, $compress, $refCntFile * 2);
next if ( !-f $poolCntHostFile );
my $countHost = BackupPC::XS::PoolRefCnt::new();
if ( $countHost->read($poolCntHostFile) ) {
print("Can't open pool count file $poolCntHostFile\n");
$ErrorCnt++;
next;
}
my($d, $c);
my $idx = 0;
while ( 1 ) {
($d, $c, $idx) = $countHost->iterate($idx);
last if ( !defined($d) );
if ( $c < 0 ) {
printf(
"BackupPC_refCountUpdate: host %s (%s) digest %s has negative count (%d); will do fsck on #%d\n",
$host, $poolCntHostFile, unpack("H*", $d),
$c, $bkupNum
);
$needFsck->{$bkupNum}++;
$ErrorCnt++;
} elsif ( $c > 0 ) {
$count->incr($d, $c);
$countDirty = 1;
}
}
}
if ( $countDirty && $count->write($poolCntFileNew) ) {
print("Can't write new host pool count file $poolCntFileNew\n");
$ErrorCnt++;
}
$count = undef;
$bpc->flushXSLibMesgs();
}
}
if ( keys(%$needFsck) ) {
return [sort({ $a <=> $b } keys(%$needFsck))];
}
}
#
# Process all the delta files in the given directory and update the poolCnt files
#
sub updateHostDelta2Cnt
{
my($host, $refCntDir, $forceFsck, $compareCurr, $bkupNum) = @_;
my $gotDeltas;
#
# Read all the poolCntDelta files, and update the host's poolCnt files
# (or overwrite them if $forceFsck)
#
my $entries = BackupPC::DirOps::dirRead($bpc, $refCntDir);
foreach my $e ( @$entries ) {
unlink("$refCntDir/$e->{name}") if ( $e->{name} =~ /^poolCntNew/ );
}
foreach my $e ( @$entries ) {
next if ( $e->{name} !~ /^poolCntDelta/ );
my $deltaFileName = "$refCntDir/$e->{name}";
my $compress = 1;
$compress = $1 if ( $deltaFileName =~ m{/poolCntDelta_(\d)_} );
$gotDeltas++;
updateHostPoolOneDelta2Cnt($host, $refCntDir, $deltaFileName, !$forceFsck, $compress, $bkupNum);
}
renameNewCntFiles($host, $refCntDir, $compareCurr, "#$bkupNum") if ( $gotDeltas );
}
#
# Rename all the new pool files, replacing the old ones
#
sub renameNewCntFiles
{
my($host, $refCntDir, $compareCurr, $statusText) = @_;
print("__bpc_progress_state__ rename $statusText\n") if ( !$opts{p} );
for ( my $refCntFile = 0 ; $refCntFile < 128 ; $refCntFile++ ) {
for ( my $compress = 0 ; $compress < 2 ; $compress++ ) {
my $poolCntFileNew = sprintf("%s/poolCntNew.%d.%02x", $refCntDir, $compress, $refCntFile * 2);
my $poolCntFileCur = sprintf("%s/poolCnt.%d.%02x", $refCntDir, $compress, $refCntFile * 2);
if ( $compareCurr ) {
$ErrorCnt += poolCountHostNewCompare($host, $compress, $poolCntFileCur, $poolCntFileNew);
}
if ( -f $poolCntFileNew ) {
if ( !rename($poolCntFileNew, $poolCntFileCur) ) {
print("BackupPC_refCountUpdate: can't rename $poolCntFileNew to $poolCntFileCur ($!)\n");
unlink($poolCntFileNew);
$ErrorCnt++;
next;
}
} elsif ( -f $poolCntFileCur && !unlink($poolCntFileCur) ) {
print("BackupPC_refCountUpdate: can't unlink $poolCntFileCur ($!)\n");
$ErrorCnt++;
next;
}
}
}
}
#
# Apply a single pool delta file to the host's new count database.
#
sub updateHostPoolOneDelta2Cnt
{
my($host, $refCntDir, $deltaFileName, $accumCurr, $compress, $bkupNum) = @_;
my $count = BackupPC::XS::PoolRefCnt::new();
if ( $count->read($deltaFileName) ) {
print("BackupPC_refCountUpdate: can't read pool count delta file $deltaFileName\n");
$ErrorCnt++;
return;
}
my($delta, $d, $c, $entryCnt);
my $idx = 0;
while ( 1 ) {
($d, $c, $idx) = $count->iterate($idx);
last if ( !defined($d) );
$delta->[vec($d, 0, 8) >> 1]{$d} = $c;
$entryCnt++;
}
$count = undef;
print("BackupPC_refCountUpdate: processing host $host #$bkupNum deltaFile $deltaFileName with $entryCnt entries\n")
if ( $Conf{XferLogLevel} >= 2 );
for ( my $refCntFile = 0 ; $refCntFile < 128 ; $refCntFile++ ) {
my $poolCntFileNew = sprintf("%s/poolCntNew.%d.%02x", $refCntDir, $compress, $refCntFile * 2);
my $poolCntFileCur = sprintf("%s/poolCnt.%d.%02x", $refCntDir, $compress, $refCntFile * 2);
if ( !defined($delta->[$refCntFile]) || !%{$delta->[$refCntFile]} ) {
next if ( -f $poolCntFileNew || !-f $poolCntFileCur );
}
#
# Read the existing count
#
my $count = BackupPC::XS::PoolRefCnt::new();
if ( -f $poolCntFileNew ) {
if ( $count->read($poolCntFileNew) ) {
print("BackupPC_refCountUpdate: can't open new pool count file $poolCntFileNew\n");
$ErrorCnt++;
next;
}
} else {
#
# Read in the existing counts if we are accumulating.
#
if ( $accumCurr && -f $poolCntFileCur && $count->read($poolCntFileCur) ) {
print("BackupPC_refCountUpdate: can't open cur pool count file $poolCntFileCur\n");
$ErrorCnt++;
next;
}
}
#
# Apply the deltas and write the file back; it's an error for any count to
# be negative. Remove zero counts.
#
foreach my $d ( keys(%{$delta->[$refCntFile]}) ) {
my $c = $count->incr($d, $delta->[$refCntFile]{$d});
$count->delete($d) if ( $c == 0 );
next if ( $c >= 0 );
printf(
"BackupPC_refCountUpdate: got a negative count for %s processing %s; redoing #%d\n",
unpack("H*", $d),
$deltaFileName, $bkupNum
);
$ErrorCnt++;
last;
}
if ( $count->write($poolCntFileNew) ) {
print("BackupPC_refCountUpdate: can't write new pool count file $poolCntFileNew\n");
$ErrorCnt++;
next;
}
}
$delta = {};
if ( unlink($deltaFileName) != 1 ) {
print("BackupPC_refCountUpdate: can't unlink $deltaFileName ($!)\n");
$ErrorCnt++;
}
}
#
# Compare a new pool count file with the existing one. Returns an error count.
# After the compare, renames the new pool count to replace the existing
# one.
#
sub poolCountHostNewCompare
{
my($host, $compress, $poolCntFileCur, $poolCntFileNew) = @_;
my $errorCnt = 0;
my $countNew = BackupPC::XS::PoolRefCnt::new();
my $count = BackupPC::XS::PoolRefCnt::new();
if ( -f $poolCntFileNew && $countNew->read($poolCntFileNew) ) {
print("BackupPC_refCountUpdate: can't open new pool count file $poolCntFileNew\n");
$errorCnt++;
}
if ( -f $poolCntFileCur && $count->read($poolCntFileCur) ) {
print("BackupPC_refCountUpdate: can't open current pool count file $poolCntFileCur\n");
$errorCnt++;
}
#
# Compare the new and existing counts
#
my($digest, $cnt);
my $idx = 0;
while ( 1 ) {
($digest, $cnt, $idx) = $count->iterate($idx);
last if ( !defined($digest) );
if ( !defined($countNew->get($digest)) && $cnt > 0 ) {
printf("BackupPC_refCountUpdate: host %s digest.%d %s count is %d, but should be 0\n",
$host, $compress, unpack("H*", $digest), $cnt);
$errorCnt++;
next;
}
if ( $cnt != $countNew->get($digest) ) {
printf(
"BackupPC_refCountUpdate: host %s digest.%d %s count is %d, but should be %d\n",
$host, $compress, unpack("H*", $digest),
$cnt, $countNew->get($digest)
);
$errorCnt++;
}
$countNew->delete($digest);
}
$idx = 0;
while ( 1 ) {
($digest, $cnt, $idx) = $countNew->iterate($idx);
last if ( !defined($digest) );
printf(
"BackupPC_refCountUpdate: host %s digest.%d %s count missing, but should be %d\n",
$host, $compress,
unpack("H*", $digest),
$countNew->get($digest)
);
$errorCnt++;
}
return $errorCnt;
}
sub updateAllHostPoolCnt
{
my($forceLast2Fsck, $forceFullFsck) = @_;
foreach my $host ( @Hosts ) {
updateHostPoolCnt($host, $forceLast2Fsck, $forceFullFsck, 0);
}
}
#
# Accumulate all the host poolRefCnt files to create the
# overall pool reference counts
#
sub updateTotalRefCnt
{
return if ( $ErrorCnt );
print("__bpc_progress_state__ refCnt pool\n") if ( !$opts{p} );
for ( my $compress = 0 ; $compress < 2 ; $compress++ ) {
my $poolName = $compress ? "cpool4" : "pool4";
for ( my $refCntFile = $refCntStart ; $refCntFile <= $refCntEnd ; $refCntFile++ ) {
#
# Count the number of pool directories
#
print("__bpc_progress_fileCnt__ cntUpdate $refCntFile/$refCntEnd\n") if ( !$opts{p} );
my $poolDir = sprintf("%s/%02x", $compress ? $bpc->{CPoolDir} : $bpc->{PoolDir}, $refCntFile * 2);
next if ( !-d $poolDir );
$PoolStats->{$poolName}[$refCntFile]{dirCnt}++;
my $entries = BackupPC::DirOps::dirRead($bpc, $poolDir);
foreach my $e ( @$entries ) {
next if ( $e->{name} !~ /^[\da-f][\da-f]$/ );
$PoolStats->{$poolName}[$refCntFile]{dirCnt}++;
}
#
# For each host update the per-host count
#
my $poolCntFile = "$poolDir/poolCnt";
my $count = BackupPC::XS::PoolRefCnt::new();
my $countCopy = BackupPC::XS::PoolRefCnt::new();
my $countCurr = BackupPC::XS::PoolRefCnt::new();
$countCurr->read($poolCntFile) if ( -f $poolCntFile );
foreach my $host ( @Hosts ) {
my $refCntDir = "$TopDir/pc/$host/refCnt";
my $hostCntFile = sprintf("%s/poolCnt.%d.%02x", $refCntDir, $compress, $refCntFile * 2);
my $countHost = BackupPC::XS::PoolRefCnt::new();
$countHost->read($hostCntFile) if ( -f $hostCntFile );
my($d, $c);
my $idx = 0;
while ( 1 ) {
($d, $c, $idx) = $countHost->iterate($idx);
last if ( !defined($d) );
if ( $c < 0 ) {
printf("BackupPC_refCountUpdate: host %s (%s) digest %s has negative count (%d)\n",
$host, $hostCntFile, unpack("H*", $d), $c);
$ErrorCnt++;
}
if ( !defined($countCurr->get($d)) ) {
#
# add stats for the new pool file
#
my $poolFile = $bpc->MD52Path($d, $compress);
my @s = stat($poolFile);
if ( @s ) {
my $nBlks = $s[12];
$PoolStats->{$poolName}[$refCntFile]{blkCnt} += $nBlks;
if ( $c > 0 ) {
if ( ($s[2] & S_IXOTH) && chmod(0444, $poolFile) != 1 ) {
print("BackupPC_refCountUpdate: can't chmod 0444 $poolFile\n");
$ErrorCnt++;
}
}
}
} elsif ( $countCurr->get($d) == 0 && $c > 0 ) {
#
# remove S_IXOTH flag since this digest is now referenced
#
my $poolFile = $bpc->MD52Path($d, $compress);
my @s = stat($poolFile);
if ( @s ) {
if ( ($s[2] & S_IXOTH) && chmod(0444, $poolFile) != 1 ) {
print("BackupPC_refCountUpdate: can't chmod 0444 $poolFile\n");
$ErrorCnt++;
}
}
}
$count->incr($d, $c);
$countCopy->incr($d, $c);
$countCurr->incr($d, $c); # make sure we only count the new pool file once.
}
}
#
# Add entries for any files in the existing pool count that aren't already in count/countCopy.
#
if ( -f $poolCntFile ) {
my($d, $c);
my $idx = 0;
while ( 1 ) {
($d, $c, $idx) = $countCurr->iterate($idx);
last if ( !defined($d) );
next if ( defined($count->get($d)) );
$count->incr($d, 0);
$countCopy->incr($d, 0);
}
}
#
# Scan the pool to add any missing files that have a zero count
#
for ( my $subDir = 0 ; $subDir < 128 ; $subDir++ ) {
my $poolSubDir = sprintf("%s/%02x", $poolDir, $subDir * 2);
my $entries = BackupPC::DirOps::dirRead($bpc, $poolSubDir);
foreach my $e ( @$entries ) {
next if ( $e->{name} eq "." || $e->{name} eq ".." );
if ( $e->{name} !~ /^[\da-f]{32,48}$/ && $e->{name} !~ /lock/i ) {
print("BackupPC_refCountUpdate: unknown pool file $poolSubDir/$e->{name} removed\n");
unlink("$poolSubDir/$e->{name}");
next;
}
my $d = pack("H*", $e->{name});
my $b2 = vec($d, 0, 16);
if ( $refCntFile != (($b2 >> 8) & 0xfe) / 2 || $subDir != (($b2 >> 0) & 0xfe) / 2 ) {
print("BackupPC_refCountUpdate: unexpected pool file $poolSubDir/$e->{name} removed\n");
unlink("$poolSubDir/$e->{name}");
next;
}
#
# check a small random subset of the pool file's actual md5 against its file name
#
if ( rand(100) < $Conf{PoolNightlyDigestCheckPercent} ) {
my $md5 = BackupPC::XS::FileDigest::digest("$poolSubDir/$e->{name}", $compress);
if ( substr($d, 0, 16) ne $md5 ) {
printf(
"BackupPC_refCountUpdate: ERROR pool file %s has digest %s instead of %s\n",
"$poolSubDir/$e->{name}",
unpack("H*", $md5),
unpack("H*", substr($d, 0, 16))
);
} else {
print("BackupPC_refCountUpdate: pool file $poolSubDir/$e->{name} md5 digest verified\n")
if ( $Conf{XferLogLevel} >= 5 );
}
}
if ( !defined($count->get($d)) ) {
my @s = stat("$poolSubDir/$e->{name}");
print("BackupPC_refCountUpdate: adding pool file $e->{name} with count 0 (size $s[7])\n")
if ( $Conf{XferLogLevel} >= 5 );
#
# add stats for new pool file
#
my $nBlks = $s[12];
$PoolStats->{$poolName}[$refCntFile]{blkCnt} += $nBlks;
$count->incr($d, 0);
} else {
$countCopy->delete($d);
}
}
}
#
# Compute the full pool size periodically, in case the relative +/- above
# doesn't exactly track.
#
# Normally we only update relative changes to the pool size (when a new pool
# file is added, or an old one is deleted), which is a lot more efficient.
#
# So decide when to do a full pool size scan. $refCntFile goes from 0..127,
# phase ($opts{P}) is 0..15 and $Conf{PoolSizeNightlyUpdatePeriod} is
# 0, 1, 2, 4, 8, 16.
#
my $fullPoolScan;
if ( $Conf{PoolSizeNightlyUpdatePeriod} > 0 && defined($opts{P}) ) {
$fullPoolScan = (int($refCntFile / 8) % $Conf{PoolSizeNightlyUpdatePeriod}) ==
($opts{P} % $Conf{PoolSizeNightlyUpdatePeriod});
}
# print("BackupPC_refCountUpdate: computing full $poolName size for $refCntFile (phase = $opts{P},"
# . " \$Conf{PoolSizeNightlyUpdatePeriod} = $Conf{PoolSizeNightlyUpdatePeriod})\n")
# if ( $fullPoolScan );
my $blkCnt = 0; # size of pool files
my $fileCntRep = 0; # total number of pool files with repeated md5 checksums
# (ie: digest > 16 bytes; first instance isn't counted)
my $fileRepMax = 0; # worse case chain length of pool files that have repeated
# checksums (ie: max(NNN) for all digests xxxxxxxxxxxxxxxxNNN)
my $fileLinkMax = 0; # maximum number of links on a pool file
my $fileLinkTotal = 0; # total number of links on entire pool
my($digest, $cnt);
my $idx = 0;
my $poolFileCnt = 0;
while ( 1 ) {
($digest, $cnt, $idx) = $count->iterate($idx);
last if ( !defined($digest) );
$poolFileCnt++;
$fileLinkTotal += $cnt;
$fileLinkMax = $cnt if ( $fileLinkMax < $cnt && $digest ne $EmptyMD5 );
if ( $fullPoolScan ) {
my $poolFile = $bpc->MD52Path($digest, $compress);
my @s = stat($poolFile);
$blkCnt += $s[12] if ( @s );
}
next if ( length($digest) <= 16 );
my $ext = $bpc->digestExtGet($digest);
$fileCntRep++;
$fileRepMax = $ext if ( $fileRepMax < $ext );
}
$PoolStats->{$poolName}[$refCntFile]{blkCnt} = $blkCnt if ( $fullPoolScan );
$PoolStats->{$poolName}[$refCntFile]{fileCnt} = $poolFileCnt;
$PoolStats->{$poolName}[$refCntFile]{fileLinkMax} = $fileLinkMax;
$PoolStats->{$poolName}[$refCntFile]{fileLinkTotal} = $fileLinkTotal;
$PoolStats->{$poolName}[$refCntFile]{fileCntRep} = $fileCntRep;
$PoolStats->{$poolName}[$refCntFile]{fileRepMax} = $fileRepMax;
#
# Remove zero counts on pool files that don't exist. Report pool files
# that have non-zero counts and are missing.
#
$idx = 0;
while ( 1 ) {
($digest, $cnt, $idx) = $countCopy->iterate($idx);
last if ( !defined($digest) );
if ( $cnt == 0 ) {
$count->delete($digest);
next;
}
next if ( $digest eq $EmptyMD5 );
my $digestStr = unpack("H*", $digest);
print("BackupPC_refCountUpdate: missing pool file $digestStr count $cnt\n");
$ErrorCnt++;
}
if ( $count->write("$poolCntFile.$$") ) {
print("BackupPC_refCountUpdate: can't write new pool count file $poolCntFile.$$\n");
unlink("$poolCntFile.$$");
$ErrorCnt++;
return;
} elsif ( !rename("$poolCntFile.$$", $poolCntFile) ) {
print("BackupPC_refCountUpdate: can't rename $poolCntFile.$$ to $poolCntFile ($!)\n");
unlink("$poolCntFile.$$");
$ErrorCnt++;
return;
}
$bpc->flushXSLibMesgs();
}
}
}
sub cleanPoolFiles
{
for ( my $compress = 0 ; $compress < 2 ; $compress++ ) {
my $poolName = $compress ? "cpool4" : "pool4";
for ( my $refCntFile = $refCntStart ; $refCntFile <= $refCntEnd ; $refCntFile++ ) {
#
# Read the existing count
#
my $dirty = 0;
my $poolDir = sprintf("%s/%02x", $compress ? $bpc->{CPoolDir} : $bpc->{PoolDir}, $refCntFile * 2);
my $poolCntFile = "$poolDir/poolCnt";
my $count = BackupPC::XS::PoolRefCnt::new();
#
# Grab a lock to make sure BackupPC_dump won't unmark and use a pending
# delete file.
#
my $lockFd = BackupPC::XS::DirOps::lockRangeFile("$poolDir/LOCK", 0, 1, 1);
if ( -f $poolCntFile && $count->read($poolCntFile) ) {
print("BackupPC_refCountUpdate: can't read pool count file $poolCntFile\n");
$dirty = 1;
$ErrorCnt++;
}
my($digest, $cnt);
my $idx = 0;
my $minMtime = time() - 7 * 24 * 3600;
while ( 1 ) {
($digest, $cnt, $idx) = $count->iterate($idx);
last if ( !defined($digest) );
next if ( $cnt > 0 );
my $poolFile = $bpc->MD52Path($digest, $compress);
my @s = stat($poolFile);
next if ( !@s || $s[7] == 0 || $s[9] >= $minMtime );
my $mode = $s[2];
my $nBlks = $s[12];
if ( $mode & S_IXOTH ) {
#
# figure out the next file in the sequence
#
my $ext = $bpc->digestExtGet($digest);
my($nextDigest, $nextPoolFile) = $bpc->digestConcat($digest, $ext + 1, $compress);
if ( !-f $nextPoolFile ) {
#
# last in the chain (or no chain) - just delete it
#
print("BackupPC_refCountUpdate: removing pool file $poolFile\n")
if ( $Conf{XferLogLevel} >= 4 );
if ( unlink($poolFile) != 1 ) {
print("BackupPC_refCountUpdate: can't remove $poolFile\n");
$ErrorCnt++;
next;
}
} else {
#
# in a chain of pool files we can't delete so
# we replace the file with an empty file.
# first remove S_IXOTH mode
#
print("BackupPC_refCountUpdate: zeroing pool file $poolFile (next $nextPoolFile exists)\n")
if ( $Conf{XferLogLevel} >= 4 );
if ( chmod(0644, $poolFile) != 1 ) {
print("BackupPC_refCountUpdate: can't chmod 0644 $poolFile\n");
$ErrorCnt++;
}
if ( open(my $fh, ">", $poolFile) ) {
close($fh);
} else {
print("BackupPC_refCountUpdate: can't truncate $poolFile\n");
$ErrorCnt++;
next;
}
}
$count->delete($digest);
$dirty = 1;
#
# update stats
#
$PoolStats->{$poolName}[$refCntFile]{fileCnt}--;
$PoolStats->{$poolName}[$refCntFile]{blkCnt} -= $nBlks;
$PoolStats->{$poolName}[$refCntFile]{fileCntRm}++;
$PoolStats->{$poolName}[$refCntFile]{blkCntRm} += $nBlks;
} else {
#
# mark the pool file so no one links to it
#
print("BackupPC_refCountUpdate: marking pool file $poolFile\n") if ( $Conf{XferLogLevel} >= 4 );
if ( chmod(0445, $poolFile) != 1 ) {
print("BackupPC_refCountUpdate: can't chmod 0445 $poolFile\n");
$ErrorCnt++;
}
}
}
if ( $dirty ) {
#
# rewrite the poolCnt file
#
if ( $count->write("$poolCntFile.$$") ) {
print("BackupPC_refCountUpdate: can't write new pool count file $poolCntFile.$$\n");
$ErrorCnt++;
} elsif ( !rename("$poolCntFile.$$", $poolCntFile) ) {
print("BackupPC_refCountUpdate: can't rename $poolCntFile.$$ to $poolCntFile ($!)\n");
$ErrorCnt++;
}
}
BackupPC::XS::DirOps::unlockRangeFile($lockFd);
if ( $opts{s} ) {
statsPrintSingle($poolName, $refCntFile);
}
}
}
}
sub statsPrintSingle
{
my($poolName, $refCntFile) = @_;
my $s = $PoolStats->{$poolName}[$refCntFile];
my $kb = $s->{blkCnt} >= 0 ? int($s->{blkCnt} / 2 + 0.5) : int($s->{blkCnt} / 2 - 0.5);
my $kbRm = $s->{blkCntRm} >= 0 ? int($s->{blkCntRm} / 2 + 0.5) : int($s->{blkCntRm} / 2 - 0.5);
printf(
"BackupPC_stats4 %d = %s,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
$refCntFile, $poolName, $s->{fileCnt}, $s->{dirCnt},
$kb, $kbRm, $s->{fileCntRm}, $s->{fileCntRep},
$s->{fileRepMax}, $s->{fileLinkMax}, $s->{fileLinkTotal}
);
}
sub statsPrint
{
foreach my $poolName ( qw(pool4 cpool4) ) {
for ( my $refCntFile = $refCntStart ; $refCntFile <= $refCntEnd ; $refCntFile++ ) {
statsPrintSingle($poolName, $refCntFile);
}
}
}
|