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 60d 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
,
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
)
101 fp
= popen("df -t hammer,null", "r");
103 errx(1, "hammer cleanup: 'df' failed");
104 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
105 ptr
= strtok(buf
, WS
);
106 if (ptr
&& strcmp(ptr
, "Filesystem") == 0)
109 ptr
= strtok(NULL
, WS
);
111 ptr
= strtok(NULL
, WS
);
113 ptr
= strtok(NULL
, WS
);
115 ptr
= strtok(NULL
, WS
);
117 path
= strtok(NULL
, WS
);
134 do_cleanup(const char *path
)
136 struct hammer_ioc_pseudofs_rw pfs
;
137 union hammer_ioc_mrecord_any mrec_tmp
;
138 char *snapshots_path
;
148 struct didpfs
*didpfs
;
149 int snapshots_disabled
= 0;
150 int prune_warning
= 0;
154 bzero(&pfs
, sizeof(pfs
));
155 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
156 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
157 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
160 printf("cleanup %-20s -", path
);
161 fd
= open(path
, O_RDONLY
);
163 printf(" unable to access directory: %s\n", strerror(errno
));
166 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
167 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
171 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
172 printf(" unrecognized HAMMER version\n");
177 * Make sure we have not already handled this PFS. Several nullfs
178 * mounts might alias the same PFS.
180 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
181 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
182 printf(" PFS #%d already handled\n", pfs
.pfs_id
);
186 didpfs
= malloc(sizeof(*didpfs
));
187 didpfs
->next
= FirstPFS
;
189 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
192 * Figure out where the snapshot directory is.
194 if (mrec_tmp
.pfs
.pfsd
.snapshots
[0] == '/') {
195 asprintf(&snapshots_path
, "%s", mrec_tmp
.pfs
.pfsd
.snapshots
);
196 } else if (mrec_tmp
.pfs
.pfsd
.snapshots
[0]) {
197 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
199 } else if (mrec_tmp
.pfs
.pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
200 printf(" WARNING: must configure snapshot dir for PFS slave\n");
201 printf("\tWe suggest <fs>/var/slaves/<name> where "
202 "<fs> is the base HAMMER fs\n");
203 printf("\tcontaining the slave\n");
206 asprintf(&snapshots_path
,
207 "%s%ssnapshots", path
, dividing_slash(path
));
211 * Create a snapshot directory if necessary, and a config file if
214 if (stat(snapshots_path
, &st
) < 0) {
215 if (mkdir(snapshots_path
, 0755) != 0) {
216 free(snapshots_path
);
217 printf(" unable to create snapshot dir \"%s\": %s\n",
218 snapshots_path
, strerror(errno
));
222 asprintf(&config_path
, "%s/config", snapshots_path
);
223 if ((fp
= fopen(config_path
, "r")) == NULL
) {
224 fp
= fopen(config_path
, "w");
226 printf(" cannot create %s: %s\n",
227 config_path
, strerror(errno
));
230 if (strcmp(path
, "/tmp") == 0 ||
231 strcmp(path
, "/var/tmp") == 0 ||
232 strcmp(path
, "/usr/obj") == 0) {
233 fprintf(fp
, "snapshots 0d 60d\n");
235 fprintf(fp
, "snapshots 1d 60d\n");
242 fp
= fopen(config_path
, "r");
245 printf(" cannot access %s: %s\n",
246 config_path
, strerror(errno
));
250 printf(" handle PFS #%d using %s\n", pfs
.pfs_id
, snapshots_path
);
253 * Process the config file
255 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
256 cmd
= strtok(buf
, WS
);
259 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
260 arg1
= strtosecs(ptr
);
261 if ((ptr
= strtok(NULL
, WS
)) != NULL
)
262 arg2
= strtosecs(ptr
);
265 printf("%20s - ", cmd
);
269 printf("disabled\n");
270 if (strcmp(cmd
, "snapshots") == 0) {
271 if (check_softlinks(snapshots_path
))
274 snapshots_disabled
= 1;
280 if (strcmp(cmd
, "snapshots") == 0) {
281 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
283 cleanup_softlinks(path
, snapshots_path
, arg2
);
284 r
= cleanup_snapshots(path
, snapshots_path
,
289 } else if (strcmp(cmd
, "prune") == 0) {
290 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
292 printf("run - WARNING snapshot softlinks present "
293 "but snapshots disabled\n");
296 r
= cleanup_prune(path
, snapshots_path
,
297 arg1
, arg2
, snapshots_disabled
);
301 } else if (strcmp(cmd
, "reblock") == 0) {
302 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
307 r
= cleanup_reblock(path
, snapshots_path
,
312 } else if (strcmp(cmd
, "recopy") == 0) {
313 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
318 r
= cleanup_recopy(path
, snapshots_path
,
324 printf("unknown directive\n");
328 save_period(snapshots_path
, cmd
, savet
);
340 val
= strtol(ptr
, &ptr
, 0);
354 errx(1, "illegal suffix converting %s\n", ptr
);
361 dividing_slash(const char *path
)
363 int len
= strlen(path
);
364 if (len
&& path
[len
-1] == '/')
371 * Check whether the desired period has elapsed since the last successful
372 * run. The run may take a while and cross a boundary so we remember the
373 * current time_t so we can save it later on.
375 * Periods in minutes, hours, or days are assumed to have been crossed
376 * if the local time crosses a minute, hour, or day boundary regardless
377 * of how close the last operation actually was.
380 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
391 localtime_r(savep
, &tp1
);
394 * Retrieve the start time of the last successful operation.
396 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
397 fp
= fopen(check_path
, "r");
401 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
407 lastt
= strtol(buf
, NULL
, 0);
408 localtime_r(&lastt
, &tp2
);
411 * Normalize the times. e.g. if asked to do something on a 1-day
412 * interval the operation will be performed as soon as the day
413 * turns over relative to the previous operation, even if the previous
414 * operation ran a few seconds ago just before midnight.
416 if (arg1
% 60 == 0) {
420 if (arg1
% (60 * 60) == 0) {
424 if (arg1
% (24 * 60 * 60) == 0) {
429 baset
= mktime(&tp1
);
430 lastt
= mktime(&tp2
);
433 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
436 if ((int)(baset
- lastt
) >= arg1
)
442 * Store the start time of the last successful operation.
445 save_period(const char *snapshots_path
, const char *cmd
,
452 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
453 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
454 fp
= fopen(ncheck_path
, "w");
455 fprintf(fp
, "0x%08llx\n", (long long)savet
);
457 rename(ncheck_path
, ocheck_path
);
462 * Simply count the number of softlinks in the snapshots dir
465 check_softlinks(const char *snapshots_path
)
473 if ((dir
= opendir(snapshots_path
)) != NULL
) {
474 while ((den
= readdir(dir
)) != NULL
) {
475 if (den
->d_name
[0] == '.')
477 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
478 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
488 * Clean up expired softlinks in the snapshots dir
491 cleanup_softlinks(const char *path __unused
, const char *snapshots_path
, int arg2
)
498 if ((dir
= opendir(snapshots_path
)) != NULL
) {
499 while ((den
= readdir(dir
)) != NULL
) {
500 if (den
->d_name
[0] == '.')
502 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
503 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
504 strncmp(den
->d_name
, "snap-", 5) == 0) {
505 if (check_expired(den
->d_name
, arg2
)) {
507 printf(" expire %s\n",
520 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
521 * expiration in seconds (arg2) and return non-zero if the softlink
525 check_expired(const char *fpath
, int arg2
)
536 r
= sscanf(fpath
, "snap-%4d%2d%2d-%2d%2d",
537 &year
, &month
, &day
, &hour
, &minute
);
539 bzero(&tm
, sizeof(tm
));
544 tm
.tm_mon
= month
- 1;
545 tm
.tm_year
= year
- 1900;
546 t
= time(NULL
) - mktime(&tm
);
557 cleanup_snapshots(const char *path __unused
, const char *snapshots_path
,
558 int arg1 __unused
, int arg2 __unused
)
562 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
567 cleanup_prune(const char *path __unused
, const char *snapshots_path
,
568 int arg1 __unused
, int arg2
, int snapshots_disabled
)
571 * If snapshots have been disabled run prune-everything instead
574 if (snapshots_disabled
&& arg2
) {
575 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
576 snapshots_path
, arg2
, path
);
577 } else if (snapshots_disabled
) {
578 runcmd(NULL
, "hammer prune-everything %s", path
);
580 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
581 snapshots_path
, arg2
, snapshots_path
);
583 runcmd(NULL
, "hammer prune %s", snapshots_path
);
589 cleanup_reblock(const char *path
, const char *snapshots_path
,
590 int arg1 __unused
, int arg2
)
592 if (VerboseOpt
== 0) {
597 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s 95",
598 snapshots_path
, arg2
, path
);
599 if (VerboseOpt
== 0) {
604 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s 95",
605 snapshots_path
, arg2
, path
);
606 if (VerboseOpt
== 0) {
611 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
612 snapshots_path
, arg2
, path
);
619 cleanup_recopy(const char *path
, const char *snapshots_path
,
620 int arg1 __unused
, int arg2
)
622 if (VerboseOpt
== 0) {
627 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
628 snapshots_path
, arg2
, path
);
629 if (VerboseOpt
== 0) {
634 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
635 snapshots_path
, arg2
, path
);
636 if (VerboseOpt
== 0) {
641 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
642 snapshots_path
, arg2
, path
);
650 runcmd(int *resp
, const char *ctl
, ...)
662 * Generate the command
665 vasprintf(&cmd
, ctl
, va
);
668 printf(" %s\n", cmd
);
671 * Break us down into arguments. We do not just use system() here
672 * because it blocks SIGINT and friends.
676 av
= malloc(sizeof(char *) * nmax
);
678 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
681 av
= realloc(av
, sizeof(char *) * nmax
);
690 if ((pid
= fork()) == 0) {
691 if (VerboseOpt
< 2) {
692 int fd
= open("/dev/null", O_RDWR
);
698 } else if (pid
< 0) {
702 while (waitpid(pid
, &status
, 0) != pid
)
704 res
= WEXITSTATUS(status
);