2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sbin/hammer/cmd_cleanup.c,v 1.6 2008/10/07 22:28:41 thomas Exp $
37 * Clean up specific HAMMER filesystems or all HAMMER filesystems.
39 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
42 * Each HAMMER filesystem may contain a configuration file. If no
43 * configuration file is present one will be created with the following
46 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj)
52 * All hammer commands create and maintain cycle files in the snapshots
55 * For HAMMER version 2- the configuration file is a named 'config' in
56 * the snapshots directory, which defaults to <pfs>/snapshots.
57 * For HAMMER version 3+ the configuration file is saved in filesystem
58 * meta-data. The snapshots directory defaults to /var/hammer/<pfs>
59 * (/var/hammer/root for root mount).
69 static void do_cleanup(const char *path);
70 static void config_init(const char *path, struct hammer_ioc_config *config);
71 static void migrate_config(FILE *fp, struct hammer_ioc_config *config);
72 static void migrate_snapshots(int fd, const char *snapshots_path);
73 static void migrate_one_snapshot(int fd, const char *fpath,
74 struct hammer_ioc_snapshot *snapshot);
75 static int strtosecs(char *ptr);
76 static const char *dividing_slash(const char *path);
77 static int check_period(const char *snapshots_path, const char *cmd, int arg1,
79 static void save_period(const char *snapshots_path, const char *cmd,
81 static int check_softlinks(int fd, int new_config, const char *snapshots_path);
82 static void cleanup_softlinks(int fd, int new_config,
83 const char *snapshots_path, int arg2, char *arg3);
84 static int check_expired(const char *fpath, int arg2);
86 static int create_snapshot(const char *path, const char *snapshots_path);
87 static int cleanup_rebalance(const char *path, const char *snapshots_path,
89 static int cleanup_prune(const char *path, const char *snapshots_path,
90 int arg1, int arg2, int snapshots_disabled);
91 static int cleanup_reblock(const char *path, const char *snapshots_path,
93 static int cleanup_recopy(const char *path, const char *snapshots_path,
96 static void runcmd(int *resp, const char *ctl, ...);
99 * WARNING: Do not make the SNAPSHOTS_BASE "/var/snapshots" because
100 * it will interfere with the older HAMMER VERS < 3 snapshots directory
103 #define SNAPSHOTS_BASE "/var/hammer" /* HAMMER VERS >= 3 */
106 struct didpfs *FirstPFS;
109 hammer_cmd_cleanup(char **av, int ac)
111 char *fstype, *fs, *path;
112 struct statfs *stfsbuf;
117 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
119 for (i=0; i < mntsize; i++) {
121 * We will cleanup in the case fstype is hammer.
122 * If we have null-mounted PFS, we check the
123 * mount source. If it looks like a PFS, we
124 * proceed to cleanup also.
126 fstype = stfsbuf[i].f_fstypename;
127 fs = stfsbuf[i].f_mntfromname;
128 if ((strcmp(fstype, "hammer") == 0) ||
129 ((strcmp(fstype, "null") == 0) &&
130 (strstr(fs, "/@@0x") != NULL ||
131 strstr(fs, "/@@-1") != NULL))) {
132 path = stfsbuf[i].f_mntonname;
149 do_cleanup(const char *path)
151 struct hammer_ioc_pseudofs_rw pfs;
152 struct hammer_ioc_config config;
153 struct hammer_ioc_version version;
154 union hammer_ioc_mrecord_any mrec_tmp;
155 char *snapshots_path = NULL;
168 struct didpfs *didpfs;
169 int snapshots_disabled = 0;
170 int prune_warning = 0;
172 int snapshots_from_pfs = 0;
177 bzero(&pfs, sizeof(pfs));
178 bzero(&mrec_tmp, sizeof(mrec_tmp));
179 pfs.ondisk = &mrec_tmp.pfs.pfsd;
180 pfs.bytes = sizeof(mrec_tmp.pfs.pfsd);
183 printf("cleanup %-20s -", path);
184 fd = open(path, O_RDONLY);
186 printf(" unable to access directory: %s\n", strerror(errno));
189 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
190 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
194 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
195 printf(" unrecognized HAMMER version\n");
199 bzero(&version, sizeof(version));
200 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
201 printf(" HAMMER filesystem but couldn't retrieve version!\n");
206 bzero(&config, sizeof(config));
207 if (version.cur_version >= 3) {
208 if (ioctl(fd, HAMMERIOC_GET_CONFIG, &config) == 0 &&
209 config.head.error == 0) {
215 * Make sure we have not already handled this PFS. Several nullfs
216 * mounts might alias the same PFS.
218 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
219 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
220 printf(" PFS #%d already handled\n", pfs.pfs_id);
225 didpfs = malloc(sizeof(*didpfs));
226 didpfs->next = FirstPFS;
228 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
231 * Calculate the old snapshots directory for HAMMER VERSION < 3
233 * If the directory is explicitly specified in the PFS config
234 * we flag it and will not migrate it later.
236 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
237 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
238 snapshots_from_pfs = 1;
239 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
240 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
243 } else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
244 if (version.cur_version < 3) {
245 printf(" WARNING: must configure snapshot dir for PFS slave\n");
246 printf("\tWe suggest <fs>/var/slaves/<name> where "
247 "<fs> is the base HAMMER fs\n");
248 printf("\tcontaining the slave\n");
253 asprintf(&snapshots_path,
254 "%s%ssnapshots", path, dividing_slash(path));
258 * Check for old-style config file
260 if (snapshots_path) {
261 asprintf(&config_path, "%s/config", snapshots_path);
262 fp = fopen(config_path, "r");
266 * Handle upgrades to hammer version 3, move the config
267 * file into meta-data.
269 * For the old config read the file into the config structure,
270 * we will parse it out of the config structure regardless.
272 if (version.cur_version >= 3) {
274 printf("(migrating) ");
276 migrate_config(fp, &config);
277 migrate_snapshots(fd, snapshots_path);
279 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
280 printf(" cannot init meta-data config!\n");
285 } else if (new_config == 0) {
286 config_init(path, &config);
287 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
288 printf(" cannot init meta-data config!\n");
296 * Create missing snapshots directory for HAMMER VERSION < 3
298 if (stat(snapshots_path, &st) < 0) {
299 if (mkdir(snapshots_path, 0755) != 0) {
300 free(snapshots_path);
301 printf(" unable to create snapshot dir \"%s\": %s\n",
302 snapshots_path, strerror(errno));
309 * Create missing config file for HAMMER VERSION < 3
312 config_init(path, &config);
313 fp = fopen(config_path, "w");
315 fwrite(config.config.text, 1,
316 strlen(config.config.text), fp);
320 migrate_config(fp, &config);
326 * If snapshots_from_pfs is not set we calculate the new snapshots
327 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
328 * the old snapshots directory over.
330 * People who have set an explicit snapshots directory will have
331 * to migrate the data manually into /var/hammer, or not bother at
332 * all. People running slaves may wish to migrate it and then
333 * clear the snapshots specification in the PFS config for the
336 if (new_config && snapshots_from_pfs == 0) {
339 assert(path[0] == '/');
340 if (strcmp(path, "/") == 0)
341 asprintf(&npath, "%s/root", SNAPSHOTS_BASE);
343 asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1);
344 if (snapshots_path) {
345 if (stat(npath, &st) < 0 && errno == ENOENT) {
346 if (stat(snapshots_path, &st) < 0 && errno == ENOENT) {
347 printf(" HAMMER UPGRADE: Creating snapshots\n"
348 "\tCreating snapshots in %s\n",
350 runcmd(&r, "mkdir -p %s", npath);
352 printf(" HAMMER UPGRADE: Moving snapshots\n"
353 "\tMoving snapshots from %s to %s\n",
354 snapshots_path, npath);
355 runcmd(&r, "mkdir -p %s", npath);
356 runcmd(&r, "cpdup %s %s", snapshots_path, npath);
358 printf("Unable to move snapshots directory!\n");
359 printf("Please fix this critical error.\n");
360 printf("Aborting cleanup of %s\n", path);
364 runcmd(&r, "rm -rf %s", snapshots_path);
367 free(snapshots_path);
368 } else if (stat(npath, &st) < 0 && errno == ENOENT) {
369 runcmd(&r, "mkdir -p %s", npath);
371 snapshots_path = npath;
375 * Lock the PFS. fd is the base directory of the mounted PFS.
377 if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
378 if (errno == EWOULDBLOCK)
379 printf(" PFS #%d locked by other process\n", pfs.pfs_id);
381 printf(" can not lock %s: %s\n", config_path, strerror(errno));
386 printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
389 * Process the config file
391 cbase = config.config.text;
393 while ((cptr = strchr(cbase, '\n')) != NULL) {
394 bcopy(cbase, buf, cptr - cbase);
395 buf[cptr - cbase] = 0;
398 cmd = strtok(buf, WS);
402 if ((ptr = strtok(NULL, WS)) != NULL) {
403 arg1 = strtosecs(ptr);
404 if ((ptr = strtok(NULL, WS)) != NULL) {
405 arg2 = strtosecs(ptr);
406 arg3 = strtok(NULL, WS);
410 printf("%20s - ", cmd);
414 if (strcmp(cmd, "snapshots") == 0) {
417 check_softlinks(fd, new_config,
419 printf("only removing old snapshots\n");
421 cleanup_softlinks(fd, new_config,
425 printf("disabled\n");
426 snapshots_disabled = 1;
429 if (check_period(snapshots_path, cmd, arg1, &savet)) {
431 cleanup_softlinks(fd, new_config,
434 r = create_snapshot(path, snapshots_path);
438 } else if (arg1 == 0) {
440 * The commands following this check can't handle
441 * a period of 0, so call the feature disabled and
442 * ignore the directive.
444 printf("disabled\n");
445 } else if (strcmp(cmd, "prune") == 0) {
446 if (check_period(snapshots_path, cmd, arg1, &savet)) {
448 printf("run - WARNING snapshot "
450 "but snapshots disabled\n");
454 r = cleanup_prune(path, snapshots_path,
455 arg1, arg2, snapshots_disabled);
459 } else if (strcmp(cmd, "rebalance") == 0) {
461 if (check_period(snapshots_path, cmd, arg1, &savet)) {
466 r = cleanup_rebalance(path, snapshots_path,
471 } else if (strcmp(cmd, "reblock") == 0) {
472 if (check_period(snapshots_path, cmd, arg1, &savet)) {
477 r = cleanup_reblock(path, snapshots_path,
482 } else if (strcmp(cmd, "recopy") == 0) {
483 if (check_period(snapshots_path, cmd, arg1, &savet)) {
488 r = cleanup_recopy(path, snapshots_path,
494 printf("unknown directive\n");
498 save_period(snapshots_path, cmd, savet);
502 * Add new rebalance feature if the config doesn't have it.
503 * (old style config only).
505 if (new_config == 0 && found_rebal == 0) {
506 if ((fp = fopen(config_path, "r+")) != NULL) {
508 fprintf(fp, "rebalance 1d 5m\n");
514 * Cleanup, and delay a little
521 * Initialize new config data (new or old style)
524 config_init(const char *path, struct hammer_ioc_config *config)
526 const char *snapshots;
528 if (strcmp(path, "/tmp") == 0 ||
529 strcmp(path, "/var/tmp") == 0 ||
530 strcmp(path, "/usr/obj") == 0) {
531 snapshots = "snapshots 0d 0d\n";
533 snapshots = "snapshots 1d 60d\n";
535 bzero(config->config.text, sizeof(config->config.text));
536 snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s",
545 * Migrate configuration data from the old snapshots/config
546 * file to the new meta-data format.
549 migrate_config(FILE *fp, struct hammer_ioc_config *config)
553 n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp);
555 bzero(config->config.text + n, sizeof(config->config.text) - n);
559 * Migrate snapshot softlinks in the snapshots directory to the
560 * new meta-data format. The softlinks are left intact, but
561 * this way the pruning code won't lose track of them if you
562 * happen to blow away the snapshots directory.
565 migrate_snapshots(int fd, const char *snapshots_path)
567 struct hammer_ioc_snapshot snapshot;
573 bzero(&snapshot, sizeof(snapshot));
575 if ((dir = opendir(snapshots_path)) != NULL) {
576 while ((den = readdir(dir)) != NULL) {
577 if (den->d_name[0] == '.')
579 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
580 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode)) {
581 migrate_one_snapshot(fd, fpath, &snapshot);
587 migrate_one_snapshot(fd, NULL, &snapshot);
592 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
593 * otherwise it is flushed when it fills up.
596 migrate_one_snapshot(int fd, const char *fpath,
597 struct hammer_ioc_snapshot *snapshot)
600 struct hammer_snapshot_data *snap;
614 tid = (hammer_tid_t)(int64_t)-1;
617 while (*ptr && *ptr != '-' && *ptr != '.')
621 r = sscanf(ptr, "%4d%2d%2d-%2d%2d",
622 &year, &month, &day, &hour, &minute);
625 bzero(&tm, sizeof(tm));
630 tm.tm_mon = month - 1;
631 tm.tm_year = year - 1900;
634 bzero(linkbuf, sizeof(linkbuf));
635 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 &&
636 (ptr = strrchr(linkbuf, '@')) != NULL &&
637 ptr > linkbuf && ptr[-1] == '@') {
638 tid = strtoull(ptr + 1, NULL, 16);
640 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) {
641 snap = &snapshot->snaps[snapshot->count];
642 bzero(snap, sizeof(*snap));
644 snap->ts = (u_int64_t)t * 1000000ULL;
645 snprintf(snap->label, sizeof(snap->label),
651 if ((fpath == NULL && snapshot->count) ||
652 snapshot->count == HAMMER_SNAPS_PER_IOCTL) {
653 printf(" (%d snapshots)", snapshot->count);
655 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) {
656 printf(" Ioctl to migrate snapshots failed: %s\n",
658 } else if (snapshot->head.error == EALREADY) {
661 } else if (snapshot->head.error) {
662 printf(" Ioctl to migrate snapshots failed: %s\n",
663 strerror(snapshot->head.error));
665 printf("index %d\n", snapshot->index);
668 snapshot->head.error = 0;
678 val = strtol(ptr, &ptr, 0);
692 errx(1, "illegal suffix converting %s\n", ptr);
699 dividing_slash(const char *path)
701 int len = strlen(path);
702 if (len && path[len-1] == '/')
709 * Check whether the desired period has elapsed since the last successful
710 * run. The run may take a while and cross a boundary so we remember the
711 * current time_t so we can save it later on.
713 * Periods in minutes, hours, or days are assumed to have been crossed
714 * if the local time crosses a minute, hour, or day boundary regardless
715 * of how close the last operation actually was.
718 check_period(const char *snapshots_path, const char *cmd, int arg1,
729 localtime_r(savep, &tp1);
732 * Retrieve the start time of the last successful operation.
734 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
735 fp = fopen(check_path, "r");
739 if (fgets(buf, sizeof(buf), fp) == NULL) {
745 lastt = strtol(buf, NULL, 0);
746 localtime_r(&lastt, &tp2);
749 * Normalize the times. e.g. if asked to do something on a 1-day
750 * interval the operation will be performed as soon as the day
751 * turns over relative to the previous operation, even if the previous
752 * operation ran a few seconds ago just before midnight.
754 if (arg1 % 60 == 0) {
758 if (arg1 % (60 * 60) == 0) {
762 if (arg1 % (24 * 60 * 60) == 0) {
767 baset = mktime(&tp1);
768 lastt = mktime(&tp2);
771 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
774 if ((int)(baset - lastt) >= arg1)
780 * Store the start time of the last successful operation.
783 save_period(const char *snapshots_path, const char *cmd,
790 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
791 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
792 fp = fopen(ncheck_path, "w");
794 fprintf(fp, "0x%08llx\n", (long long)savet);
796 rename(ncheck_path, ocheck_path);
799 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
800 ncheck_path, strerror(errno));
805 * Simply count the number of softlinks in the snapshots dir
808 check_softlinks(int fd, int new_config, const char *snapshots_path)
817 * Old-style softlink-based snapshots
819 if ((dir = opendir(snapshots_path)) != NULL) {
820 while ((den = readdir(dir)) != NULL) {
821 if (den->d_name[0] == '.')
823 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
824 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
832 * New-style snapshots are stored as filesystem meta-data,
836 struct hammer_ioc_snapshot snapshot;
838 bzero(&snapshot, sizeof(snapshot));
840 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
841 err(2, "hammer cleanup: check_softlink "
845 res += snapshot.count;
846 } while (snapshot.head.error == 0 && snapshot.count);
852 * Clean up expired softlinks in the snapshots dir
855 cleanup_softlinks(int fd, int new_config,
856 const char *snapshots_path, int arg2, char *arg3)
864 if (arg3 != NULL && strstr(arg3, "any") != NULL)
867 if ((dir = opendir(snapshots_path)) != NULL) {
868 while ((den = readdir(dir)) != NULL) {
869 if (den->d_name[0] == '.')
871 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
872 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
873 (anylink || strncmp(den->d_name, "snap-", 5) == 0)
875 if (check_expired(den->d_name, arg2)) {
877 printf(" expire %s\n",
889 * New-style snapshots are stored as filesystem meta-data,
893 struct hammer_ioc_snapshot snapshot;
894 struct hammer_ioc_snapshot dsnapshot;
895 struct hammer_snapshot_data *snap;
901 bzero(&snapshot, sizeof(snapshot));
902 bzero(&dsnapshot, sizeof(dsnapshot));
904 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
905 err(2, "hammer cleanup: check_softlink "
909 for (i = 0; i < snapshot.count; ++i) {
910 snap = &snapshot.snaps[i];
911 t = time(NULL) - snap->ts / 1000000ULL;
912 if ((int)t > arg2 && snap->tid != 0) {
913 dsnapshot.snaps[dsnapshot.count++] =
916 if ((int)t > arg2 && VerboseOpt) {
918 strftime(snapts, sizeof(snapts),
919 "%Y-%m-%d %H:%M:%S %Z", tp);
920 printf(" expire 0x%016jx %s %s\n",
921 (uintmax_t)snap->tid,
925 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL) {
926 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
927 printf(" Ioctl to delete snapshots failed: %s index %d\n", strerror(errno), dsnapshot.index);
928 } else if (dsnapshot.head.error) {
929 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
934 dsnapshot.head.error = 0;
937 } while (snapshot.head.error == 0 && snapshot.count);
939 if (dsnapshot.count) {
940 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
941 printf(" Ioctl to delete snapshots failed: %s\n", strerror(errno));
942 } else if (dsnapshot.head.error) {
943 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
947 dsnapshot.head.error = 0;
953 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
954 * expiration in seconds (arg2) and return non-zero if the softlink
958 check_expired(const char *fpath, int arg2)
969 while (*fpath && *fpath != '-' && *fpath != '.')
974 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
975 &year, &month, &day, &hour, &minute);
978 bzero(&tm, sizeof(tm));
983 tm.tm_mon = month - 1;
984 tm.tm_year = year - 1900;
999 create_snapshot(const char *path, const char *snapshots_path)
1003 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
1008 cleanup_prune(const char *path __unused, const char *snapshots_path,
1009 int arg1 __unused, int arg2, int snapshots_disabled)
1012 * If snapshots have been disabled run prune-everything instead
1015 if (snapshots_disabled && arg2) {
1016 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1017 snapshots_path, arg2, path);
1018 } else if (snapshots_disabled) {
1019 runcmd(NULL, "hammer prune-everything %s", path);
1021 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
1022 snapshots_path, arg2, snapshots_path);
1024 runcmd(NULL, "hammer prune %s", snapshots_path);
1030 cleanup_rebalance(const char *path, const char *snapshots_path,
1031 int arg1 __unused, int arg2)
1033 if (VerboseOpt == 0) {
1039 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1040 snapshots_path, arg2, path);
1041 if (VerboseOpt == 0) {
1045 if (VerboseOpt == 0)
1051 cleanup_reblock(const char *path, const char *snapshots_path,
1052 int arg1 __unused, int arg2)
1054 if (VerboseOpt == 0) {
1060 * When reblocking the B-Tree always reblock everything in normal
1064 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1065 snapshots_path, arg2, path);
1066 if (VerboseOpt == 0) {
1072 * When reblocking the inodes always reblock everything in normal
1076 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1077 snapshots_path, arg2, path);
1078 if (VerboseOpt == 0) {
1084 * When reblocking the directories always reblock everything in normal
1088 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1089 snapshots_path, arg2, path);
1090 if (VerboseOpt == 0) {
1096 * Do not reblock all the data in normal mode.
1099 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1100 snapshots_path, arg2, path);
1101 if (VerboseOpt == 0)
1107 cleanup_recopy(const char *path, const char *snapshots_path,
1108 int arg1 __unused, int arg2)
1110 if (VerboseOpt == 0) {
1115 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1116 snapshots_path, arg2, path);
1117 if (VerboseOpt == 0) {
1122 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1123 snapshots_path, arg2, path);
1124 if (VerboseOpt == 0) {
1129 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1130 snapshots_path, arg2, path);
1131 if (VerboseOpt == 0) {
1136 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1137 snapshots_path, arg2, path);
1138 if (VerboseOpt == 0)
1145 runcmd(int *resp, const char *ctl, ...)
1157 * Generate the command
1160 vasprintf(&cmd, ctl, va);
1163 printf(" %s\n", cmd);
1166 * Break us down into arguments. We do not just use system() here
1167 * because it blocks SIGINT and friends.
1171 av = malloc(sizeof(char *) * nmax);
1173 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
1174 if (n == nmax - 1) {
1176 av = realloc(av, sizeof(char *) * nmax);
1186 if ((pid = fork()) == 0) {
1187 if (VerboseOpt < 2) {
1188 int fd = open("/dev/null", O_RDWR);
1194 } else if (pid < 0) {
1199 while (waitpid(pid, &status, 0) != pid)
1201 res = WEXITSTATUS(status);