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);
399 if (cmd == NULL || cmd[0] == '#')
405 if ((ptr = strtok(NULL, WS)) != NULL) {
406 arg1 = strtosecs(ptr);
407 if ((ptr = strtok(NULL, WS)) != NULL) {
408 arg2 = strtosecs(ptr);
409 arg3 = strtok(NULL, WS);
413 printf("%20s - ", cmd);
417 if (strcmp(cmd, "snapshots") == 0) {
420 check_softlinks(fd, new_config,
422 printf("only removing old snapshots\n");
424 cleanup_softlinks(fd, new_config,
428 printf("disabled\n");
429 snapshots_disabled = 1;
432 if (check_period(snapshots_path, cmd, arg1, &savet)) {
434 cleanup_softlinks(fd, new_config,
437 r = create_snapshot(path, snapshots_path);
441 } else if (arg1 == 0) {
443 * The commands following this check can't handle
444 * a period of 0, so call the feature disabled and
445 * ignore the directive.
447 printf("disabled\n");
448 } else if (strcmp(cmd, "prune") == 0) {
449 if (check_period(snapshots_path, cmd, arg1, &savet)) {
451 printf("run - WARNING snapshot "
453 "but snapshots disabled\n");
457 r = cleanup_prune(path, snapshots_path,
458 arg1, arg2, snapshots_disabled);
462 } else if (strcmp(cmd, "rebalance") == 0) {
464 if (check_period(snapshots_path, cmd, arg1, &savet)) {
469 r = cleanup_rebalance(path, snapshots_path,
474 } else if (strcmp(cmd, "reblock") == 0) {
475 if (check_period(snapshots_path, cmd, arg1, &savet)) {
480 r = cleanup_reblock(path, snapshots_path,
485 } else if (strcmp(cmd, "recopy") == 0) {
486 if (check_period(snapshots_path, cmd, arg1, &savet)) {
491 r = cleanup_recopy(path, snapshots_path,
497 printf("unknown directive\n");
501 save_period(snapshots_path, cmd, savet);
505 * Add new rebalance feature if the config doesn't have it.
506 * (old style config only).
508 if (new_config == 0 && found_rebal == 0) {
509 if ((fp = fopen(config_path, "r+")) != NULL) {
511 fprintf(fp, "rebalance 1d 5m\n");
517 * Cleanup, and delay a little
524 * Initialize new config data (new or old style)
527 config_init(const char *path, struct hammer_ioc_config *config)
529 const char *snapshots;
531 if (strcmp(path, "/tmp") == 0 ||
532 strcmp(path, "/var/tmp") == 0 ||
533 strcmp(path, "/usr/obj") == 0) {
534 snapshots = "snapshots 0d 0d\n";
536 snapshots = "snapshots 1d 60d\n";
538 bzero(config->config.text, sizeof(config->config.text));
539 snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s",
548 * Migrate configuration data from the old snapshots/config
549 * file to the new meta-data format.
552 migrate_config(FILE *fp, struct hammer_ioc_config *config)
556 n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp);
558 bzero(config->config.text + n, sizeof(config->config.text) - n);
562 * Migrate snapshot softlinks in the snapshots directory to the
563 * new meta-data format. The softlinks are left intact, but
564 * this way the pruning code won't lose track of them if you
565 * happen to blow away the snapshots directory.
568 migrate_snapshots(int fd, const char *snapshots_path)
570 struct hammer_ioc_snapshot snapshot;
576 bzero(&snapshot, sizeof(snapshot));
578 if ((dir = opendir(snapshots_path)) != NULL) {
579 while ((den = readdir(dir)) != NULL) {
580 if (den->d_name[0] == '.')
582 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
583 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode)) {
584 migrate_one_snapshot(fd, fpath, &snapshot);
590 migrate_one_snapshot(fd, NULL, &snapshot);
595 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
596 * otherwise it is flushed when it fills up.
599 migrate_one_snapshot(int fd, const char *fpath,
600 struct hammer_ioc_snapshot *snapshot)
603 struct hammer_snapshot_data *snap;
617 tid = (hammer_tid_t)(int64_t)-1;
620 while (*ptr && *ptr != '-' && *ptr != '.')
624 r = sscanf(ptr, "%4d%2d%2d-%2d%2d",
625 &year, &month, &day, &hour, &minute);
628 bzero(&tm, sizeof(tm));
633 tm.tm_mon = month - 1;
634 tm.tm_year = year - 1900;
637 bzero(linkbuf, sizeof(linkbuf));
638 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 &&
639 (ptr = strrchr(linkbuf, '@')) != NULL &&
640 ptr > linkbuf && ptr[-1] == '@') {
641 tid = strtoull(ptr + 1, NULL, 16);
643 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) {
644 snap = &snapshot->snaps[snapshot->count];
645 bzero(snap, sizeof(*snap));
647 snap->ts = (u_int64_t)t * 1000000ULL;
648 snprintf(snap->label, sizeof(snap->label),
654 if ((fpath == NULL && snapshot->count) ||
655 snapshot->count == HAMMER_SNAPS_PER_IOCTL) {
656 printf(" (%d snapshots)", snapshot->count);
658 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) {
659 printf(" Ioctl to migrate snapshots failed: %s\n",
661 } else if (snapshot->head.error == EALREADY) {
664 } else if (snapshot->head.error) {
665 printf(" Ioctl to migrate snapshots failed: %s\n",
666 strerror(snapshot->head.error));
668 printf("index %d\n", snapshot->index);
671 snapshot->head.error = 0;
681 val = strtol(ptr, &ptr, 0);
695 errx(1, "illegal suffix converting %s\n", ptr);
702 dividing_slash(const char *path)
704 int len = strlen(path);
705 if (len && path[len-1] == '/')
712 * Check whether the desired period has elapsed since the last successful
713 * run. The run may take a while and cross a boundary so we remember the
714 * current time_t so we can save it later on.
716 * Periods in minutes, hours, or days are assumed to have been crossed
717 * if the local time crosses a minute, hour, or day boundary regardless
718 * of how close the last operation actually was.
721 check_period(const char *snapshots_path, const char *cmd, int arg1,
732 localtime_r(savep, &tp1);
735 * Retrieve the start time of the last successful operation.
737 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
738 fp = fopen(check_path, "r");
742 if (fgets(buf, sizeof(buf), fp) == NULL) {
748 lastt = strtol(buf, NULL, 0);
749 localtime_r(&lastt, &tp2);
752 * Normalize the times. e.g. if asked to do something on a 1-day
753 * interval the operation will be performed as soon as the day
754 * turns over relative to the previous operation, even if the previous
755 * operation ran a few seconds ago just before midnight.
757 if (arg1 % 60 == 0) {
761 if (arg1 % (60 * 60) == 0) {
765 if (arg1 % (24 * 60 * 60) == 0) {
770 baset = mktime(&tp1);
771 lastt = mktime(&tp2);
774 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
777 if ((int)(baset - lastt) >= arg1)
783 * Store the start time of the last successful operation.
786 save_period(const char *snapshots_path, const char *cmd,
793 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
794 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
795 fp = fopen(ncheck_path, "w");
797 fprintf(fp, "0x%08llx\n", (long long)savet);
799 rename(ncheck_path, ocheck_path);
802 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
803 ncheck_path, strerror(errno));
808 * Simply count the number of softlinks in the snapshots dir
811 check_softlinks(int fd, int new_config, const char *snapshots_path)
820 * Old-style softlink-based snapshots
822 if ((dir = opendir(snapshots_path)) != NULL) {
823 while ((den = readdir(dir)) != NULL) {
824 if (den->d_name[0] == '.')
826 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
827 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
835 * New-style snapshots are stored as filesystem meta-data,
839 struct hammer_ioc_snapshot snapshot;
841 bzero(&snapshot, sizeof(snapshot));
843 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
844 err(2, "hammer cleanup: check_softlink "
848 res += snapshot.count;
849 } while (snapshot.head.error == 0 && snapshot.count);
855 * Clean up expired softlinks in the snapshots dir
858 cleanup_softlinks(int fd, int new_config,
859 const char *snapshots_path, int arg2, char *arg3)
867 if (arg3 != NULL && strstr(arg3, "any") != NULL)
870 if ((dir = opendir(snapshots_path)) != NULL) {
871 while ((den = readdir(dir)) != NULL) {
872 if (den->d_name[0] == '.')
874 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
875 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
876 (anylink || strncmp(den->d_name, "snap-", 5) == 0)
878 if (check_expired(den->d_name, arg2)) {
880 printf(" expire %s\n",
892 * New-style snapshots are stored as filesystem meta-data,
896 struct hammer_ioc_snapshot snapshot;
897 struct hammer_ioc_snapshot dsnapshot;
898 struct hammer_snapshot_data *snap;
904 bzero(&snapshot, sizeof(snapshot));
905 bzero(&dsnapshot, sizeof(dsnapshot));
907 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
908 err(2, "hammer cleanup: check_softlink "
912 for (i = 0; i < snapshot.count; ++i) {
913 snap = &snapshot.snaps[i];
914 t = time(NULL) - snap->ts / 1000000ULL;
915 if ((int)t > arg2 && snap->tid != 0) {
916 dsnapshot.snaps[dsnapshot.count++] =
919 if ((int)t > arg2 && VerboseOpt) {
921 strftime(snapts, sizeof(snapts),
922 "%Y-%m-%d %H:%M:%S %Z", tp);
923 printf(" expire 0x%016jx %s %s\n",
924 (uintmax_t)snap->tid,
928 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL) {
929 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
930 printf(" Ioctl to delete snapshots failed: %s index %d\n", strerror(errno), dsnapshot.index);
931 } else if (dsnapshot.head.error) {
932 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
937 dsnapshot.head.error = 0;
940 } while (snapshot.head.error == 0 && snapshot.count);
942 if (dsnapshot.count) {
943 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
944 printf(" Ioctl to delete snapshots failed: %s\n", strerror(errno));
945 } else if (dsnapshot.head.error) {
946 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
950 dsnapshot.head.error = 0;
956 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
957 * expiration in seconds (arg2) and return non-zero if the softlink
961 check_expired(const char *fpath, int arg2)
972 while (*fpath && *fpath != '-' && *fpath != '.')
977 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
978 &year, &month, &day, &hour, &minute);
981 bzero(&tm, sizeof(tm));
986 tm.tm_mon = month - 1;
987 tm.tm_year = year - 1900;
1002 create_snapshot(const char *path, const char *snapshots_path)
1006 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
1011 cleanup_prune(const char *path __unused, const char *snapshots_path,
1012 int arg1 __unused, int arg2, int snapshots_disabled)
1015 * If snapshots have been disabled run prune-everything instead
1018 if (snapshots_disabled && arg2) {
1019 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1020 snapshots_path, arg2, path);
1021 } else if (snapshots_disabled) {
1022 runcmd(NULL, "hammer prune-everything %s", path);
1024 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
1025 snapshots_path, arg2, snapshots_path);
1027 runcmd(NULL, "hammer prune %s", snapshots_path);
1033 cleanup_rebalance(const char *path, const char *snapshots_path,
1034 int arg1 __unused, int arg2)
1036 if (VerboseOpt == 0) {
1042 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1043 snapshots_path, arg2, path);
1044 if (VerboseOpt == 0) {
1048 if (VerboseOpt == 0)
1054 cleanup_reblock(const char *path, const char *snapshots_path,
1055 int arg1 __unused, int arg2)
1057 if (VerboseOpt == 0) {
1063 * When reblocking the B-Tree always reblock everything in normal
1067 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1068 snapshots_path, arg2, path);
1069 if (VerboseOpt == 0) {
1075 * When reblocking the inodes always reblock everything in normal
1079 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1080 snapshots_path, arg2, path);
1081 if (VerboseOpt == 0) {
1087 * When reblocking the directories always reblock everything in normal
1091 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1092 snapshots_path, arg2, path);
1093 if (VerboseOpt == 0) {
1099 * Do not reblock all the data in normal mode.
1102 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1103 snapshots_path, arg2, path);
1104 if (VerboseOpt == 0)
1110 cleanup_recopy(const char *path, const char *snapshots_path,
1111 int arg1 __unused, int arg2)
1113 if (VerboseOpt == 0) {
1118 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1119 snapshots_path, arg2, path);
1120 if (VerboseOpt == 0) {
1125 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1126 snapshots_path, arg2, path);
1127 if (VerboseOpt == 0) {
1132 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1133 snapshots_path, arg2, path);
1134 if (VerboseOpt == 0) {
1139 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1140 snapshots_path, arg2, path);
1141 if (VerboseOpt == 0)
1148 runcmd(int *resp, const char *ctl, ...)
1160 * Generate the command
1163 vasprintf(&cmd, ctl, va);
1166 printf(" %s\n", cmd);
1169 * Break us down into arguments. We do not just use system() here
1170 * because it blocks SIGINT and friends.
1174 av = malloc(sizeof(char *) * nmax);
1176 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
1177 if (n == nmax - 1) {
1179 av = realloc(av, sizeof(char *) * nmax);
1189 if ((pid = fork()) == 0) {
1190 if (VerboseOpt < 2) {
1191 int fd = open("/dev/null", O_RDWR);
1197 } else if (pid < 0) {
1202 while (waitpid(pid, &status, 0) != pid)
1204 res = WEXITSTATUS(status);