1 /* 2 * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 /* 35 * Clean up specific HAMMER filesystems or all HAMMER filesystems. 36 * 37 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's 38 * are cleaned. 39 * 40 * Each HAMMER filesystem may contain a configuration file. If no 41 * configuration file is present one will be created with the following 42 * defaults: 43 * 44 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj) 45 * prune 1d 5m 46 * rebalance 1d 5m 47 * #dedup 1d 5m (not enabled by default) 48 * reblock 1d 5m 49 * recopy 30d 10m 50 * 51 * All hammer commands create and maintain cycle files in the snapshots 52 * directory. 53 * 54 * For HAMMER version 2- the configuration file is a named 'config' in 55 * the snapshots directory, which defaults to <pfs>/snapshots. 56 * For HAMMER version 3+ the configuration file is saved in filesystem 57 * meta-data. The snapshots directory defaults to /var/hammer/<pfs> 58 * (/var/hammer/root for root mount). 59 */ 60 61 #include "hammer.h" 62 63 #include <libutil.h> 64 65 struct didpfs { 66 struct didpfs *next; 67 hammer_uuid_t uuid; 68 }; 69 70 static void do_cleanup(const char *path); 71 static void config_init(const char *path, struct hammer_ioc_config *config); 72 static void migrate_config(FILE *fp, struct hammer_ioc_config *config); 73 static void migrate_snapshots(int fd, const char *snapshots_path); 74 static void migrate_one_snapshot(int fd, const char *fpath, 75 struct hammer_ioc_snapshot *snapshot); 76 static int strtosecs(char *ptr); 77 static const char *dividing_slash(const char *path); 78 static int check_period(const char *snapshots_path, const char *cmd, int arg1, 79 time_t *savep); 80 static void save_period(const char *snapshots_path, const char *cmd, 81 time_t savet); 82 static int check_softlinks(int fd, int new_config, const char *snapshots_path); 83 static void cleanup_softlinks(int fd, int new_config, 84 const char *snapshots_path, int arg2, char *arg3); 85 static void delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot); 86 static int check_expired(const char *fpath, int arg2); 87 88 static int create_snapshot(const char *path, const char *snapshots_path); 89 static int cleanup_rebalance(const char *path, const char *snapshots_path, 90 int arg1, int arg2); 91 static int cleanup_prune(const char *path, const char *snapshots_path, 92 int arg1, int arg2, int snapshots_disabled); 93 static int cleanup_reblock(const char *path, const char *snapshots_path, 94 int arg1, int arg2); 95 static int cleanup_recopy(const char *path, const char *snapshots_path, 96 int arg1, int arg2); 97 static int cleanup_dedup(const char *path, const char *snapshots_path, 98 int arg1, int arg2); 99 100 static void runcmd(int *resp, const char *ctl, ...) __printflike(2, 3); 101 102 #define WS " \t\r\n" 103 104 struct didpfs *FirstPFS; 105 106 void 107 hammer_cmd_cleanup(char **av, int ac) 108 { 109 char *fstype, *fs, *path; 110 struct statfs *stfsbuf; 111 int mntsize, i; 112 113 tzset(); 114 if (ac == 0) { 115 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT); 116 if (mntsize > 0) { 117 for (i=0; i < mntsize; i++) { 118 /* 119 * We will cleanup in the case fstype is hammer. 120 * If we have null-mounted PFS, we check the 121 * mount source. If it looks like a PFS, we 122 * proceed to cleanup also. 123 */ 124 fstype = stfsbuf[i].f_fstypename; 125 fs = stfsbuf[i].f_mntfromname; 126 if ((strcmp(fstype, "hammer") == 0) || 127 ((strcmp(fstype, "null") == 0) && 128 (strstr(fs, "/@@0x") != NULL || 129 strstr(fs, "/@@-1") != NULL))) { 130 path = stfsbuf[i].f_mntonname; 131 do_cleanup(path); 132 } 133 } 134 } 135 } else { 136 while (ac) { 137 do_cleanup(*av); 138 --ac; 139 ++av; 140 } 141 } 142 } 143 144 static 145 void 146 do_cleanup(const char *path) 147 { 148 struct hammer_ioc_pseudofs_rw pfs; 149 struct hammer_ioc_config config; 150 struct hammer_ioc_version version; 151 union hammer_ioc_mrecord_any mrec_tmp; 152 char *snapshots_path = NULL; 153 char *config_path; 154 struct stat st; 155 char *cmd; 156 char *ptr; 157 int arg1; 158 int arg2; 159 char *arg3; 160 time_t savet; 161 char buf[256]; 162 char *cbase; 163 char *cptr; 164 FILE *fp = NULL; 165 struct didpfs *didpfs; 166 int snapshots_disabled = 0; 167 int prune_warning = 0; 168 int new_config = 0; 169 int snapshots_from_pfs = 0; 170 int fd; 171 int r; 172 int found_rebal = 0; 173 174 bzero(&mrec_tmp, sizeof(mrec_tmp)); 175 clrpfs(&pfs, &mrec_tmp.pfs.pfsd, -1); 176 177 printf("cleanup %-20s -", path); 178 fd = open(path, O_RDONLY); 179 if (fd < 0) { 180 printf(" unable to access directory: %s\n", strerror(errno)); 181 return; 182 } 183 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) { 184 printf(" not a HAMMER filesystem: %s\n", strerror(errno)); 185 close(fd); 186 return; 187 } 188 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) { 189 printf(" unrecognized HAMMER version\n"); 190 close(fd); 191 return; 192 } 193 bzero(&version, sizeof(version)); 194 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) { 195 printf(" HAMMER filesystem but couldn't retrieve version!\n"); 196 close(fd); 197 return; 198 } 199 HammerVersion = version.cur_version; 200 201 bzero(&config, sizeof(config)); 202 if (version.cur_version >= 3) { 203 if (ioctl(fd, HAMMERIOC_GET_CONFIG, &config) == 0 && 204 config.head.error == 0) { 205 new_config = 1; 206 } 207 } 208 209 /* 210 * Make sure we have not already handled this PFS. Several nullfs 211 * mounts might alias the same PFS. 212 */ 213 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) { 214 if (hammer_uuid_compare(&didpfs->uuid, 215 &mrec_tmp.pfs.pfsd.unique_uuid) == 0) { 216 printf(" PFS#%d already handled\n", pfs.pfs_id); 217 close(fd); 218 return; 219 } 220 } 221 didpfs = malloc(sizeof(*didpfs)); 222 didpfs->next = FirstPFS; 223 FirstPFS = didpfs; 224 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid; 225 226 /* 227 * Calculate the old snapshots directory for HAMMER VERSION < 3 228 * 229 * If the directory is explicitly specified in the PFS config 230 * we flag it and will not migrate it later. 231 */ 232 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') { 233 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots); 234 snapshots_from_pfs = 1; 235 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) { 236 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n"); 237 close(fd); 238 return; 239 } else if (hammer_is_pfs_slave(&mrec_tmp.pfs.pfsd)) { 240 if (version.cur_version < 3) { 241 printf(" WARNING: must configure snapshot dir for PFS slave\n"); 242 printf("\tWe suggest <fs>/var/slaves/<name> where " 243 "<fs> is the base HAMMER fs\n"); 244 printf("\tcontaining the slave\n"); 245 close(fd); 246 return; 247 } 248 } else { 249 asprintf(&snapshots_path, 250 "%s%ssnapshots", path, dividing_slash(path)); 251 } 252 253 /* 254 * Check for old-style config file 255 */ 256 if (snapshots_path) { 257 asprintf(&config_path, "%s/config", snapshots_path); 258 fp = fopen(config_path, "r"); 259 } 260 261 /* 262 * Handle upgrades to hammer version 3, move the config 263 * file into meta-data. 264 * 265 * For the old config read the file into the config structure, 266 * we will parse it out of the config structure regardless. 267 */ 268 if (version.cur_version >= 3) { 269 if (fp) { 270 printf("(migrating) "); 271 fflush(stdout); 272 migrate_config(fp, &config); 273 migrate_snapshots(fd, snapshots_path); 274 fclose(fp); 275 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) { 276 printf(" cannot init meta-data config!\n"); 277 close(fd); 278 return; 279 } 280 remove(config_path); 281 } else if (new_config == 0) { 282 config_init(path, &config); 283 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) { 284 printf(" cannot init meta-data config!\n"); 285 close(fd); 286 return; 287 } 288 } 289 new_config = 1; 290 } else { 291 /* 292 * Create missing snapshots directory for HAMMER VERSION < 3 293 */ 294 if (stat(snapshots_path, &st) < 0) { 295 if (mkdir(snapshots_path, 0755) != 0) { 296 free(snapshots_path); 297 printf(" unable to create snapshot dir \"%s\": %s\n", 298 snapshots_path, strerror(errno)); 299 close(fd); 300 return; 301 } 302 } 303 304 /* 305 * Create missing config file for HAMMER VERSION < 3 306 */ 307 if (fp == NULL) { 308 config_init(path, &config); 309 fp = fopen(config_path, "w"); 310 if (fp) { 311 fwrite(config.config.text, 1, 312 strlen(config.config.text), fp); 313 fclose(fp); 314 } 315 } else { 316 migrate_config(fp, &config); 317 fclose(fp); 318 } 319 } 320 321 /* 322 * If snapshots_from_pfs is not set we calculate the new snapshots 323 * directory default (in /var) for HAMMER VERSION >= 3 and migrate 324 * the old snapshots directory over. 325 * 326 * People who have set an explicit snapshots directory will have 327 * to migrate the data manually into /var/hammer, or not bother at 328 * all. People running slaves may wish to migrate it and then 329 * clear the snapshots specification in the PFS config for the 330 * slave. 331 */ 332 if (new_config && snapshots_from_pfs == 0) { 333 char *npath; 334 335 if (path[0] != '/') { 336 printf(" path must start with '/'\n"); 337 return; 338 } 339 if (strcmp(path, "/") == 0) 340 asprintf(&npath, "%s/root", SNAPSHOTS_BASE); 341 else 342 asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1); 343 if (snapshots_path) { 344 if (stat(npath, &st) < 0 && errno == ENOENT) { 345 if (stat(snapshots_path, &st) < 0 && errno == ENOENT) { 346 printf(" HAMMER UPGRADE: Creating snapshots\n" 347 "\tCreating snapshots in %s\n", 348 npath); 349 runcmd(&r, "mkdir -p %s", npath); 350 } else { 351 printf(" HAMMER UPGRADE: Moving snapshots\n" 352 "\tMoving snapshots from %s to %s\n", 353 snapshots_path, npath); 354 runcmd(&r, "mkdir -p %s", npath); 355 runcmd(&r, "cpdup %s %s", snapshots_path, npath); 356 if (r != 0) { 357 printf("Unable to move snapshots directory!\n"); 358 printf("Please fix this critical error.\n"); 359 printf("Aborting cleanup of %s\n", path); 360 close(fd); 361 return; 362 } 363 runcmd(&r, "rm -rf %s", snapshots_path); 364 } 365 } 366 free(snapshots_path); 367 } else if (stat(npath, &st) < 0 && errno == ENOENT) { 368 runcmd(&r, "mkdir -p %s", npath); 369 } 370 snapshots_path = npath; 371 } 372 373 /* 374 * Lock the PFS. fd is the base directory of the mounted PFS. 375 */ 376 if (flock(fd, LOCK_EX|LOCK_NB) == -1) { 377 if (errno == EWOULDBLOCK) 378 printf(" PFS#%d locked by other process\n", pfs.pfs_id); 379 else 380 printf(" can not lock %s: %s\n", config_path, strerror(errno)); 381 close(fd); 382 return; 383 } 384 385 printf(" handle PFS#%d using %s\n", pfs.pfs_id, snapshots_path); 386 387 struct pidfh *pfh = NULL; 388 static char pidfile[PIDFILE_BUFSIZE]; 389 390 snprintf (pidfile, PIDFILE_BUFSIZE, "%s/hammer.cleanup.%d", 391 pidfile_loc, getpid()); 392 pfh = pidfile_open(pidfile, 0644, NULL); 393 if (pfh == NULL) 394 warn ("Unable to open or create %s", pidfile); 395 pidfile_write(pfh); 396 397 /* 398 * Process the config file 399 */ 400 cbase = config.config.text; 401 402 while ((cptr = strchr(cbase, '\n')) != NULL) { 403 bcopy(cbase, buf, cptr - cbase); 404 buf[cptr - cbase] = 0; 405 cbase = cptr + 1; 406 407 cmd = strtok(buf, WS); 408 if (cmd == NULL || cmd[0] == '#') 409 continue; 410 411 arg1 = 0; 412 arg2 = 0; 413 arg3 = NULL; 414 if ((ptr = strtok(NULL, WS)) != NULL) { 415 arg1 = strtosecs(ptr); 416 if ((ptr = strtok(NULL, WS)) != NULL) { 417 arg2 = strtosecs(ptr); 418 arg3 = strtok(NULL, WS); 419 } 420 } 421 422 printf("%20s - ", cmd); 423 fflush(stdout); 424 425 r = 1; 426 if (strcmp(cmd, "snapshots") == 0) { 427 if (arg1 == 0) { 428 if (arg2 && 429 check_softlinks(fd, new_config, 430 snapshots_path)) { 431 printf("only removing old snapshots\n"); 432 prune_warning = 1; 433 cleanup_softlinks(fd, new_config, 434 snapshots_path, 435 arg2, arg3); 436 } else { 437 printf("disabled\n"); 438 snapshots_disabled = 1; 439 } 440 } else 441 if (check_period(snapshots_path, cmd, arg1, &savet)) { 442 printf("run\n"); 443 cleanup_softlinks(fd, new_config, 444 snapshots_path, 445 arg2, arg3); 446 r = create_snapshot(path, snapshots_path); 447 } else { 448 printf("skip\n"); 449 } 450 } else if (arg1 == 0) { 451 /* 452 * The commands following this check can't handle 453 * a period of 0, so call the feature disabled and 454 * ignore the directive. 455 */ 456 printf("disabled\n"); 457 } else if (strcmp(cmd, "prune") == 0) { 458 if (check_period(snapshots_path, cmd, arg1, &savet)) { 459 if (prune_warning) { 460 printf("run - WARNING snapshot " 461 "softlinks present " 462 "but snapshots disabled\n"); 463 } else { 464 printf("run\n"); 465 } 466 r = cleanup_prune(path, snapshots_path, 467 arg1, arg2, snapshots_disabled); 468 } else { 469 printf("skip\n"); 470 } 471 } else if (strcmp(cmd, "rebalance") == 0) { 472 found_rebal = 1; 473 if (check_period(snapshots_path, cmd, arg1, &savet)) { 474 printf("run"); 475 fflush(stdout); 476 if (VerboseOpt) 477 printf("\n"); 478 r = cleanup_rebalance(path, snapshots_path, 479 arg1, arg2); 480 } else { 481 printf("skip\n"); 482 } 483 } else if (strcmp(cmd, "reblock") == 0) { 484 if (check_period(snapshots_path, cmd, arg1, &savet)) { 485 printf("run"); 486 fflush(stdout); 487 if (VerboseOpt) 488 printf("\n"); 489 r = cleanup_reblock(path, snapshots_path, 490 arg1, arg2); 491 } else { 492 printf("skip\n"); 493 } 494 } else if (strcmp(cmd, "recopy") == 0) { 495 if (check_period(snapshots_path, cmd, arg1, &savet)) { 496 printf("run"); 497 fflush(stdout); 498 if (VerboseOpt) 499 printf("\n"); 500 r = cleanup_recopy(path, snapshots_path, 501 arg1, arg2); 502 } else { 503 printf("skip\n"); 504 } 505 } else if (strcmp(cmd, "dedup") == 0) { 506 if (check_period(snapshots_path, cmd, arg1, &savet)) { 507 printf("run"); 508 fflush(stdout); 509 if (VerboseOpt) 510 printf("\n"); 511 r = cleanup_dedup(path, snapshots_path, 512 arg1, arg2); 513 } else { 514 printf("skip\n"); 515 } 516 } else { 517 printf("unknown directive\n"); 518 r = 1; 519 } 520 if (r == 0) 521 save_period(snapshots_path, cmd, savet); 522 } 523 524 /* 525 * Add new rebalance feature if the config doesn't have it. 526 * (old style config only). 527 */ 528 if (new_config == 0 && found_rebal == 0) { 529 if ((fp = fopen(config_path, "r+")) != NULL) { 530 fseek(fp, 0L, 2); 531 fprintf(fp, "rebalance 1d 5m\n"); 532 fclose(fp); 533 } 534 } 535 536 /* 537 * Cleanup, and delay a little 538 */ 539 close(fd); 540 usleep(1000); 541 pidfile_close(pfh); 542 pidfile_remove(pfh); 543 } 544 545 /* 546 * Initialize new config data (new or old style) 547 */ 548 static 549 void 550 config_init(const char *path, struct hammer_ioc_config *config) 551 { 552 const char *snapshots; 553 554 if (strcmp(path, "/tmp") == 0 || 555 strcmp(path, "/var/tmp") == 0 || 556 strcmp(path, "/usr/obj") == 0) { 557 snapshots = "snapshots 0d 0d\n"; 558 } else { 559 snapshots = "snapshots 1d 60d\n"; 560 } 561 bzero(config->config.text, sizeof(config->config.text)); 562 snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s", 563 snapshots, 564 "prune 1d 5m\n" 565 "rebalance 1d 5m\n" 566 "#dedup 1d 5m\n" 567 "reblock 1d 5m\n" 568 "recopy 30d 10m\n"); 569 } 570 571 /* 572 * Migrate configuration data from the old snapshots/config 573 * file to the new meta-data format. 574 */ 575 static 576 void 577 migrate_config(FILE *fp, struct hammer_ioc_config *config) 578 { 579 int n; 580 581 n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp); 582 if (n >= 0) 583 bzero(config->config.text + n, sizeof(config->config.text) - n); 584 } 585 586 /* 587 * Migrate snapshot softlinks in the snapshots directory to the 588 * new meta-data format. The softlinks are left intact, but 589 * this way the pruning code won't lose track of them if you 590 * happen to blow away the snapshots directory. 591 */ 592 static 593 void 594 migrate_snapshots(int fd, const char *snapshots_path) 595 { 596 struct hammer_ioc_snapshot snapshot; 597 struct dirent *den; 598 struct stat st; 599 DIR *dir; 600 char *fpath; 601 602 bzero(&snapshot, sizeof(snapshot)); 603 604 if ((dir = opendir(snapshots_path)) != NULL) { 605 while ((den = readdir(dir)) != NULL) { 606 if (den->d_name[0] == '.') 607 continue; 608 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name); 609 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode)) 610 migrate_one_snapshot(fd, fpath, &snapshot); 611 free(fpath); 612 } 613 closedir(dir); 614 } 615 migrate_one_snapshot(fd, NULL, &snapshot); 616 617 } 618 619 /* 620 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed, 621 * otherwise it is flushed when it fills up. 622 */ 623 static 624 void 625 migrate_one_snapshot(int fd, const char *fpath, 626 struct hammer_ioc_snapshot *snapshot) 627 { 628 if (fpath) { 629 hammer_snapshot_data_t snap; 630 struct tm tm; 631 time_t t; 632 int year; 633 int month; 634 int day = 0; 635 int hour = 0; 636 int minute = 0; 637 int r; 638 char linkbuf[1024]; 639 const char *ptr; 640 hammer_tid_t tid; 641 642 t = (time_t)-1; 643 tid = (hammer_tid_t)(int64_t)-1; 644 645 /* fpath may contain directory components */ 646 if ((ptr = strrchr(fpath, '/')) != NULL) 647 ++ptr; 648 else 649 ptr = fpath; 650 while (*ptr && *ptr != '-' && *ptr != '.') 651 ++ptr; 652 if (*ptr) 653 ++ptr; 654 r = sscanf(ptr, "%4d%2d%2d-%2d%2d", 655 &year, &month, &day, &hour, &minute); 656 657 if (r >= 3) { 658 bzero(&tm, sizeof(tm)); 659 tm.tm_isdst = -1; 660 tm.tm_min = minute; 661 tm.tm_hour = hour; 662 tm.tm_mday = day; 663 tm.tm_mon = month - 1; 664 tm.tm_year = year - 1900; 665 t = mktime(&tm); 666 } 667 bzero(linkbuf, sizeof(linkbuf)); 668 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 && 669 (ptr = strrchr(linkbuf, '@')) != NULL && 670 ptr > linkbuf && ptr[-1] == '@') { 671 tid = strtoull(ptr + 1, NULL, 16); 672 } 673 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) { 674 snap = &snapshot->snaps[snapshot->count]; 675 bzero(snap, sizeof(*snap)); 676 snap->tid = tid; 677 snap->ts = (uint64_t)t * 1000000ULL; 678 snprintf(snap->label, sizeof(snap->label), 679 "migrated"); 680 ++snapshot->count; 681 } else { 682 printf(" non-canonical snapshot softlink: %s->%s\n", 683 fpath, linkbuf); 684 } 685 } 686 687 if ((fpath == NULL && snapshot->count) || 688 snapshot->count == HAMMER_SNAPS_PER_IOCTL) { 689 printf(" (%d snapshots)", snapshot->count); 690 again: 691 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) { 692 printf(" Ioctl to migrate snapshots failed: %s\n", 693 strerror(errno)); 694 } else if (snapshot->head.error == EALREADY) { 695 ++snapshot->index; 696 goto again; 697 } else if (snapshot->head.error) { 698 printf(" Ioctl to migrate snapshots failed: %s\n", 699 strerror(snapshot->head.error)); 700 } 701 printf("index %d\n", snapshot->index); 702 snapshot->index = 0; 703 snapshot->count = 0; 704 snapshot->head.error = 0; 705 } 706 } 707 708 static 709 int 710 strtosecs(char *ptr) 711 { 712 int val; 713 714 val = strtol(ptr, &ptr, 0); 715 switch(*ptr) { 716 case 'd': 717 val *= 24; 718 /* fall through */ 719 case 'h': 720 val *= 60; 721 /* fall through */ 722 case 'm': 723 val *= 60; 724 /* fall through */ 725 case 's': 726 break; 727 default: 728 errx(1, "illegal suffix converting %s", ptr); 729 /* not reached */ 730 break; 731 } 732 return(val); 733 } 734 735 static 736 const char * 737 dividing_slash(const char *path) 738 { 739 int len = strlen(path); 740 if (len && path[len-1] == '/') 741 return(""); 742 else 743 return("/"); 744 } 745 746 /* 747 * Check whether the desired period has elapsed since the last successful 748 * run. The run may take a while and cross a boundary so we remember the 749 * current time_t so we can save it later on. 750 * 751 * Periods in minutes, hours, or days are assumed to have been crossed 752 * if the local time crosses a minute, hour, or day boundary regardless 753 * of how close the last operation actually was. 754 * 755 * If ForceOpt is set always return true. 756 */ 757 static 758 int 759 check_period(const char *snapshots_path, const char *cmd, int arg1, 760 time_t *savep) 761 { 762 char *check_path; 763 struct tm tp1; 764 struct tm tp2; 765 FILE *fp; 766 time_t baset, lastt; 767 char buf[256]; 768 769 time(savep); 770 localtime_r(savep, &tp1); 771 772 /* 773 * Force run if -F 774 */ 775 if (ForceOpt) 776 return(1); 777 778 /* 779 * Retrieve the start time of the last successful operation. 780 */ 781 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd); 782 fp = fopen(check_path, "r"); 783 free(check_path); 784 if (fp == NULL) 785 return(1); 786 if (fgets(buf, sizeof(buf), fp) == NULL) { 787 fclose(fp); 788 return(1); 789 } 790 fclose(fp); 791 792 lastt = strtol(buf, NULL, 0); 793 localtime_r(&lastt, &tp2); 794 795 /* 796 * Normalize the times. e.g. if asked to do something on a 1-day 797 * interval the operation will be performed as soon as the day 798 * turns over relative to the previous operation, even if the previous 799 * operation ran a few seconds ago just before midnight. 800 */ 801 if (arg1 % 60 == 0) { 802 tp1.tm_sec = 0; 803 tp2.tm_sec = 0; 804 } 805 if (arg1 % (60 * 60) == 0) { 806 tp1.tm_min = 0; 807 tp2.tm_min = 0; 808 } 809 if (arg1 % (24 * 60 * 60) == 0) { 810 tp1.tm_hour = 0; 811 tp2.tm_hour = 0; 812 } 813 814 baset = mktime(&tp1); 815 lastt = mktime(&tp2); 816 817 #if 0 818 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1); 819 #endif 820 821 if ((int)(baset - lastt) >= arg1) 822 return(1); 823 return(0); 824 } 825 826 /* 827 * Store the start time of the last successful operation. 828 */ 829 static 830 void 831 save_period(const char *snapshots_path, const char *cmd, 832 time_t savet) 833 { 834 char *ocheck_path; 835 char *ncheck_path; 836 FILE *fp; 837 838 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd); 839 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd); 840 fp = fopen(ncheck_path, "w"); 841 if (fp) { 842 fprintf(fp, "0x%08llx\n", (long long)savet); 843 if (fclose(fp) == 0) 844 rename(ncheck_path, ocheck_path); 845 remove(ncheck_path); 846 } else { 847 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n", 848 ncheck_path, strerror(errno)); 849 } 850 } 851 852 /* 853 * Simply count the number of softlinks in the snapshots dir 854 */ 855 static 856 int 857 check_softlinks(int fd, int new_config, const char *snapshots_path) 858 { 859 struct dirent *den; 860 struct stat st; 861 DIR *dir; 862 char *fpath; 863 int res = 0; 864 865 /* 866 * Old-style softlink-based snapshots 867 */ 868 if ((dir = opendir(snapshots_path)) != NULL) { 869 while ((den = readdir(dir)) != NULL) { 870 if (den->d_name[0] == '.') 871 continue; 872 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name); 873 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode)) 874 ++res; 875 free(fpath); 876 } 877 closedir(dir); 878 } 879 880 /* 881 * New-style snapshots are stored as filesystem meta-data, 882 * count those too. 883 */ 884 if (new_config) { 885 struct hammer_ioc_snapshot snapshot; 886 887 bzero(&snapshot, sizeof(snapshot)); 888 do { 889 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) { 890 err(2, "hammer cleanup: check_softlink " 891 "snapshot error"); 892 /* not reached */ 893 } 894 res += snapshot.count; 895 } while (snapshot.head.error == 0 && snapshot.count); 896 } 897 return (res); 898 } 899 900 /* 901 * Clean up expired softlinks in the snapshots dir 902 */ 903 static 904 void 905 cleanup_softlinks(int fd, int new_config, 906 const char *snapshots_path, int arg2, char *arg3) 907 { 908 struct dirent *den; 909 struct stat st; 910 DIR *dir; 911 char *fpath; 912 int anylink = 0; 913 914 if (arg3 != NULL && strstr(arg3, "any") != NULL) 915 anylink = 1; 916 917 if ((dir = opendir(snapshots_path)) != NULL) { 918 while ((den = readdir(dir)) != NULL) { 919 if (den->d_name[0] == '.') 920 continue; 921 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name); 922 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) && 923 (anylink || strncmp(den->d_name, "snap-", 5) == 0)) { 924 if (check_expired(den->d_name, arg2)) { 925 if (VerboseOpt) { 926 printf(" expire %s\n", 927 fpath); 928 } 929 remove(fpath); 930 } 931 } 932 free(fpath); 933 } 934 closedir(dir); 935 } 936 937 /* 938 * New-style snapshots are stored as filesystem meta-data, 939 * count those too. 940 */ 941 if (new_config) { 942 struct hammer_ioc_snapshot snapshot; 943 struct hammer_ioc_snapshot dsnapshot; 944 hammer_snapshot_data_t snap; 945 struct tm *tp; 946 time_t t; 947 time_t dt; 948 char snapts[32]; 949 uint32_t i; 950 951 bzero(&snapshot, sizeof(snapshot)); 952 bzero(&dsnapshot, sizeof(dsnapshot)); 953 do { 954 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) { 955 err(2, "hammer cleanup: check_softlink " 956 "snapshot error"); 957 /* not reached */ 958 } 959 for (i = 0; i < snapshot.count; ++i) { 960 snap = &snapshot.snaps[i]; 961 t = snap->ts / 1000000ULL; 962 dt = time(NULL) - t; 963 if ((int)dt > arg2 || snap->tid == 0) { 964 dsnapshot.snaps[dsnapshot.count++] = 965 *snap; 966 } 967 if ((int)dt > arg2 && VerboseOpt) { 968 tp = localtime(&t); 969 strftime(snapts, sizeof(snapts), 970 "%Y-%m-%d %H:%M:%S %Z", tp); 971 printf(" expire 0x%016jx %s %s\n", 972 (uintmax_t)snap->tid, 973 snapts, 974 snap->label); 975 } 976 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL) 977 delete_snapshots(fd, &dsnapshot); 978 } 979 } while (snapshot.head.error == 0 && snapshot.count); 980 981 if (dsnapshot.count) 982 delete_snapshots(fd, &dsnapshot); 983 } 984 } 985 986 static 987 void 988 delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot) 989 { 990 for (;;) { 991 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) { 992 printf(" Ioctl to delete snapshots failed: %s\n", 993 strerror(errno)); 994 break; 995 } 996 if (dsnapshot->head.error) { 997 printf(" Ioctl to delete snapshots failed at " 998 "snap=%016jx: %s\n", 999 dsnapshot->snaps[dsnapshot->index].tid, 1000 strerror(dsnapshot->head.error)); 1001 if (++dsnapshot->index < dsnapshot->count) 1002 continue; 1003 } 1004 break; 1005 } 1006 dsnapshot->index = 0; 1007 dsnapshot->count = 0; 1008 dsnapshot->head.error = 0; 1009 } 1010 1011 /* 1012 * Take a softlink path in the form snap-yyyymmdd-hhmm and the 1013 * expiration in seconds (arg2) and return non-zero if the softlink 1014 * has expired. 1015 */ 1016 static 1017 int 1018 check_expired(const char *fpath, int arg2) 1019 { 1020 struct tm tm; 1021 time_t t; 1022 int year; 1023 int month; 1024 int day = 0; 1025 int hour = 0; 1026 int minute = 0; 1027 int r; 1028 1029 while (*fpath && *fpath != '-' && *fpath != '.') 1030 ++fpath; 1031 if (*fpath) 1032 ++fpath; 1033 1034 r = sscanf(fpath, "%4d%2d%2d-%2d%2d", 1035 &year, &month, &day, &hour, &minute); 1036 1037 if (r >= 3) { 1038 bzero(&tm, sizeof(tm)); 1039 tm.tm_isdst = -1; 1040 tm.tm_min = minute; 1041 tm.tm_hour = hour; 1042 tm.tm_mday = day; 1043 tm.tm_mon = month - 1; 1044 tm.tm_year = year - 1900; 1045 t = mktime(&tm); 1046 if (t == (time_t)-1) 1047 return(0); 1048 t = time(NULL) - t; 1049 if ((int)t > arg2) 1050 return(1); 1051 } 1052 return(0); 1053 } 1054 1055 /* 1056 * Issue a snapshot. 1057 */ 1058 static 1059 int 1060 create_snapshot(const char *path, const char *snapshots_path) 1061 { 1062 int r; 1063 1064 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path); 1065 return(r); 1066 } 1067 1068 static 1069 int 1070 cleanup_prune(const char *path, const char *snapshots_path, 1071 int arg1 __unused, int arg2, int snapshots_disabled) 1072 { 1073 const char *path_or_snapshots_path; 1074 1075 /* 1076 * If the snapshots_path (e.g. /var/hammer/...) has no snapshots 1077 * in it then prune will get confused and prune the filesystem 1078 * containing the snapshots_path instead of the requested 1079 * filesystem. De-confuse prune. We need a better way. 1080 */ 1081 if (hammer_softprune_testdir(snapshots_path)) 1082 path_or_snapshots_path = snapshots_path; 1083 else 1084 path_or_snapshots_path = path; 1085 1086 /* 1087 * If snapshots have been disabled run prune-everything instead 1088 * of prune. 1089 */ 1090 if (snapshots_disabled && arg2) { 1091 runcmd(NULL, 1092 "hammer -c %s/.prune.cycle -t %d prune-everything %s", 1093 snapshots_path, arg2, path); 1094 } else if (snapshots_disabled) { 1095 runcmd(NULL, "hammer prune-everything %s", path); 1096 } else if (arg2) { 1097 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s", 1098 snapshots_path, arg2, path_or_snapshots_path); 1099 } else { 1100 runcmd(NULL, "hammer prune %s", path_or_snapshots_path); 1101 } 1102 return(0); 1103 } 1104 1105 static 1106 int 1107 cleanup_rebalance(const char *path, const char *snapshots_path, 1108 int arg1 __unused, int arg2) 1109 { 1110 if (VerboseOpt == 0) { 1111 printf("."); 1112 fflush(stdout); 1113 } 1114 1115 runcmd(NULL, 1116 "hammer -c %s/.rebalance.cycle -t %d rebalance %s", 1117 snapshots_path, arg2, path); 1118 if (VerboseOpt == 0) { 1119 printf("."); 1120 fflush(stdout); 1121 } 1122 if (VerboseOpt == 0) 1123 printf("\n"); 1124 return(0); 1125 } 1126 1127 static 1128 int 1129 cleanup_reblock(const char *path, const char *snapshots_path, 1130 int arg1 __unused, int arg2) 1131 { 1132 if (VerboseOpt == 0) { 1133 printf("."); 1134 fflush(stdout); 1135 } 1136 1137 /* 1138 * When reblocking the B-Tree always reblock everything in normal 1139 * mode. 1140 */ 1141 runcmd(NULL, 1142 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s", 1143 snapshots_path, arg2, path); 1144 if (VerboseOpt == 0) { 1145 printf("."); 1146 fflush(stdout); 1147 } 1148 1149 /* 1150 * When reblocking the inodes always reblock everything in normal 1151 * mode. 1152 */ 1153 runcmd(NULL, 1154 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s", 1155 snapshots_path, arg2, path); 1156 if (VerboseOpt == 0) { 1157 printf("."); 1158 fflush(stdout); 1159 } 1160 1161 /* 1162 * When reblocking the directories always reblock everything in normal 1163 * mode. 1164 */ 1165 runcmd(NULL, 1166 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s", 1167 snapshots_path, arg2, path); 1168 if (VerboseOpt == 0) { 1169 printf("."); 1170 fflush(stdout); 1171 } 1172 1173 /* 1174 * Do not reblock all the data in normal mode. 1175 */ 1176 runcmd(NULL, 1177 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95", 1178 snapshots_path, arg2, path); 1179 if (VerboseOpt == 0) 1180 printf("\n"); 1181 return(0); 1182 } 1183 1184 static 1185 int 1186 cleanup_recopy(const char *path, const char *snapshots_path, 1187 int arg1 __unused, int arg2) 1188 { 1189 if (VerboseOpt == 0) { 1190 printf("."); 1191 fflush(stdout); 1192 } 1193 runcmd(NULL, 1194 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s", 1195 snapshots_path, arg2, path); 1196 if (VerboseOpt == 0) { 1197 printf("."); 1198 fflush(stdout); 1199 } 1200 runcmd(NULL, 1201 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s", 1202 snapshots_path, arg2, path); 1203 if (VerboseOpt == 0) { 1204 printf("."); 1205 fflush(stdout); 1206 } 1207 runcmd(NULL, 1208 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s", 1209 snapshots_path, arg2, path); 1210 if (VerboseOpt == 0) { 1211 printf("."); 1212 fflush(stdout); 1213 } 1214 runcmd(NULL, 1215 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s", 1216 snapshots_path, arg2, path); 1217 if (VerboseOpt == 0) 1218 printf("\n"); 1219 return(0); 1220 } 1221 1222 static 1223 int 1224 cleanup_dedup(const char *path, const char *snapshots_path __unused, 1225 int arg1 __unused, int arg2) 1226 { 1227 if (VerboseOpt == 0) { 1228 printf("."); 1229 fflush(stdout); 1230 } 1231 1232 runcmd(NULL, "hammer -t %d dedup %s", arg2, path); 1233 if (VerboseOpt == 0) { 1234 printf("."); 1235 fflush(stdout); 1236 } 1237 if (VerboseOpt == 0) 1238 printf("\n"); 1239 return(0); 1240 } 1241 1242 static 1243 void 1244 runcmd(int *resp, const char *ctl, ...) 1245 { 1246 va_list va; 1247 char *cmd; 1248 char *arg; 1249 char **av; 1250 int n; 1251 int nmax; 1252 int res; 1253 pid_t pid; 1254 1255 /* 1256 * Generate the command 1257 */ 1258 va_start(va, ctl); 1259 vasprintf(&cmd, ctl, va); 1260 va_end(va); 1261 if (VerboseOpt) 1262 printf(" %s\n", cmd); 1263 1264 /* 1265 * Break us down into arguments. We do not just use system() here 1266 * because it blocks SIGINT and friends. 1267 */ 1268 n = 0; 1269 nmax = 16; 1270 av = malloc(sizeof(char *) * nmax); 1271 1272 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) { 1273 if (n == nmax - 1) { 1274 nmax += 16; 1275 av = realloc(av, sizeof(char *) * nmax); 1276 } 1277 av[n++] = arg; 1278 } 1279 av[n++] = NULL; 1280 1281 /* 1282 * Run the command. 1283 */ 1284 RunningIoctl = 1; 1285 if ((pid = fork()) == 0) { 1286 if (VerboseOpt < 2) { 1287 int fd = open("/dev/null", O_RDWR); 1288 dup2(fd, 1); 1289 close(fd); 1290 } 1291 execvp(av[0], av); 1292 _exit(127); 1293 } else if (pid < 0) { 1294 res = 127; 1295 } else { 1296 int status; 1297 1298 while (waitpid(pid, &status, 0) != pid) 1299 ; 1300 res = WEXITSTATUS(status); 1301 } 1302 RunningIoctl = 0; 1303 if (DidInterrupt) 1304 _exit(1); 1305 1306 free(cmd); 1307 free(av); 1308 if (resp) 1309 *resp = res; 1310 } 1311