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.3 2008/09/20 07:08:06 dillon 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
);
73 static int cleanup_snapshots(const char *path
, const char *snapshots_path
,
75 static int cleanup_prune(const char *path
, const char *snapshots_path
,
76 int arg1
, int arg2
, int snapshots_disabled
);
77 static int cleanup_reblock(const char *path
, const char *snapshots_path
,
79 static int cleanup_recopy(const char *path
, const char *snapshots_path
,
82 static void runcmd(int *resp
, const char *ctl
, ...);
86 struct didpfs
*FirstPFS
;
89 hammer_cmd_cleanup(char **av
, int ac
)
98 fp
= popen("df -t hammer,null", "r");
100 errx(1, "hammer cleanup: 'df' failed");
101 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
102 ptr
= strtok(buf
, WS
);
103 if (ptr
&& strcmp(ptr
, "Filesystem") == 0)
106 ptr
= strtok(NULL
, WS
);
108 ptr
= strtok(NULL
, WS
);
110 ptr
= strtok(NULL
, WS
);
112 ptr
= strtok(NULL
, WS
);
114 path
= strtok(NULL
, WS
);
131 do_cleanup(const char *path
)
133 struct hammer_ioc_pseudofs_rw pfs
;
134 union hammer_ioc_mrecord_any mrec_tmp
;
135 char *snapshots_path
;
145 struct didpfs
*didpfs
;
146 int snapshots_disabled
= 0;
147 int prune_warning
= 0;
151 bzero(&pfs
, sizeof(pfs
));
152 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
153 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
154 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
157 printf("cleanup %-20s -", path
);
158 fd
= open(path
, O_RDONLY
);
160 printf(" unable to access directory: %s\n", strerror(errno
));
163 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
164 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
168 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
169 printf(" unrecognized HAMMER version\n");
174 * Make sure we have not already handled this PFS. Several nullfs
175 * mounts might alias the same PFS.
177 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
178 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
179 printf(" pfs_id %d already handled\n", pfs
.pfs_id
);
183 didpfs
= malloc(sizeof(*didpfs
));
184 didpfs
->next
= FirstPFS
;
186 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
189 * Create a snapshot directory if necessary, and a config file if
192 asprintf(&snapshots_path
, "%s%ssnapshots", path
, dividing_slash(path
));
193 if (stat(snapshots_path
, &st
) < 0) {
194 if (mkdir(snapshots_path
, 0755) != 0) {
195 free(snapshots_path
);
196 printf(" unable to create snapshot dir: %s\n",
201 asprintf(&config_path
, "%s/config", snapshots_path
);
202 if ((fp
= fopen(config_path
, "r")) == NULL
) {
203 fp
= fopen(config_path
, "w");
205 printf(" cannot create %s: %s\n",
206 config_path
, strerror(errno
));
209 if (strcmp(path
, "/tmp") == 0 ||
210 strcmp(path
, "/var/tmp") == 0 ||
211 strcmp(path
, "/usr/obj") == 0) {
212 fprintf(fp
, "snapshots 0d 60d\n");
214 fprintf(fp
, "snapshots 1d 60d\n");
221 fp
= fopen(config_path
, "r");
224 printf(" cannot access %s: %s\n",
225 config_path
, strerror(errno
));
229 printf(" processing PFS #%d\n", pfs
.pfs_id
);
232 * Process the config file
234 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
235 cmd
= strtok(buf
, WS
);
238 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
239 arg1
= strtosecs(ptr
);
240 if ((ptr
= strtok(NULL
, WS
)) != NULL
)
241 arg2
= strtosecs(ptr
);
244 printf("%20s - ", cmd
);
248 printf("disabled\n");
249 if (strcmp(cmd
, "snapshots") == 0) {
250 if (check_softlinks(snapshots_path
))
253 snapshots_disabled
= 1;
259 if (strcmp(cmd
, "snapshots") == 0) {
260 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
262 r
= cleanup_snapshots(path
, snapshots_path
,
267 } else if (strcmp(cmd
, "prune") == 0) {
268 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
270 printf("run - WARNING snapshot softlinks present but snapshots disabled\n");
273 r
= cleanup_prune(path
, snapshots_path
,
274 arg1
, arg2
, snapshots_disabled
);
278 } else if (strcmp(cmd
, "reblock") == 0) {
279 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
284 r
= cleanup_reblock(path
, snapshots_path
,
289 } else if (strcmp(cmd
, "recopy") == 0) {
290 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
295 r
= cleanup_recopy(path
, snapshots_path
,
301 printf("unknown directive\n");
305 save_period(snapshots_path
, cmd
, savet
);
317 val
= strtol(ptr
, &ptr
, 0);
331 errx(1, "illegal suffix converting %s\n", ptr
);
338 dividing_slash(const char *path
)
340 int len
= strlen(path
);
341 if (len
&& path
[len
-1] == '/')
348 * Check whether the desired period has elapsed since the last successful
349 * run. The run may take a while and cross a boundary so we remember the
350 * current time_t so we can save it later on.
352 * Periods in minutes, hours, or days are assumed to have been crossed
353 * if the local time crosses a minute, hour, or day boundary regardless
354 * of how close the last operation actually was.
357 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
368 localtime_r(savep
, &tp1
);
371 * Retrieve the start time of the last successful operation.
373 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
374 fp
= fopen(check_path
, "r");
378 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
384 lastt
= strtol(buf
, NULL
, 0);
385 localtime_r(&lastt
, &tp2
);
388 * Normalize the times. e.g. if asked to do something on a 1-day
389 * interval the operation will be performed as soon as the day
390 * turns over relative to the previous operation, even if the previous
391 * operation ran a few seconds ago just before midnight.
393 if (arg1
% 60 == 0) {
397 if (arg1
% (60 * 60) == 0) {
401 if (arg1
% (24 * 60 * 60) == 0) {
406 baset
= mktime(&tp1
);
407 lastt
= mktime(&tp2
);
410 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
413 if ((int)(baset
- lastt
) >= arg1
)
419 * Store the start time of the last successful operation.
422 save_period(const char *snapshots_path
, const char *cmd
,
429 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
430 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
431 fp
= fopen(ncheck_path
, "w");
432 fprintf(fp
, "0x%08llx\n", (long long)savet
);
434 rename(ncheck_path
, ocheck_path
);
439 check_softlinks(const char *snapshots_path
)
448 * Force snapshots_disabled to 0 if the snapshots directory
449 * contains softlinks.
451 if ((dir
= opendir(snapshots_path
)) != NULL
) {
452 while ((den
= readdir(dir
)) != NULL
) {
453 if (den
->d_name
[0] == '.')
455 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
456 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
469 cleanup_snapshots(const char *path __unused
, const char *snapshots_path
,
470 int arg1 __unused
, int arg2 __unused
)
474 runcmd(&r
, "hammer snapshot %s", snapshots_path
);
479 cleanup_prune(const char *path __unused
, const char *snapshots_path
,
480 int arg1 __unused
, int arg2
, int snapshots_disabled
)
483 * If snapshots have been disabled run prune-everything instead
486 if (snapshots_disabled
&& arg2
) {
487 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
488 snapshots_path
, arg2
, path
);
489 } else if (snapshots_disabled
) {
490 runcmd(NULL
, "hammer prune-everything %s", path
);
492 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
493 snapshots_path
, arg2
, snapshots_path
);
495 runcmd(NULL
, "hammer prune %s", snapshots_path
);
501 cleanup_reblock(const char *path
, const char *snapshots_path
,
502 int arg1 __unused
, int arg2
)
504 if (VerboseOpt
== 0) {
509 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s 95",
510 snapshots_path
, arg2
, path
);
511 if (VerboseOpt
== 0) {
516 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s 95",
517 snapshots_path
, arg2
, path
);
518 if (VerboseOpt
== 0) {
523 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
524 snapshots_path
, arg2
, path
);
531 cleanup_recopy(const char *path
, const char *snapshots_path
,
532 int arg1 __unused
, int arg2
)
534 if (VerboseOpt
== 0) {
539 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
540 snapshots_path
, arg2
, path
);
541 if (VerboseOpt
== 0) {
546 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
547 snapshots_path
, arg2
, path
);
548 if (VerboseOpt
== 0) {
553 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
554 snapshots_path
, arg2
, path
);
562 runcmd(int *resp
, const char *ctl
, ...)
574 * Generate the command
577 vasprintf(&cmd
, ctl
, va
);
580 printf(" %s\n", cmd
);
583 * Break us down into arguments. We do not just use system() here
584 * because it blocks SIGINT and friends.
588 av
= malloc(sizeof(char *) * nmax
);
590 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
593 av
= realloc(av
, sizeof(char *) * nmax
);
602 if ((pid
= fork()) == 0) {
603 if (VerboseOpt
< 2) {
604 int fd
= open("/dev/null", O_RDWR
);
610 } else if (pid
< 0) {
614 while (waitpid(pid
, &status
, 0) != pid
)
616 res
= WEXITSTATUS(status
);