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 create_snapshot(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
)
94 char *fstype
, *fs
, *path
;
95 struct statfs
*stfsbuf
;
100 mntsize
= getmntinfo(&stfsbuf
, MNT_NOWAIT
);
102 for (i
=0; i
< mntsize
; i
++) {
104 * We will cleanup in the case fstype is hammer.
105 * If we have null-mounted PFS, we check the
106 * mount source. If it looks like a PFS, we
107 * proceed to cleanup also.
109 fstype
= stfsbuf
[i
].f_fstypename
;
110 fs
= stfsbuf
[i
].f_mntfromname
;
111 if ((strcmp(fstype
, "hammer") == 0) ||
112 ((strcmp(fstype
, "null") == 0) &&
113 (strstr(fs
, "/@@0x") != NULL
||
114 strstr(fs
, "/@@-1") != NULL
))) {
115 path
= stfsbuf
[i
].f_mntonname
;
132 do_cleanup(const char *path
)
134 struct hammer_ioc_pseudofs_rw pfs
;
135 union hammer_ioc_mrecord_any mrec_tmp
;
136 char *snapshots_path
;
147 struct didpfs
*didpfs
;
148 int snapshots_disabled
= 0;
149 int prune_warning
= 0;
153 bzero(&pfs
, sizeof(pfs
));
154 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
155 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
156 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
159 printf("cleanup %-20s -", path
);
160 fd
= open(path
, O_RDONLY
);
162 printf(" unable to access directory: %s\n", strerror(errno
));
165 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
166 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
170 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
171 printf(" unrecognized HAMMER version\n");
176 * Make sure we have not already handled this PFS. Several nullfs
177 * mounts might alias the same PFS.
179 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
180 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
181 printf(" PFS #%d already handled\n", pfs
.pfs_id
);
185 didpfs
= malloc(sizeof(*didpfs
));
186 didpfs
->next
= FirstPFS
;
188 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
191 * Figure out where the snapshot directory is.
193 if (mrec_tmp
.pfs
.pfsd
.snapshots
[0] == '/') {
194 asprintf(&snapshots_path
, "%s", mrec_tmp
.pfs
.pfsd
.snapshots
);
195 } else if (mrec_tmp
.pfs
.pfsd
.snapshots
[0]) {
196 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
198 } else if (mrec_tmp
.pfs
.pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
199 printf(" WARNING: must configure snapshot dir for PFS slave\n");
200 printf("\tWe suggest <fs>/var/slaves/<name> where "
201 "<fs> is the base HAMMER fs\n");
202 printf("\tcontaining the slave\n");
205 asprintf(&snapshots_path
,
206 "%s%ssnapshots", path
, dividing_slash(path
));
210 * Create a snapshot directory if necessary, and a config file if
213 if (stat(snapshots_path
, &st
) < 0) {
214 if (mkdir(snapshots_path
, 0755) != 0) {
215 free(snapshots_path
);
216 printf(" unable to create snapshot dir \"%s\": %s\n",
217 snapshots_path
, strerror(errno
));
221 asprintf(&config_path
, "%s/config", snapshots_path
);
222 if ((fp
= fopen(config_path
, "r")) == NULL
) {
223 fp
= fopen(config_path
, "w");
225 printf(" cannot create %s: %s\n",
226 config_path
, strerror(errno
));
229 if (strcmp(path
, "/tmp") == 0 ||
230 strcmp(path
, "/var/tmp") == 0 ||
231 strcmp(path
, "/usr/obj") == 0) {
232 fprintf(fp
, "snapshots 0d 0d\n");
234 fprintf(fp
, "snapshots 1d 60d\n");
241 fp
= fopen(config_path
, "r");
244 printf(" cannot access %s: %s\n",
245 config_path
, strerror(errno
));
249 if (flock(fileno(fp
), LOCK_EX
|LOCK_NB
) == -1) {
250 if (errno
== EWOULDBLOCK
)
251 printf(" PFS #%d locked by other process\n", pfs
.pfs_id
);
253 printf(" can not lock %s: %s\n", config_path
, strerror(errno
));
258 printf(" handle PFS #%d using %s\n", pfs
.pfs_id
, snapshots_path
);
261 * Process the config file
263 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
264 cmd
= strtok(buf
, WS
);
268 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
269 arg1
= strtosecs(ptr
);
270 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
271 arg2
= strtosecs(ptr
);
272 arg3
= strtok(NULL
, WS
);
276 printf("%20s - ", cmd
);
280 if (strcmp(cmd
, "snapshots") == 0) {
282 if (arg2
&& check_softlinks(snapshots_path
)) {
283 printf("only removing old snapshots\n");
285 cleanup_softlinks(path
, snapshots_path
,
288 printf("disabled\n");
289 snapshots_disabled
= 1;
292 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
294 cleanup_softlinks(path
, snapshots_path
,
296 r
= create_snapshot(path
, snapshots_path
,
301 } else if (arg1
== 0) {
303 * The commands following this check can't handle
304 * a period of 0, so call the feature disabled and
305 * ignore the directive.
307 printf("disabled\n");
308 } else if (strcmp(cmd
, "prune") == 0) {
309 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
311 printf("run - WARNING snapshot "
313 "but snapshots disabled\n");
317 r
= cleanup_prune(path
, snapshots_path
,
318 arg1
, arg2
, snapshots_disabled
);
322 } else if (strcmp(cmd
, "reblock") == 0) {
323 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
328 r
= cleanup_reblock(path
, snapshots_path
,
333 } else if (strcmp(cmd
, "recopy") == 0) {
334 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
339 r
= cleanup_recopy(path
, snapshots_path
,
345 printf("unknown directive\n");
349 save_period(snapshots_path
, cmd
, savet
);
361 val
= strtol(ptr
, &ptr
, 0);
375 errx(1, "illegal suffix converting %s\n", ptr
);
382 dividing_slash(const char *path
)
384 int len
= strlen(path
);
385 if (len
&& path
[len
-1] == '/')
392 * Check whether the desired period has elapsed since the last successful
393 * run. The run may take a while and cross a boundary so we remember the
394 * current time_t so we can save it later on.
396 * Periods in minutes, hours, or days are assumed to have been crossed
397 * if the local time crosses a minute, hour, or day boundary regardless
398 * of how close the last operation actually was.
401 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
412 localtime_r(savep
, &tp1
);
415 * Retrieve the start time of the last successful operation.
417 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
418 fp
= fopen(check_path
, "r");
422 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
428 lastt
= strtol(buf
, NULL
, 0);
429 localtime_r(&lastt
, &tp2
);
432 * Normalize the times. e.g. if asked to do something on a 1-day
433 * interval the operation will be performed as soon as the day
434 * turns over relative to the previous operation, even if the previous
435 * operation ran a few seconds ago just before midnight.
437 if (arg1
% 60 == 0) {
441 if (arg1
% (60 * 60) == 0) {
445 if (arg1
% (24 * 60 * 60) == 0) {
450 baset
= mktime(&tp1
);
451 lastt
= mktime(&tp2
);
454 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
457 if ((int)(baset
- lastt
) >= arg1
)
463 * Store the start time of the last successful operation.
466 save_period(const char *snapshots_path
, const char *cmd
,
473 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
474 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
475 fp
= fopen(ncheck_path
, "w");
477 fprintf(fp
, "0x%08llx\n", (long long)savet
);
479 rename(ncheck_path
, ocheck_path
);
482 fprintf(stderr
, "hammer: Unable to create period-file %s: %s\n",
483 ncheck_path
, strerror(errno
));
488 * Simply count the number of softlinks in the snapshots dir
491 check_softlinks(const char *snapshots_path
)
499 if ((dir
= opendir(snapshots_path
)) != NULL
) {
500 while ((den
= readdir(dir
)) != NULL
) {
501 if (den
->d_name
[0] == '.')
503 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
504 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
514 * Clean up expired softlinks in the snapshots dir
517 cleanup_softlinks(const char *path __unused
, const char *snapshots_path
,
518 int arg2
, char *arg3
)
526 if (arg3
!= NULL
&& strstr(arg3
, "any") != NULL
)
529 if ((dir
= opendir(snapshots_path
)) != NULL
) {
530 while ((den
= readdir(dir
)) != NULL
) {
531 if (den
->d_name
[0] == '.')
533 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
534 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
535 (anylink
|| strncmp(den
->d_name
, "snap-", 5) == 0)
537 if (check_expired(den
->d_name
, arg2
)) {
539 printf(" expire %s\n",
552 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
553 * expiration in seconds (arg2) and return non-zero if the softlink
557 check_expired(const char *fpath
, int arg2
)
568 while (*fpath
&& *fpath
!= '-' && *fpath
!= '.')
573 r
= sscanf(fpath
, "%4d%2d%2d-%2d%2d",
574 &year
, &month
, &day
, &hour
, &minute
);
577 bzero(&tm
, sizeof(tm
));
582 tm
.tm_mon
= month
- 1;
583 tm
.tm_year
= year
- 1900;
598 create_snapshot(const char *path __unused
, const char *snapshots_path
,
599 int arg1 __unused
, int arg2 __unused
)
603 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
608 cleanup_prune(const char *path __unused
, const char *snapshots_path
,
609 int arg1 __unused
, int arg2
, int snapshots_disabled
)
612 * If snapshots have been disabled run prune-everything instead
615 if (snapshots_disabled
&& arg2
) {
616 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
617 snapshots_path
, arg2
, path
);
618 } else if (snapshots_disabled
) {
619 runcmd(NULL
, "hammer prune-everything %s", path
);
621 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
622 snapshots_path
, arg2
, snapshots_path
);
624 runcmd(NULL
, "hammer prune %s", snapshots_path
);
630 cleanup_reblock(const char *path
, const char *snapshots_path
,
631 int arg1 __unused
, int arg2
)
633 if (VerboseOpt
== 0) {
639 * When reblocking the B-Tree always reblock everything in normal
643 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
644 snapshots_path
, arg2
, path
);
645 if (VerboseOpt
== 0) {
651 * When reblocking the inodes always reblock everything in normal
655 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
656 snapshots_path
, arg2
, path
);
657 if (VerboseOpt
== 0) {
663 * When reblocking the directories always reblock everything in normal
667 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
668 snapshots_path
, arg2
, path
);
669 if (VerboseOpt
== 0) {
675 * Do not reblock all the data in normal mode.
678 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
679 snapshots_path
, arg2
, path
);
686 cleanup_recopy(const char *path
, const char *snapshots_path
,
687 int arg1 __unused
, int arg2
)
689 if (VerboseOpt
== 0) {
694 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
695 snapshots_path
, arg2
, path
);
696 if (VerboseOpt
== 0) {
701 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
702 snapshots_path
, arg2
, path
);
703 if (VerboseOpt
== 0) {
708 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
709 snapshots_path
, arg2
, path
);
710 if (VerboseOpt
== 0) {
715 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
716 snapshots_path
, arg2
, path
);
724 runcmd(int *resp
, const char *ctl
, ...)
736 * Generate the command
739 vasprintf(&cmd
, ctl
, va
);
742 printf(" %s\n", cmd
);
745 * Break us down into arguments. We do not just use system() here
746 * because it blocks SIGINT and friends.
750 av
= malloc(sizeof(char *) * nmax
);
752 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
755 av
= realloc(av
, sizeof(char *) * nmax
);
765 if ((pid
= fork()) == 0) {
766 if (VerboseOpt
< 2) {
767 int fd
= open("/dev/null", O_RDWR
);
773 } else if (pid
< 0) {
778 while (waitpid(pid
, &status
, 0) != pid
)
780 res
= WEXITSTATUS(status
);