sbin/*hammer: Use typedef'd for struct zone_stat*
[dragonfly.git] / sbin / hammer / cmd_cleanup.c
blobdab6b0cb77b3f80e1f6f57c9f82e8be5cdecd467
1 /*
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
9 * are met:
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.
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.
35 * Clean up specific HAMMER filesystems or all HAMMER filesystems.
37 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
38 * are cleaned.
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:
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
51 * All hammer commands create and maintain cycle files in the snapshots
52 * directory.
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).
61 #include <libutil.h>
63 #include "hammer.h"
65 struct didpfs {
66 struct didpfs *next;
67 uuid_t uuid;
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);
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);
100 static void runcmd(int *resp, const char *ctl, ...) __printflike(2, 3);
102 #define WS " \t\r\n"
104 struct didpfs *FirstPFS;
106 void
107 hammer_cmd_cleanup(char **av, int ac)
109 char *fstype, *fs, *path;
110 struct statfs *stfsbuf;
111 int mntsize, i;
113 tzset();
114 if (ac == 0) {
115 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
116 if (mntsize > 0) {
117 for (i=0; i < mntsize; i++) {
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.
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);
135 } else {
136 while (ac) {
137 do_cleanup(*av);
138 --ac;
139 ++av;
144 static
145 void
146 do_cleanup(const char *path)
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;
174 bzero(&mrec_tmp, sizeof(mrec_tmp));
175 clrpfs(&pfs, &mrec_tmp.pfs.pfsd, -1);
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;
183 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
184 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
185 close(fd);
186 return;
188 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
189 printf(" unrecognized HAMMER version\n");
190 close(fd);
191 return;
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;
199 HammerVersion = version.cur_version;
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;
210 * Make sure we have not already handled this PFS. Several nullfs
211 * mounts might alias the same PFS.
213 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
214 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
215 printf(" PFS#%d already handled\n", pfs.pfs_id);
216 close(fd);
217 return;
220 didpfs = malloc(sizeof(*didpfs));
221 didpfs->next = FirstPFS;
222 FirstPFS = didpfs;
223 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
226 * Calculate the old snapshots directory for HAMMER VERSION < 3
228 * If the directory is explicitly specified in the PFS config
229 * we flag it and will not migrate it later.
231 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
232 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
233 snapshots_from_pfs = 1;
234 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
235 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
236 close(fd);
237 return;
238 } else if (hammer_is_pfs_slave(&mrec_tmp.pfs.pfsd)) {
239 if (version.cur_version < 3) {
240 printf(" WARNING: must configure snapshot dir for PFS slave\n");
241 printf("\tWe suggest <fs>/var/slaves/<name> where "
242 "<fs> is the base HAMMER fs\n");
243 printf("\tcontaining the slave\n");
244 close(fd);
245 return;
247 } else {
248 asprintf(&snapshots_path,
249 "%s%ssnapshots", path, dividing_slash(path));
253 * Check for old-style config file
255 if (snapshots_path) {
256 asprintf(&config_path, "%s/config", snapshots_path);
257 fp = fopen(config_path, "r");
261 * Handle upgrades to hammer version 3, move the config
262 * file into meta-data.
264 * For the old config read the file into the config structure,
265 * we will parse it out of the config structure regardless.
267 if (version.cur_version >= 3) {
268 if (fp) {
269 printf("(migrating) ");
270 fflush(stdout);
271 migrate_config(fp, &config);
272 migrate_snapshots(fd, snapshots_path);
273 fclose(fp);
274 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
275 printf(" cannot init meta-data config!\n");
276 close(fd);
277 return;
279 remove(config_path);
280 } else if (new_config == 0) {
281 config_init(path, &config);
282 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
283 printf(" cannot init meta-data config!\n");
284 close(fd);
285 return;
288 new_config = 1;
289 } else {
291 * Create missing snapshots directory for HAMMER VERSION < 3
293 if (stat(snapshots_path, &st) < 0) {
294 if (mkdir(snapshots_path, 0755) != 0) {
295 free(snapshots_path);
296 printf(" unable to create snapshot dir \"%s\": %s\n",
297 snapshots_path, strerror(errno));
298 close(fd);
299 return;
304 * Create missing config file for HAMMER VERSION < 3
306 if (fp == NULL) {
307 config_init(path, &config);
308 fp = fopen(config_path, "w");
309 if (fp) {
310 fwrite(config.config.text, 1,
311 strlen(config.config.text), fp);
312 fclose(fp);
314 } else {
315 migrate_config(fp, &config);
316 fclose(fp);
321 * If snapshots_from_pfs is not set we calculate the new snapshots
322 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
323 * the old snapshots directory over.
325 * People who have set an explicit snapshots directory will have
326 * to migrate the data manually into /var/hammer, or not bother at
327 * all. People running slaves may wish to migrate it and then
328 * clear the snapshots specification in the PFS config for the
329 * slave.
331 if (new_config && snapshots_from_pfs == 0) {
332 char *npath;
334 if (path[0] != '/') {
335 printf(" path must start with '/'\n");
336 return;
338 if (strcmp(path, "/") == 0)
339 asprintf(&npath, "%s/root", SNAPSHOTS_BASE);
340 else
341 asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1);
342 if (snapshots_path) {
343 if (stat(npath, &st) < 0 && errno == ENOENT) {
344 if (stat(snapshots_path, &st) < 0 && errno == ENOENT) {
345 printf(" HAMMER UPGRADE: Creating snapshots\n"
346 "\tCreating snapshots in %s\n",
347 npath);
348 runcmd(&r, "mkdir -p %s", npath);
349 } else {
350 printf(" HAMMER UPGRADE: Moving snapshots\n"
351 "\tMoving snapshots from %s to %s\n",
352 snapshots_path, npath);
353 runcmd(&r, "mkdir -p %s", npath);
354 runcmd(&r, "cpdup %s %s", snapshots_path, npath);
355 if (r != 0) {
356 printf("Unable to move snapshots directory!\n");
357 printf("Please fix this critical error.\n");
358 printf("Aborting cleanup of %s\n", path);
359 close(fd);
360 return;
362 runcmd(&r, "rm -rf %s", snapshots_path);
365 free(snapshots_path);
366 } else if (stat(npath, &st) < 0 && errno == ENOENT) {
367 runcmd(&r, "mkdir -p %s", npath);
369 snapshots_path = npath;
373 * Lock the PFS. fd is the base directory of the mounted PFS.
375 if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
376 if (errno == EWOULDBLOCK)
377 printf(" PFS#%d locked by other process\n", pfs.pfs_id);
378 else
379 printf(" can not lock %s: %s\n", config_path, strerror(errno));
380 close(fd);
381 return;
384 printf(" handle PFS#%d using %s\n", pfs.pfs_id, snapshots_path);
386 struct pidfh *pfh = NULL;
387 static char pidfile[PIDFILE_BUFSIZE];
389 snprintf (pidfile, PIDFILE_BUFSIZE, "%s/hammer.cleanup.%d",
390 pidfile_loc, getpid());
391 pfh = pidfile_open(pidfile, 0644, NULL);
392 if (pfh == NULL)
393 warn ("Unable to open or create %s", pidfile);
394 pidfile_write(pfh);
397 * Process the config file
399 cbase = config.config.text;
401 while ((cptr = strchr(cbase, '\n')) != NULL) {
402 bcopy(cbase, buf, cptr - cbase);
403 buf[cptr - cbase] = 0;
404 cbase = cptr + 1;
406 cmd = strtok(buf, WS);
407 if (cmd == NULL || cmd[0] == '#')
408 continue;
410 arg1 = 0;
411 arg2 = 0;
412 arg3 = NULL;
413 if ((ptr = strtok(NULL, WS)) != NULL) {
414 arg1 = strtosecs(ptr);
415 if ((ptr = strtok(NULL, WS)) != NULL) {
416 arg2 = strtosecs(ptr);
417 arg3 = strtok(NULL, WS);
421 printf("%20s - ", cmd);
422 fflush(stdout);
424 r = 1;
425 if (strcmp(cmd, "snapshots") == 0) {
426 if (arg1 == 0) {
427 if (arg2 &&
428 check_softlinks(fd, new_config,
429 snapshots_path)) {
430 printf("only removing old snapshots\n");
431 prune_warning = 1;
432 cleanup_softlinks(fd, new_config,
433 snapshots_path,
434 arg2, arg3);
435 } else {
436 printf("disabled\n");
437 snapshots_disabled = 1;
439 } else
440 if (check_period(snapshots_path, cmd, arg1, &savet)) {
441 printf("run\n");
442 cleanup_softlinks(fd, new_config,
443 snapshots_path,
444 arg2, arg3);
445 r = create_snapshot(path, snapshots_path);
446 } else {
447 printf("skip\n");
449 } else if (arg1 == 0) {
451 * The commands following this check can't handle
452 * a period of 0, so call the feature disabled and
453 * ignore the directive.
455 printf("disabled\n");
456 } else if (strcmp(cmd, "prune") == 0) {
457 if (check_period(snapshots_path, cmd, arg1, &savet)) {
458 if (prune_warning) {
459 printf("run - WARNING snapshot "
460 "softlinks present "
461 "but snapshots disabled\n");
462 } else {
463 printf("run\n");
465 r = cleanup_prune(path, snapshots_path,
466 arg1, arg2, snapshots_disabled);
467 } else {
468 printf("skip\n");
470 } else if (strcmp(cmd, "rebalance") == 0) {
471 found_rebal = 1;
472 if (check_period(snapshots_path, cmd, arg1, &savet)) {
473 printf("run");
474 fflush(stdout);
475 if (VerboseOpt)
476 printf("\n");
477 r = cleanup_rebalance(path, snapshots_path,
478 arg1, arg2);
479 } else {
480 printf("skip\n");
482 } else if (strcmp(cmd, "reblock") == 0) {
483 if (check_period(snapshots_path, cmd, arg1, &savet)) {
484 printf("run");
485 fflush(stdout);
486 if (VerboseOpt)
487 printf("\n");
488 r = cleanup_reblock(path, snapshots_path,
489 arg1, arg2);
490 } else {
491 printf("skip\n");
493 } else if (strcmp(cmd, "recopy") == 0) {
494 if (check_period(snapshots_path, cmd, arg1, &savet)) {
495 printf("run");
496 fflush(stdout);
497 if (VerboseOpt)
498 printf("\n");
499 r = cleanup_recopy(path, snapshots_path,
500 arg1, arg2);
501 } else {
502 printf("skip\n");
504 } else if (strcmp(cmd, "dedup") == 0) {
505 if (check_period(snapshots_path, cmd, arg1, &savet)) {
506 printf("run");
507 fflush(stdout);
508 if (VerboseOpt)
509 printf("\n");
510 r = cleanup_dedup(path, snapshots_path,
511 arg1, arg2);
512 } else {
513 printf("skip\n");
515 } else {
516 printf("unknown directive\n");
517 r = 1;
519 if (r == 0)
520 save_period(snapshots_path, cmd, savet);
524 * Add new rebalance feature if the config doesn't have it.
525 * (old style config only).
527 if (new_config == 0 && found_rebal == 0) {
528 if ((fp = fopen(config_path, "r+")) != NULL) {
529 fseek(fp, 0L, 2);
530 fprintf(fp, "rebalance 1d 5m\n");
531 fclose(fp);
536 * Cleanup, and delay a little
538 close(fd);
539 usleep(1000);
540 pidfile_close(pfh);
541 pidfile_remove(pfh);
545 * Initialize new config data (new or old style)
547 static void
548 config_init(const char *path, struct hammer_ioc_config *config)
550 const char *snapshots;
552 if (strcmp(path, "/tmp") == 0 ||
553 strcmp(path, "/var/tmp") == 0 ||
554 strcmp(path, "/usr/obj") == 0) {
555 snapshots = "snapshots 0d 0d\n";
556 } else {
557 snapshots = "snapshots 1d 60d\n";
559 bzero(config->config.text, sizeof(config->config.text));
560 snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s",
561 snapshots,
562 "prune 1d 5m\n"
563 "rebalance 1d 5m\n"
564 "#dedup 1d 5m\n"
565 "reblock 1d 5m\n"
566 "recopy 30d 10m\n");
570 * Migrate configuration data from the old snapshots/config
571 * file to the new meta-data format.
573 static void
574 migrate_config(FILE *fp, struct hammer_ioc_config *config)
576 int n;
578 n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp);
579 if (n >= 0)
580 bzero(config->config.text + n, sizeof(config->config.text) - n);
584 * Migrate snapshot softlinks in the snapshots directory to the
585 * new meta-data format. The softlinks are left intact, but
586 * this way the pruning code won't lose track of them if you
587 * happen to blow away the snapshots directory.
589 static void
590 migrate_snapshots(int fd, const char *snapshots_path)
592 struct hammer_ioc_snapshot snapshot;
593 struct dirent *den;
594 struct stat st;
595 DIR *dir;
596 char *fpath;
598 bzero(&snapshot, sizeof(snapshot));
600 if ((dir = opendir(snapshots_path)) != NULL) {
601 while ((den = readdir(dir)) != NULL) {
602 if (den->d_name[0] == '.')
603 continue;
604 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
605 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
606 migrate_one_snapshot(fd, fpath, &snapshot);
607 free(fpath);
609 closedir(dir);
611 migrate_one_snapshot(fd, NULL, &snapshot);
616 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
617 * otherwise it is flushed when it fills up.
619 static void
620 migrate_one_snapshot(int fd, const char *fpath,
621 struct hammer_ioc_snapshot *snapshot)
623 if (fpath) {
624 hammer_snapshot_data_t snap;
625 struct tm tm;
626 time_t t;
627 int year;
628 int month;
629 int day = 0;
630 int hour = 0;
631 int minute = 0;
632 int r;
633 char linkbuf[1024];
634 const char *ptr;
635 hammer_tid_t tid;
637 t = (time_t)-1;
638 tid = (hammer_tid_t)(int64_t)-1;
640 /* fpath may contain directory components */
641 if ((ptr = strrchr(fpath, '/')) != NULL)
642 ++ptr;
643 else
644 ptr = fpath;
645 while (*ptr && *ptr != '-' && *ptr != '.')
646 ++ptr;
647 if (*ptr)
648 ++ptr;
649 r = sscanf(ptr, "%4d%2d%2d-%2d%2d",
650 &year, &month, &day, &hour, &minute);
652 if (r >= 3) {
653 bzero(&tm, sizeof(tm));
654 tm.tm_isdst = -1;
655 tm.tm_min = minute;
656 tm.tm_hour = hour;
657 tm.tm_mday = day;
658 tm.tm_mon = month - 1;
659 tm.tm_year = year - 1900;
660 t = mktime(&tm);
662 bzero(linkbuf, sizeof(linkbuf));
663 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 &&
664 (ptr = strrchr(linkbuf, '@')) != NULL &&
665 ptr > linkbuf && ptr[-1] == '@') {
666 tid = strtoull(ptr + 1, NULL, 16);
668 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) {
669 snap = &snapshot->snaps[snapshot->count];
670 bzero(snap, sizeof(*snap));
671 snap->tid = tid;
672 snap->ts = (uint64_t)t * 1000000ULL;
673 snprintf(snap->label, sizeof(snap->label),
674 "migrated");
675 ++snapshot->count;
676 } else {
677 printf(" non-canonical snapshot softlink: %s->%s\n",
678 fpath, linkbuf);
682 if ((fpath == NULL && snapshot->count) ||
683 snapshot->count == HAMMER_SNAPS_PER_IOCTL) {
684 printf(" (%d snapshots)", snapshot->count);
685 again:
686 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) {
687 printf(" Ioctl to migrate snapshots failed: %s\n",
688 strerror(errno));
689 } else if (snapshot->head.error == EALREADY) {
690 ++snapshot->index;
691 goto again;
692 } else if (snapshot->head.error) {
693 printf(" Ioctl to migrate snapshots failed: %s\n",
694 strerror(snapshot->head.error));
696 printf("index %d\n", snapshot->index);
697 snapshot->index = 0;
698 snapshot->count = 0;
699 snapshot->head.error = 0;
703 static
705 strtosecs(char *ptr)
707 int val;
709 val = strtol(ptr, &ptr, 0);
710 switch(*ptr) {
711 case 'd':
712 val *= 24;
713 /* fall through */
714 case 'h':
715 val *= 60;
716 /* fall through */
717 case 'm':
718 val *= 60;
719 /* fall through */
720 case 's':
721 break;
722 default:
723 errx(1, "illegal suffix converting %s", ptr);
724 /* not reached */
725 break;
727 return(val);
730 static const char *
731 dividing_slash(const char *path)
733 int len = strlen(path);
734 if (len && path[len-1] == '/')
735 return("");
736 else
737 return("/");
741 * Check whether the desired period has elapsed since the last successful
742 * run. The run may take a while and cross a boundary so we remember the
743 * current time_t so we can save it later on.
745 * Periods in minutes, hours, or days are assumed to have been crossed
746 * if the local time crosses a minute, hour, or day boundary regardless
747 * of how close the last operation actually was.
749 * If ForceOpt is set always return true.
751 static int
752 check_period(const char *snapshots_path, const char *cmd, int arg1,
753 time_t *savep)
755 char *check_path;
756 struct tm tp1;
757 struct tm tp2;
758 FILE *fp;
759 time_t baset, lastt;
760 char buf[256];
762 time(savep);
763 localtime_r(savep, &tp1);
766 * Force run if -F
768 if (ForceOpt)
769 return(1);
772 * Retrieve the start time of the last successful operation.
774 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
775 fp = fopen(check_path, "r");
776 free(check_path);
777 if (fp == NULL)
778 return(1);
779 if (fgets(buf, sizeof(buf), fp) == NULL) {
780 fclose(fp);
781 return(1);
783 fclose(fp);
785 lastt = strtol(buf, NULL, 0);
786 localtime_r(&lastt, &tp2);
789 * Normalize the times. e.g. if asked to do something on a 1-day
790 * interval the operation will be performed as soon as the day
791 * turns over relative to the previous operation, even if the previous
792 * operation ran a few seconds ago just before midnight.
794 if (arg1 % 60 == 0) {
795 tp1.tm_sec = 0;
796 tp2.tm_sec = 0;
798 if (arg1 % (60 * 60) == 0) {
799 tp1.tm_min = 0;
800 tp2.tm_min = 0;
802 if (arg1 % (24 * 60 * 60) == 0) {
803 tp1.tm_hour = 0;
804 tp2.tm_hour = 0;
807 baset = mktime(&tp1);
808 lastt = mktime(&tp2);
810 #if 0
811 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
812 #endif
814 if ((int)(baset - lastt) >= arg1)
815 return(1);
816 return(0);
820 * Store the start time of the last successful operation.
822 static void
823 save_period(const char *snapshots_path, const char *cmd,
824 time_t savet)
826 char *ocheck_path;
827 char *ncheck_path;
828 FILE *fp;
830 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
831 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
832 fp = fopen(ncheck_path, "w");
833 if (fp) {
834 fprintf(fp, "0x%08llx\n", (long long)savet);
835 if (fclose(fp) == 0)
836 rename(ncheck_path, ocheck_path);
837 remove(ncheck_path);
838 } else {
839 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
840 ncheck_path, strerror(errno));
845 * Simply count the number of softlinks in the snapshots dir
847 static int
848 check_softlinks(int fd, int new_config, const char *snapshots_path)
850 struct dirent *den;
851 struct stat st;
852 DIR *dir;
853 char *fpath;
854 int res = 0;
857 * Old-style softlink-based snapshots
859 if ((dir = opendir(snapshots_path)) != NULL) {
860 while ((den = readdir(dir)) != NULL) {
861 if (den->d_name[0] == '.')
862 continue;
863 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
864 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
865 ++res;
866 free(fpath);
868 closedir(dir);
872 * New-style snapshots are stored as filesystem meta-data,
873 * count those too.
875 if (new_config) {
876 struct hammer_ioc_snapshot snapshot;
878 bzero(&snapshot, sizeof(snapshot));
879 do {
880 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
881 err(2, "hammer cleanup: check_softlink "
882 "snapshot error");
883 /* not reached */
885 res += snapshot.count;
886 } while (snapshot.head.error == 0 && snapshot.count);
888 return (res);
892 * Clean up expired softlinks in the snapshots dir
894 static void
895 cleanup_softlinks(int fd, int new_config,
896 const char *snapshots_path, int arg2, char *arg3)
898 struct dirent *den;
899 struct stat st;
900 DIR *dir;
901 char *fpath;
902 int anylink = 0;
904 if (arg3 != NULL && strstr(arg3, "any") != NULL)
905 anylink = 1;
907 if ((dir = opendir(snapshots_path)) != NULL) {
908 while ((den = readdir(dir)) != NULL) {
909 if (den->d_name[0] == '.')
910 continue;
911 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
912 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
913 (anylink || strncmp(den->d_name, "snap-", 5) == 0)) {
914 if (check_expired(den->d_name, arg2)) {
915 if (VerboseOpt) {
916 printf(" expire %s\n",
917 fpath);
919 remove(fpath);
922 free(fpath);
924 closedir(dir);
928 * New-style snapshots are stored as filesystem meta-data,
929 * count those too.
931 if (new_config) {
932 struct hammer_ioc_snapshot snapshot;
933 struct hammer_ioc_snapshot dsnapshot;
934 hammer_snapshot_data_t snap;
935 struct tm *tp;
936 time_t t;
937 time_t dt;
938 char snapts[32];
939 uint32_t i;
941 bzero(&snapshot, sizeof(snapshot));
942 bzero(&dsnapshot, sizeof(dsnapshot));
943 do {
944 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
945 err(2, "hammer cleanup: check_softlink "
946 "snapshot error");
947 /* not reached */
949 for (i = 0; i < snapshot.count; ++i) {
950 snap = &snapshot.snaps[i];
951 t = snap->ts / 1000000ULL;
952 dt = time(NULL) - t;
953 if ((int)dt > arg2 || snap->tid == 0) {
954 dsnapshot.snaps[dsnapshot.count++] =
955 *snap;
957 if ((int)dt > arg2 && VerboseOpt) {
958 tp = localtime(&t);
959 strftime(snapts, sizeof(snapts),
960 "%Y-%m-%d %H:%M:%S %Z", tp);
961 printf(" expire 0x%016jx %s %s\n",
962 (uintmax_t)snap->tid,
963 snapts,
964 snap->label);
966 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
967 delete_snapshots(fd, &dsnapshot);
969 } while (snapshot.head.error == 0 && snapshot.count);
971 if (dsnapshot.count)
972 delete_snapshots(fd, &dsnapshot);
976 static void
977 delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot)
979 for (;;) {
980 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) {
981 printf(" Ioctl to delete snapshots failed: %s\n",
982 strerror(errno));
983 break;
985 if (dsnapshot->head.error) {
986 printf(" Ioctl to delete snapshots failed at "
987 "snap=%016jx: %s\n",
988 dsnapshot->snaps[dsnapshot->index].tid,
989 strerror(dsnapshot->head.error));
990 if (++dsnapshot->index < dsnapshot->count)
991 continue;
993 break;
995 dsnapshot->index = 0;
996 dsnapshot->count = 0;
997 dsnapshot->head.error = 0;
1001 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
1002 * expiration in seconds (arg2) and return non-zero if the softlink
1003 * has expired.
1005 static int
1006 check_expired(const char *fpath, int arg2)
1008 struct tm tm;
1009 time_t t;
1010 int year;
1011 int month;
1012 int day = 0;
1013 int hour = 0;
1014 int minute = 0;
1015 int r;
1017 while (*fpath && *fpath != '-' && *fpath != '.')
1018 ++fpath;
1019 if (*fpath)
1020 ++fpath;
1022 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
1023 &year, &month, &day, &hour, &minute);
1025 if (r >= 3) {
1026 bzero(&tm, sizeof(tm));
1027 tm.tm_isdst = -1;
1028 tm.tm_min = minute;
1029 tm.tm_hour = hour;
1030 tm.tm_mday = day;
1031 tm.tm_mon = month - 1;
1032 tm.tm_year = year - 1900;
1033 t = mktime(&tm);
1034 if (t == (time_t)-1)
1035 return(0);
1036 t = time(NULL) - t;
1037 if ((int)t > arg2)
1038 return(1);
1040 return(0);
1044 * Issue a snapshot.
1046 static int
1047 create_snapshot(const char *path, const char *snapshots_path)
1049 int r;
1051 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
1052 return(r);
1055 static int
1056 cleanup_prune(const char *path, const char *snapshots_path,
1057 int arg1 __unused, int arg2, int snapshots_disabled)
1059 const char *path_or_snapshots_path;
1062 * If the snapshots_path (e.g. /var/hammer/...) has no snapshots
1063 * in it then prune will get confused and prune the filesystem
1064 * containing the snapshots_path instead of the requested
1065 * filesystem. De-confuse prune. We need a better way.
1067 if (hammer_softprune_testdir(snapshots_path))
1068 path_or_snapshots_path = snapshots_path;
1069 else
1070 path_or_snapshots_path = path;
1073 * If snapshots have been disabled run prune-everything instead
1074 * of prune.
1076 if (snapshots_disabled && arg2) {
1077 runcmd(NULL,
1078 "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1079 snapshots_path, arg2, path);
1080 } else if (snapshots_disabled) {
1081 runcmd(NULL, "hammer prune-everything %s", path);
1082 } else if (arg2) {
1083 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
1084 snapshots_path, arg2, path_or_snapshots_path);
1085 } else {
1086 runcmd(NULL, "hammer prune %s", path_or_snapshots_path);
1088 return(0);
1091 static int
1092 cleanup_rebalance(const char *path, const char *snapshots_path,
1093 int arg1 __unused, int arg2)
1095 if (VerboseOpt == 0) {
1096 printf(".");
1097 fflush(stdout);
1100 runcmd(NULL,
1101 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1102 snapshots_path, arg2, path);
1103 if (VerboseOpt == 0) {
1104 printf(".");
1105 fflush(stdout);
1107 if (VerboseOpt == 0)
1108 printf("\n");
1109 return(0);
1112 static int
1113 cleanup_reblock(const char *path, const char *snapshots_path,
1114 int arg1 __unused, int arg2)
1116 if (VerboseOpt == 0) {
1117 printf(".");
1118 fflush(stdout);
1122 * When reblocking the B-Tree always reblock everything in normal
1123 * mode.
1125 runcmd(NULL,
1126 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1127 snapshots_path, arg2, path);
1128 if (VerboseOpt == 0) {
1129 printf(".");
1130 fflush(stdout);
1134 * When reblocking the inodes always reblock everything in normal
1135 * mode.
1137 runcmd(NULL,
1138 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1139 snapshots_path, arg2, path);
1140 if (VerboseOpt == 0) {
1141 printf(".");
1142 fflush(stdout);
1146 * When reblocking the directories always reblock everything in normal
1147 * mode.
1149 runcmd(NULL,
1150 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1151 snapshots_path, arg2, path);
1152 if (VerboseOpt == 0) {
1153 printf(".");
1154 fflush(stdout);
1158 * Do not reblock all the data in normal mode.
1160 runcmd(NULL,
1161 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1162 snapshots_path, arg2, path);
1163 if (VerboseOpt == 0)
1164 printf("\n");
1165 return(0);
1168 static int
1169 cleanup_recopy(const char *path, const char *snapshots_path,
1170 int arg1 __unused, int arg2)
1172 if (VerboseOpt == 0) {
1173 printf(".");
1174 fflush(stdout);
1176 runcmd(NULL,
1177 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1178 snapshots_path, arg2, path);
1179 if (VerboseOpt == 0) {
1180 printf(".");
1181 fflush(stdout);
1183 runcmd(NULL,
1184 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1185 snapshots_path, arg2, path);
1186 if (VerboseOpt == 0) {
1187 printf(".");
1188 fflush(stdout);
1190 runcmd(NULL,
1191 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1192 snapshots_path, arg2, path);
1193 if (VerboseOpt == 0) {
1194 printf(".");
1195 fflush(stdout);
1197 runcmd(NULL,
1198 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1199 snapshots_path, arg2, path);
1200 if (VerboseOpt == 0)
1201 printf("\n");
1202 return(0);
1205 static int
1206 cleanup_dedup(const char *path, const char *snapshots_path __unused,
1207 int arg1 __unused, int arg2)
1209 if (VerboseOpt == 0) {
1210 printf(".");
1211 fflush(stdout);
1214 runcmd(NULL, "hammer -t %d dedup %s", arg2, path);
1215 if (VerboseOpt == 0) {
1216 printf(".");
1217 fflush(stdout);
1219 if (VerboseOpt == 0)
1220 printf("\n");
1221 return(0);
1224 static
1225 void
1226 runcmd(int *resp, const char *ctl, ...)
1228 va_list va;
1229 char *cmd;
1230 char *arg;
1231 char **av;
1232 int n;
1233 int nmax;
1234 int res;
1235 pid_t pid;
1238 * Generate the command
1240 va_start(va, ctl);
1241 vasprintf(&cmd, ctl, va);
1242 va_end(va);
1243 if (VerboseOpt)
1244 printf(" %s\n", cmd);
1247 * Break us down into arguments. We do not just use system() here
1248 * because it blocks SIGINT and friends.
1250 n = 0;
1251 nmax = 16;
1252 av = malloc(sizeof(char *) * nmax);
1254 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
1255 if (n == nmax - 1) {
1256 nmax += 16;
1257 av = realloc(av, sizeof(char *) * nmax);
1259 av[n++] = arg;
1261 av[n++] = NULL;
1264 * Run the command.
1266 RunningIoctl = 1;
1267 if ((pid = fork()) == 0) {
1268 if (VerboseOpt < 2) {
1269 int fd = open("/dev/null", O_RDWR);
1270 dup2(fd, 1);
1271 close(fd);
1273 execvp(av[0], av);
1274 _exit(127);
1275 } else if (pid < 0) {
1276 res = 127;
1277 } else {
1278 int status;
1280 while (waitpid(pid, &status, 0) != pid)
1282 res = WEXITSTATUS(status);
1284 RunningIoctl = 0;
1285 if (DidInterrupt)
1286 _exit(1);
1288 free(cmd);
1289 free(av);
1290 if (resp)
1291 *resp = res;