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)
54 * All hammer commands create and maintain cycle files in the snapshots
65 static void do_cleanup(const char *path);
66 static int strtosecs(char *ptr);
67 static const char *dividing_slash(const char *path);
68 static int check_period(const char *snapshots_path, const char *cmd, int arg1,
70 static void save_period(const char *snapshots_path, const char *cmd,
72 static int check_softlinks(const char *snapshots_path);
73 static void cleanup_softlinks(const char *path, const char *snapshots_path,
74 int arg2, char *arg3);
75 static int check_expired(const char *fpath, int arg2);
77 static int create_snapshot(const char *path, const char *snapshots_path,
79 static int cleanup_rebalance(const char *path, const char *snapshots_path,
81 static int cleanup_prune(const char *path, const char *snapshots_path,
82 int arg1, int arg2, int snapshots_disabled);
83 static int cleanup_reblock(const char *path, const char *snapshots_path,
85 static int cleanup_recopy(const char *path, const char *snapshots_path,
88 static void runcmd(int *resp, const char *ctl, ...);
92 struct didpfs *FirstPFS;
95 hammer_cmd_cleanup(char **av, int ac)
97 char *fstype, *fs, *path;
98 struct statfs *stfsbuf;
103 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
105 for (i=0; i < mntsize; i++) {
107 * We will cleanup in the case fstype is hammer.
108 * If we have null-mounted PFS, we check the
109 * mount source. If it looks like a PFS, we
110 * proceed to cleanup also.
112 fstype = stfsbuf[i].f_fstypename;
113 fs = stfsbuf[i].f_mntfromname;
114 if ((strcmp(fstype, "hammer") == 0) ||
115 ((strcmp(fstype, "null") == 0) &&
116 (strstr(fs, "/@@0x") != NULL ||
117 strstr(fs, "/@@-1") != NULL))) {
118 path = stfsbuf[i].f_mntonname;
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;
157 bzero(&pfs, sizeof(pfs));
158 bzero(&mrec_tmp, sizeof(mrec_tmp));
159 pfs.ondisk = &mrec_tmp.pfs.pfsd;
160 pfs.bytes = sizeof(mrec_tmp.pfs.pfsd);
163 printf("cleanup %-20s -", path);
164 fd = open(path, O_RDONLY);
166 printf(" unable to access directory: %s\n", strerror(errno));
169 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
170 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
174 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
175 printf(" unrecognized HAMMER version\n");
180 * Make sure we have not already handled this PFS. Several nullfs
181 * mounts might alias the same PFS.
183 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
184 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
185 printf(" PFS #%d already handled\n", pfs.pfs_id);
189 didpfs = malloc(sizeof(*didpfs));
190 didpfs->next = FirstPFS;
192 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
195 * Figure out where the snapshot directory is.
197 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
198 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
199 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
200 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
202 } else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
203 printf(" WARNING: must configure snapshot dir for PFS slave\n");
204 printf("\tWe suggest <fs>/var/slaves/<name> where "
205 "<fs> is the base HAMMER fs\n");
206 printf("\tcontaining the slave\n");
209 asprintf(&snapshots_path,
210 "%s%ssnapshots", path, dividing_slash(path));
214 * Create a snapshot directory if necessary, and a config file if
217 if (stat(snapshots_path, &st) < 0) {
218 if (mkdir(snapshots_path, 0755) != 0) {
219 free(snapshots_path);
220 printf(" unable to create snapshot dir \"%s\": %s\n",
221 snapshots_path, strerror(errno));
225 asprintf(&config_path, "%s/config", snapshots_path);
226 if ((fp = fopen(config_path, "r")) == NULL) {
227 fp = fopen(config_path, "w");
229 printf(" cannot create %s: %s\n",
230 config_path, strerror(errno));
233 if (strcmp(path, "/tmp") == 0 ||
234 strcmp(path, "/var/tmp") == 0 ||
235 strcmp(path, "/usr/obj") == 0) {
236 fprintf(fp, "snapshots 0d 0d\n");
238 fprintf(fp, "snapshots 1d 60d\n");
246 fp = fopen(config_path, "r");
249 printf(" cannot access %s: %s\n",
250 config_path, strerror(errno));
254 if (flock(fileno(fp), LOCK_EX|LOCK_NB) == -1) {
255 if (errno == EWOULDBLOCK)
256 printf(" PFS #%d locked by other process\n", pfs.pfs_id);
258 printf(" can not lock %s: %s\n", config_path, strerror(errno));
263 printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
266 * Process the config file
268 while (fgets(buf, sizeof(buf), fp) != NULL) {
269 cmd = strtok(buf, WS);
273 if ((ptr = strtok(NULL, WS)) != NULL) {
274 arg1 = strtosecs(ptr);
275 if ((ptr = strtok(NULL, WS)) != NULL) {
276 arg2 = strtosecs(ptr);
277 arg3 = strtok(NULL, WS);
281 printf("%20s - ", cmd);
285 if (strcmp(cmd, "snapshots") == 0) {
287 if (arg2 && check_softlinks(snapshots_path)) {
288 printf("only removing old snapshots\n");
290 cleanup_softlinks(path, snapshots_path,
293 printf("disabled\n");
294 snapshots_disabled = 1;
297 if (check_period(snapshots_path, cmd, arg1, &savet)) {
299 cleanup_softlinks(path, snapshots_path,
301 r = create_snapshot(path, snapshots_path,
306 } else if (arg1 == 0) {
308 * The commands following this check can't handle
309 * a period of 0, so call the feature disabled and
310 * ignore the directive.
312 printf("disabled\n");
313 } else if (strcmp(cmd, "prune") == 0) {
314 if (check_period(snapshots_path, cmd, arg1, &savet)) {
316 printf("run - WARNING snapshot "
318 "but snapshots disabled\n");
322 r = cleanup_prune(path, snapshots_path,
323 arg1, arg2, snapshots_disabled);
327 } else if (strcmp(cmd, "rebalance") == 0) {
329 if (check_period(snapshots_path, cmd, arg1, &savet)) {
334 r = cleanup_rebalance(path, snapshots_path,
339 } else if (strcmp(cmd, "reblock") == 0) {
340 if (check_period(snapshots_path, cmd, arg1, &savet)) {
345 r = cleanup_reblock(path, snapshots_path,
350 } else if (strcmp(cmd, "recopy") == 0) {
351 if (check_period(snapshots_path, cmd, arg1, &savet)) {
356 r = cleanup_recopy(path, snapshots_path,
362 printf("unknown directive\n");
366 save_period(snapshots_path, cmd, savet);
371 * Add new rebalance feature if the config doesn't have it
373 if (found_rebal == 0) {
374 if ((fp = fopen(config_path, "r+")) != NULL) {
376 fprintf(fp, "rebalance 1d 5m\n");
389 val = strtol(ptr, &ptr, 0);
403 errx(1, "illegal suffix converting %s\n", ptr);
410 dividing_slash(const char *path)
412 int len = strlen(path);
413 if (len && path[len-1] == '/')
420 * Check whether the desired period has elapsed since the last successful
421 * run. The run may take a while and cross a boundary so we remember the
422 * current time_t so we can save it later on.
424 * Periods in minutes, hours, or days are assumed to have been crossed
425 * if the local time crosses a minute, hour, or day boundary regardless
426 * of how close the last operation actually was.
429 check_period(const char *snapshots_path, const char *cmd, int arg1,
440 localtime_r(savep, &tp1);
443 * Retrieve the start time of the last successful operation.
445 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
446 fp = fopen(check_path, "r");
450 if (fgets(buf, sizeof(buf), fp) == NULL) {
456 lastt = strtol(buf, NULL, 0);
457 localtime_r(&lastt, &tp2);
460 * Normalize the times. e.g. if asked to do something on a 1-day
461 * interval the operation will be performed as soon as the day
462 * turns over relative to the previous operation, even if the previous
463 * operation ran a few seconds ago just before midnight.
465 if (arg1 % 60 == 0) {
469 if (arg1 % (60 * 60) == 0) {
473 if (arg1 % (24 * 60 * 60) == 0) {
478 baset = mktime(&tp1);
479 lastt = mktime(&tp2);
482 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
485 if ((int)(baset - lastt) >= arg1)
491 * Store the start time of the last successful operation.
494 save_period(const char *snapshots_path, const char *cmd,
501 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
502 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
503 fp = fopen(ncheck_path, "w");
505 fprintf(fp, "0x%08llx\n", (long long)savet);
507 rename(ncheck_path, ocheck_path);
510 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
511 ncheck_path, strerror(errno));
516 * Simply count the number of softlinks in the snapshots dir
519 check_softlinks(const char *snapshots_path)
527 if ((dir = opendir(snapshots_path)) != NULL) {
528 while ((den = readdir(dir)) != NULL) {
529 if (den->d_name[0] == '.')
531 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
532 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
542 * Clean up expired softlinks in the snapshots dir
545 cleanup_softlinks(const char *path __unused, const char *snapshots_path,
546 int arg2, char *arg3)
554 if (arg3 != NULL && strstr(arg3, "any") != NULL)
557 if ((dir = opendir(snapshots_path)) != NULL) {
558 while ((den = readdir(dir)) != NULL) {
559 if (den->d_name[0] == '.')
561 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
562 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
563 (anylink || strncmp(den->d_name, "snap-", 5) == 0)
565 if (check_expired(den->d_name, arg2)) {
567 printf(" expire %s\n",
580 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
581 * expiration in seconds (arg2) and return non-zero if the softlink
585 check_expired(const char *fpath, int arg2)
596 while (*fpath && *fpath != '-' && *fpath != '.')
601 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
602 &year, &month, &day, &hour, &minute);
605 bzero(&tm, sizeof(tm));
610 tm.tm_mon = month - 1;
611 tm.tm_year = year - 1900;
626 create_snapshot(const char *path __unused, const char *snapshots_path,
627 int arg1 __unused, int arg2 __unused)
631 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
636 cleanup_prune(const char *path __unused, const char *snapshots_path,
637 int arg1 __unused, int arg2, int snapshots_disabled)
640 * If snapshots have been disabled run prune-everything instead
643 if (snapshots_disabled && arg2) {
644 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
645 snapshots_path, arg2, path);
646 } else if (snapshots_disabled) {
647 runcmd(NULL, "hammer prune-everything %s", path);
649 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
650 snapshots_path, arg2, snapshots_path);
652 runcmd(NULL, "hammer prune %s", snapshots_path);
658 cleanup_rebalance(const char *path, const char *snapshots_path,
659 int arg1 __unused, int arg2)
661 if (VerboseOpt == 0) {
667 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
668 snapshots_path, arg2, path);
669 if (VerboseOpt == 0) {
679 cleanup_reblock(const char *path, const char *snapshots_path,
680 int arg1 __unused, int arg2)
682 if (VerboseOpt == 0) {
688 * When reblocking the B-Tree always reblock everything in normal
692 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
693 snapshots_path, arg2, path);
694 if (VerboseOpt == 0) {
700 * When reblocking the inodes always reblock everything in normal
704 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
705 snapshots_path, arg2, path);
706 if (VerboseOpt == 0) {
712 * When reblocking the directories always reblock everything in normal
716 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
717 snapshots_path, arg2, path);
718 if (VerboseOpt == 0) {
724 * Do not reblock all the data in normal mode.
727 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
728 snapshots_path, arg2, path);
735 cleanup_recopy(const char *path, const char *snapshots_path,
736 int arg1 __unused, int arg2)
738 if (VerboseOpt == 0) {
743 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
744 snapshots_path, arg2, path);
745 if (VerboseOpt == 0) {
750 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
751 snapshots_path, arg2, path);
752 if (VerboseOpt == 0) {
757 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
758 snapshots_path, arg2, path);
759 if (VerboseOpt == 0) {
764 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
765 snapshots_path, arg2, path);
773 runcmd(int *resp, const char *ctl, ...)
785 * Generate the command
788 vasprintf(&cmd, ctl, va);
791 printf(" %s\n", cmd);
794 * Break us down into arguments. We do not just use system() here
795 * because it blocks SIGINT and friends.
799 av = malloc(sizeof(char *) * nmax);
801 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
804 av = realloc(av, sizeof(char *) * nmax);
814 if ((pid = fork()) == 0) {
815 if (VerboseOpt < 2) {
816 int fd = open("/dev/null", O_RDWR);
822 } else if (pid < 0) {
827 while (waitpid(pid, &status, 0) != pid)
829 res = WEXITSTATUS(status);