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 a specific HAMMER filesystem or all HAMMER filesystems.
39 * Each filesystem is expected to have a <mount>/snapshots directory.
40 * No cleanup will be performed on any filesystem that does not. If
41 * no filesystems are specified the 'df' program is run and any HAMMER
42 * or null-mounted hammer PFS's are extracted.
44 * The snapshots directory may contain a config file called 'config'. If
45 * no config file is present one will be created with the following
48 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj)
53 * All hammer commands create and maintain cycle files in the snapshots
64 static void do_cleanup(const char *path);
65 static int strtosecs(char *ptr);
66 static const char *dividing_slash(const char *path);
67 static int check_period(const char *snapshots_path, const char *cmd, int arg1,
69 static void save_period(const char *snapshots_path, const char *cmd,
71 static int check_softlinks(const char *snapshots_path);
72 static void cleanup_softlinks(const char *path, const char *snapshots_path,
73 int arg2, char *arg3);
74 static int check_expired(const char *fpath, int arg2);
76 static int cleanup_snapshots(const char *path, const char *snapshots_path,
78 static int cleanup_prune(const char *path, const char *snapshots_path,
79 int arg1, int arg2, int snapshots_disabled);
80 static int cleanup_reblock(const char *path, const char *snapshots_path,
82 static int cleanup_recopy(const char *path, const char *snapshots_path,
85 static void runcmd(int *resp, const char *ctl, ...);
89 struct didpfs *FirstPFS;
92 hammer_cmd_cleanup(char **av, int ac)
95 char *fs, *ptr, *path;
100 fp = popen("/sbin/mount -t hammer,null", "r");
102 errx(1, "hammer cleanup: 'mount' failed");
103 while (fgets(buf, sizeof(buf), fp) != NULL) {
104 fs = strtok(buf, WS);
107 ptr = strtok(NULL, WS);
110 path = strtok(NULL, WS);
113 ptr = strtok(NULL, WS);
116 if ((strncmp(ptr, "(hammer,", 8) == 0) ||
117 ((strncmp(ptr, "(null,", 6) == 0) &&
118 (strstr(fs, "/@@0x") != NULL ||
119 strstr(fs, "/@@-1") != NULL))) {
135 do_cleanup(const char *path)
137 struct hammer_ioc_pseudofs_rw pfs;
138 union hammer_ioc_mrecord_any mrec_tmp;
139 char *snapshots_path;
150 struct didpfs *didpfs;
151 int snapshots_disabled = 0;
152 int prune_warning = 0;
156 bzero(&pfs, sizeof(pfs));
157 bzero(&mrec_tmp, sizeof(mrec_tmp));
158 pfs.ondisk = &mrec_tmp.pfs.pfsd;
159 pfs.bytes = sizeof(mrec_tmp.pfs.pfsd);
162 printf("cleanup %-20s -", path);
163 fd = open(path, O_RDONLY);
165 printf(" unable to access directory: %s\n", strerror(errno));
168 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
169 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
173 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
174 printf(" unrecognized HAMMER version\n");
179 * Make sure we have not already handled this PFS. Several nullfs
180 * mounts might alias the same PFS.
182 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
183 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
184 printf(" PFS #%d already handled\n", pfs.pfs_id);
188 didpfs = malloc(sizeof(*didpfs));
189 didpfs->next = FirstPFS;
191 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
194 * Figure out where the snapshot directory is.
196 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
197 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
198 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
199 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
201 } else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
202 printf(" WARNING: must configure snapshot dir for PFS slave\n");
203 printf("\tWe suggest <fs>/var/slaves/<name> where "
204 "<fs> is the base HAMMER fs\n");
205 printf("\tcontaining the slave\n");
208 asprintf(&snapshots_path,
209 "%s%ssnapshots", path, dividing_slash(path));
213 * Create a snapshot directory if necessary, and a config file if
216 if (stat(snapshots_path, &st) < 0) {
217 if (mkdir(snapshots_path, 0755) != 0) {
218 free(snapshots_path);
219 printf(" unable to create snapshot dir \"%s\": %s\n",
220 snapshots_path, strerror(errno));
224 asprintf(&config_path, "%s/config", snapshots_path);
225 if ((fp = fopen(config_path, "r")) == NULL) {
226 fp = fopen(config_path, "w");
228 printf(" cannot create %s: %s\n",
229 config_path, strerror(errno));
232 if (strcmp(path, "/tmp") == 0 ||
233 strcmp(path, "/var/tmp") == 0 ||
234 strcmp(path, "/usr/obj") == 0) {
235 fprintf(fp, "snapshots 0d 0d\n");
237 fprintf(fp, "snapshots 1d 60d\n");
244 fp = fopen(config_path, "r");
247 printf(" cannot access %s: %s\n",
248 config_path, strerror(errno));
252 printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
255 * Process the config file
257 while (fgets(buf, sizeof(buf), fp) != NULL) {
258 cmd = strtok(buf, WS);
262 if ((ptr = strtok(NULL, WS)) != NULL) {
263 arg1 = strtosecs(ptr);
264 if ((ptr = strtok(NULL, WS)) != NULL) {
265 arg2 = strtosecs(ptr);
266 arg3 = strtok(NULL, WS);
270 printf("%20s - ", cmd);
274 if (strcmp(cmd, "snapshots") == 0) {
276 if (arg2 && check_softlinks(snapshots_path)) {
277 printf("only removing old snapshots\n");
279 cleanup_softlinks(path, snapshots_path,
282 printf("disabled\n");
283 snapshots_disabled = 1;
286 if (check_period(snapshots_path, cmd, arg1, &savet)) {
288 cleanup_softlinks(path, snapshots_path,
290 r = cleanup_snapshots(path, snapshots_path,
295 } else if (arg1 == 0) {
297 * The commands following this check can't handle
298 * a period of 0, so call the feature disabled and
299 * ignore the directive.
301 printf("disabled\n");
302 } else if (strcmp(cmd, "prune") == 0) {
303 if (check_period(snapshots_path, cmd, arg1, &savet)) {
305 printf("run - WARNING snapshot "
307 "but snapshots disabled\n");
311 r = cleanup_prune(path, snapshots_path,
312 arg1, arg2, snapshots_disabled);
316 } else if (strcmp(cmd, "reblock") == 0) {
317 if (check_period(snapshots_path, cmd, arg1, &savet)) {
322 r = cleanup_reblock(path, snapshots_path,
327 } else if (strcmp(cmd, "recopy") == 0) {
328 if (check_period(snapshots_path, cmd, arg1, &savet)) {
333 r = cleanup_recopy(path, snapshots_path,
339 printf("unknown directive\n");
343 save_period(snapshots_path, cmd, savet);
355 val = strtol(ptr, &ptr, 0);
369 errx(1, "illegal suffix converting %s\n", ptr);
376 dividing_slash(const char *path)
378 int len = strlen(path);
379 if (len && path[len-1] == '/')
386 * Check whether the desired period has elapsed since the last successful
387 * run. The run may take a while and cross a boundary so we remember the
388 * current time_t so we can save it later on.
390 * Periods in minutes, hours, or days are assumed to have been crossed
391 * if the local time crosses a minute, hour, or day boundary regardless
392 * of how close the last operation actually was.
395 check_period(const char *snapshots_path, const char *cmd, int arg1,
406 localtime_r(savep, &tp1);
409 * Retrieve the start time of the last successful operation.
411 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
412 fp = fopen(check_path, "r");
416 if (fgets(buf, sizeof(buf), fp) == NULL) {
422 lastt = strtol(buf, NULL, 0);
423 localtime_r(&lastt, &tp2);
426 * Normalize the times. e.g. if asked to do something on a 1-day
427 * interval the operation will be performed as soon as the day
428 * turns over relative to the previous operation, even if the previous
429 * operation ran a few seconds ago just before midnight.
431 if (arg1 % 60 == 0) {
435 if (arg1 % (60 * 60) == 0) {
439 if (arg1 % (24 * 60 * 60) == 0) {
444 baset = mktime(&tp1);
445 lastt = mktime(&tp2);
448 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
451 if ((int)(baset - lastt) >= arg1)
457 * Store the start time of the last successful operation.
460 save_period(const char *snapshots_path, const char *cmd,
467 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
468 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
469 fp = fopen(ncheck_path, "w");
471 fprintf(fp, "0x%08llx\n", (long long)savet);
473 rename(ncheck_path, ocheck_path);
476 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
477 ncheck_path, strerror(errno));
482 * Simply count the number of softlinks in the snapshots dir
485 check_softlinks(const char *snapshots_path)
493 if ((dir = opendir(snapshots_path)) != NULL) {
494 while ((den = readdir(dir)) != NULL) {
495 if (den->d_name[0] == '.')
497 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
498 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
508 * Clean up expired softlinks in the snapshots dir
511 cleanup_softlinks(const char *path __unused, const char *snapshots_path,
512 int arg2, char *arg3)
520 if (arg3 != NULL && strstr(arg3, "any") != NULL)
523 if ((dir = opendir(snapshots_path)) != NULL) {
524 while ((den = readdir(dir)) != NULL) {
525 if (den->d_name[0] == '.')
527 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
528 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
529 (anylink || strncmp(den->d_name, "snap-", 5) == 0)
531 if (check_expired(den->d_name, arg2)) {
533 printf(" expire %s\n",
546 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
547 * expiration in seconds (arg2) and return non-zero if the softlink
551 check_expired(const char *fpath, int arg2)
562 while (*fpath && *fpath != '-' && *fpath != '.')
567 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
568 &year, &month, &day, &hour, &minute);
571 bzero(&tm, sizeof(tm));
576 tm.tm_mon = month - 1;
577 tm.tm_year = year - 1900;
592 cleanup_snapshots(const char *path __unused, const char *snapshots_path,
593 int arg1 __unused, int arg2 __unused)
597 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
602 cleanup_prune(const char *path __unused, const char *snapshots_path,
603 int arg1 __unused, int arg2, int snapshots_disabled)
606 * If snapshots have been disabled run prune-everything instead
609 if (snapshots_disabled && arg2) {
610 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
611 snapshots_path, arg2, path);
612 } else if (snapshots_disabled) {
613 runcmd(NULL, "hammer prune-everything %s", path);
615 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
616 snapshots_path, arg2, snapshots_path);
618 runcmd(NULL, "hammer prune %s", snapshots_path);
624 cleanup_reblock(const char *path, const char *snapshots_path,
625 int arg1 __unused, int arg2)
627 if (VerboseOpt == 0) {
633 * When reblocking the B-Tree always reblock everything in normal
637 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
638 snapshots_path, arg2, path);
639 if (VerboseOpt == 0) {
645 * When reblocking the inodes always reblock everything in normal
649 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
650 snapshots_path, arg2, path);
651 if (VerboseOpt == 0) {
657 * When reblocking the directories always reblock everything in normal
661 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
662 snapshots_path, arg2, path);
663 if (VerboseOpt == 0) {
669 * Do not reblock all the data in normal mode.
672 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
673 snapshots_path, arg2, path);
680 cleanup_recopy(const char *path, const char *snapshots_path,
681 int arg1 __unused, int arg2)
683 if (VerboseOpt == 0) {
688 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
689 snapshots_path, arg2, path);
690 if (VerboseOpt == 0) {
695 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
696 snapshots_path, arg2, path);
697 if (VerboseOpt == 0) {
702 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
703 snapshots_path, arg2, path);
704 if (VerboseOpt == 0) {
709 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
710 snapshots_path, arg2, path);
718 runcmd(int *resp, const char *ctl, ...)
730 * Generate the command
733 vasprintf(&cmd, ctl, va);
736 printf(" %s\n", cmd);
739 * Break us down into arguments. We do not just use system() here
740 * because it blocks SIGINT and friends.
744 av = malloc(sizeof(char *) * nmax);
746 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
749 av = realloc(av, sizeof(char *) * nmax);
759 if ((pid = fork()) == 0) {
760 if (VerboseOpt < 2) {
761 int fd = open("/dev/null", O_RDWR);
767 } else if (pid < 0) {
772 while (waitpid(pid, &status, 0) != pid)
774 res = WEXITSTATUS(status);