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 specific HAMMER filesystems or all HAMMER filesystems.
39 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
42 * Each HAMMER filesystem may contain a configuration file. If no
43 * configuration file is present one will be created with the following
46 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj)
52 * All hammer commands create and maintain cycle files in the snapshots
55 * For HAMMER version 2- the configuration file is a named 'config' in
56 * the snapshots directory, which defaults to <pfs>/snapshots.
57 * For HAMMER version 3+ the configuration file is saved in filesystem
58 * meta-data. The snapshots directory defaults to /var/hammer/<pfs>
59 * (/var/hammer/root for root mount).
69 static void do_cleanup(const char *path
);
70 static void config_init(const char *path
, struct hammer_ioc_config
*config
);
71 static void migrate_config(FILE *fp
, struct hammer_ioc_config
*config
);
72 static void migrate_snapshots(int fd
, const char *snapshots_path
);
73 static void migrate_one_snapshot(int fd
, const char *fpath
,
74 struct hammer_ioc_snapshot
*snapshot
);
75 static int strtosecs(char *ptr
);
76 static const char *dividing_slash(const char *path
);
77 static int check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
79 static void save_period(const char *snapshots_path
, const char *cmd
,
81 static int check_softlinks(int fd
, int new_config
, const char *snapshots_path
);
82 static void cleanup_softlinks(int fd
, int new_config
,
83 const char *snapshots_path
, int arg2
, char *arg3
);
84 static void delete_snapshots(int fd
, struct hammer_ioc_snapshot
*dsnapshot
);
85 static int check_expired(const char *fpath
, int arg2
);
87 static int create_snapshot(const char *path
, const char *snapshots_path
);
88 static int cleanup_rebalance(const char *path
, const char *snapshots_path
,
90 static int cleanup_prune(const char *path
, const char *snapshots_path
,
91 int arg1
, int arg2
, int snapshots_disabled
);
92 static int cleanup_reblock(const char *path
, const char *snapshots_path
,
94 static int cleanup_recopy(const char *path
, const char *snapshots_path
,
97 static void runcmd(int *resp
, const char *ctl
, ...);
100 * WARNING: Do not make the SNAPSHOTS_BASE "/var/snapshots" because
101 * it will interfere with the older HAMMER VERS < 3 snapshots directory
104 #define SNAPSHOTS_BASE "/var/hammer" /* HAMMER VERS >= 3 */
107 struct didpfs
*FirstPFS
;
110 hammer_cmd_cleanup(char **av
, int ac
)
112 char *fstype
, *fs
, *path
;
113 struct statfs
*stfsbuf
;
118 mntsize
= getmntinfo(&stfsbuf
, MNT_NOWAIT
);
120 for (i
=0; i
< mntsize
; i
++) {
122 * We will cleanup in the case fstype is hammer.
123 * If we have null-mounted PFS, we check the
124 * mount source. If it looks like a PFS, we
125 * proceed to cleanup also.
127 fstype
= stfsbuf
[i
].f_fstypename
;
128 fs
= stfsbuf
[i
].f_mntfromname
;
129 if ((strcmp(fstype
, "hammer") == 0) ||
130 ((strcmp(fstype
, "null") == 0) &&
131 (strstr(fs
, "/@@0x") != NULL
||
132 strstr(fs
, "/@@-1") != NULL
))) {
133 path
= stfsbuf
[i
].f_mntonname
;
150 do_cleanup(const char *path
)
152 struct hammer_ioc_pseudofs_rw pfs
;
153 struct hammer_ioc_config config
;
154 struct hammer_ioc_version version
;
155 union hammer_ioc_mrecord_any mrec_tmp
;
156 char *snapshots_path
= NULL
;
169 struct didpfs
*didpfs
;
170 int snapshots_disabled
= 0;
171 int prune_warning
= 0;
173 int snapshots_from_pfs
= 0;
178 bzero(&pfs
, sizeof(pfs
));
179 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
180 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
181 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
184 printf("cleanup %-20s -", path
);
185 fd
= open(path
, O_RDONLY
);
187 printf(" unable to access directory: %s\n", strerror(errno
));
190 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
191 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
195 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
196 printf(" unrecognized HAMMER version\n");
200 bzero(&version
, sizeof(version
));
201 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
202 printf(" HAMMER filesystem but couldn't retrieve version!\n");
207 bzero(&config
, sizeof(config
));
208 if (version
.cur_version
>= 3) {
209 if (ioctl(fd
, HAMMERIOC_GET_CONFIG
, &config
) == 0 &&
210 config
.head
.error
== 0) {
216 * Make sure we have not already handled this PFS. Several nullfs
217 * mounts might alias the same PFS.
219 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
220 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
221 printf(" PFS #%d already handled\n", pfs
.pfs_id
);
226 didpfs
= malloc(sizeof(*didpfs
));
227 didpfs
->next
= FirstPFS
;
229 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
232 * Calculate the old snapshots directory for HAMMER VERSION < 3
234 * If the directory is explicitly specified in the PFS config
235 * we flag it and will not migrate it later.
237 if (mrec_tmp
.pfs
.pfsd
.snapshots
[0] == '/') {
238 asprintf(&snapshots_path
, "%s", mrec_tmp
.pfs
.pfsd
.snapshots
);
239 snapshots_from_pfs
= 1;
240 } else if (mrec_tmp
.pfs
.pfsd
.snapshots
[0]) {
241 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
244 } else if (mrec_tmp
.pfs
.pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
245 if (version
.cur_version
< 3) {
246 printf(" WARNING: must configure snapshot dir for PFS slave\n");
247 printf("\tWe suggest <fs>/var/slaves/<name> where "
248 "<fs> is the base HAMMER fs\n");
249 printf("\tcontaining the slave\n");
254 asprintf(&snapshots_path
,
255 "%s%ssnapshots", path
, dividing_slash(path
));
259 * Check for old-style config file
261 if (snapshots_path
) {
262 asprintf(&config_path
, "%s/config", snapshots_path
);
263 fp
= fopen(config_path
, "r");
267 * Handle upgrades to hammer version 3, move the config
268 * file into meta-data.
270 * For the old config read the file into the config structure,
271 * we will parse it out of the config structure regardless.
273 if (version
.cur_version
>= 3) {
275 printf("(migrating) ");
277 migrate_config(fp
, &config
);
278 migrate_snapshots(fd
, snapshots_path
);
280 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
281 printf(" cannot init meta-data config!\n");
286 } else if (new_config
== 0) {
287 config_init(path
, &config
);
288 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
289 printf(" cannot init meta-data config!\n");
297 * Create missing snapshots directory for HAMMER VERSION < 3
299 if (stat(snapshots_path
, &st
) < 0) {
300 if (mkdir(snapshots_path
, 0755) != 0) {
301 free(snapshots_path
);
302 printf(" unable to create snapshot dir \"%s\": %s\n",
303 snapshots_path
, strerror(errno
));
310 * Create missing config file for HAMMER VERSION < 3
313 config_init(path
, &config
);
314 fp
= fopen(config_path
, "w");
316 fwrite(config
.config
.text
, 1,
317 strlen(config
.config
.text
), fp
);
321 migrate_config(fp
, &config
);
327 * If snapshots_from_pfs is not set we calculate the new snapshots
328 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
329 * the old snapshots directory over.
331 * People who have set an explicit snapshots directory will have
332 * to migrate the data manually into /var/hammer, or not bother at
333 * all. People running slaves may wish to migrate it and then
334 * clear the snapshots specification in the PFS config for the
337 if (new_config
&& snapshots_from_pfs
== 0) {
340 assert(path
[0] == '/');
341 if (strcmp(path
, "/") == 0)
342 asprintf(&npath
, "%s/root", SNAPSHOTS_BASE
);
344 asprintf(&npath
, "%s/%s", SNAPSHOTS_BASE
, path
+ 1);
345 if (snapshots_path
) {
346 if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
347 if (stat(snapshots_path
, &st
) < 0 && errno
== ENOENT
) {
348 printf(" HAMMER UPGRADE: Creating snapshots\n"
349 "\tCreating snapshots in %s\n",
351 runcmd(&r
, "mkdir -p %s", npath
);
353 printf(" HAMMER UPGRADE: Moving snapshots\n"
354 "\tMoving snapshots from %s to %s\n",
355 snapshots_path
, npath
);
356 runcmd(&r
, "mkdir -p %s", npath
);
357 runcmd(&r
, "cpdup %s %s", snapshots_path
, npath
);
359 printf("Unable to move snapshots directory!\n");
360 printf("Please fix this critical error.\n");
361 printf("Aborting cleanup of %s\n", path
);
365 runcmd(&r
, "rm -rf %s", snapshots_path
);
368 free(snapshots_path
);
369 } else if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
370 runcmd(&r
, "mkdir -p %s", npath
);
372 snapshots_path
= npath
;
376 * Lock the PFS. fd is the base directory of the mounted PFS.
378 if (flock(fd
, LOCK_EX
|LOCK_NB
) == -1) {
379 if (errno
== EWOULDBLOCK
)
380 printf(" PFS #%d locked by other process\n", pfs
.pfs_id
);
382 printf(" can not lock %s: %s\n", config_path
, strerror(errno
));
387 printf(" handle PFS #%d using %s\n", pfs
.pfs_id
, snapshots_path
);
390 * Process the config file
392 cbase
= config
.config
.text
;
394 while ((cptr
= strchr(cbase
, '\n')) != NULL
) {
395 bcopy(cbase
, buf
, cptr
- cbase
);
396 buf
[cptr
- cbase
] = 0;
399 cmd
= strtok(buf
, WS
);
400 if (cmd
== NULL
|| cmd
[0] == '#')
406 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
407 arg1
= strtosecs(ptr
);
408 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
409 arg2
= strtosecs(ptr
);
410 arg3
= strtok(NULL
, WS
);
414 printf("%20s - ", cmd
);
418 if (strcmp(cmd
, "snapshots") == 0) {
421 check_softlinks(fd
, new_config
,
423 printf("only removing old snapshots\n");
425 cleanup_softlinks(fd
, new_config
,
429 printf("disabled\n");
430 snapshots_disabled
= 1;
433 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
435 cleanup_softlinks(fd
, new_config
,
438 r
= create_snapshot(path
, snapshots_path
);
442 } else if (arg1
== 0) {
444 * The commands following this check can't handle
445 * a period of 0, so call the feature disabled and
446 * ignore the directive.
448 printf("disabled\n");
449 } else if (strcmp(cmd
, "prune") == 0) {
450 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
452 printf("run - WARNING snapshot "
454 "but snapshots disabled\n");
458 r
= cleanup_prune(path
, snapshots_path
,
459 arg1
, arg2
, snapshots_disabled
);
463 } else if (strcmp(cmd
, "rebalance") == 0) {
465 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
470 r
= cleanup_rebalance(path
, snapshots_path
,
475 } else if (strcmp(cmd
, "reblock") == 0) {
476 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
481 r
= cleanup_reblock(path
, snapshots_path
,
486 } else if (strcmp(cmd
, "recopy") == 0) {
487 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
492 r
= cleanup_recopy(path
, snapshots_path
,
498 printf("unknown directive\n");
502 save_period(snapshots_path
, cmd
, savet
);
506 * Add new rebalance feature if the config doesn't have it.
507 * (old style config only).
509 if (new_config
== 0 && found_rebal
== 0) {
510 if ((fp
= fopen(config_path
, "r+")) != NULL
) {
512 fprintf(fp
, "rebalance 1d 5m\n");
518 * Cleanup, and delay a little
525 * Initialize new config data (new or old style)
528 config_init(const char *path
, struct hammer_ioc_config
*config
)
530 const char *snapshots
;
532 if (strcmp(path
, "/tmp") == 0 ||
533 strcmp(path
, "/var/tmp") == 0 ||
534 strcmp(path
, "/usr/obj") == 0) {
535 snapshots
= "snapshots 0d 0d\n";
537 snapshots
= "snapshots 1d 60d\n";
539 bzero(config
->config
.text
, sizeof(config
->config
.text
));
540 snprintf(config
->config
.text
, sizeof(config
->config
.text
) - 1, "%s%s",
549 * Migrate configuration data from the old snapshots/config
550 * file to the new meta-data format.
553 migrate_config(FILE *fp
, struct hammer_ioc_config
*config
)
557 n
= fread(config
->config
.text
, 1, sizeof(config
->config
.text
) - 1, fp
);
559 bzero(config
->config
.text
+ n
, sizeof(config
->config
.text
) - n
);
563 * Migrate snapshot softlinks in the snapshots directory to the
564 * new meta-data format. The softlinks are left intact, but
565 * this way the pruning code won't lose track of them if you
566 * happen to blow away the snapshots directory.
569 migrate_snapshots(int fd
, const char *snapshots_path
)
571 struct hammer_ioc_snapshot snapshot
;
577 bzero(&snapshot
, sizeof(snapshot
));
579 if ((dir
= opendir(snapshots_path
)) != NULL
) {
580 while ((den
= readdir(dir
)) != NULL
) {
581 if (den
->d_name
[0] == '.')
583 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
584 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
585 migrate_one_snapshot(fd
, fpath
, &snapshot
);
591 migrate_one_snapshot(fd
, NULL
, &snapshot
);
596 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
597 * otherwise it is flushed when it fills up.
600 migrate_one_snapshot(int fd
, const char *fpath
,
601 struct hammer_ioc_snapshot
*snapshot
)
604 struct hammer_snapshot_data
*snap
;
618 tid
= (hammer_tid_t
)(int64_t)-1;
621 while (*ptr
&& *ptr
!= '-' && *ptr
!= '.')
625 r
= sscanf(ptr
, "%4d%2d%2d-%2d%2d",
626 &year
, &month
, &day
, &hour
, &minute
);
629 bzero(&tm
, sizeof(tm
));
634 tm
.tm_mon
= month
- 1;
635 tm
.tm_year
= year
- 1900;
638 bzero(linkbuf
, sizeof(linkbuf
));
639 if (readlink(fpath
, linkbuf
, sizeof(linkbuf
) - 1) > 0 &&
640 (ptr
= strrchr(linkbuf
, '@')) != NULL
&&
641 ptr
> linkbuf
&& ptr
[-1] == '@') {
642 tid
= strtoull(ptr
+ 1, NULL
, 16);
644 if (t
!= (time_t)-1 && tid
!= (hammer_tid_t
)(int64_t)-1) {
645 snap
= &snapshot
->snaps
[snapshot
->count
];
646 bzero(snap
, sizeof(*snap
));
648 snap
->ts
= (u_int64_t
)t
* 1000000ULL;
649 snprintf(snap
->label
, sizeof(snap
->label
),
655 if ((fpath
== NULL
&& snapshot
->count
) ||
656 snapshot
->count
== HAMMER_SNAPS_PER_IOCTL
) {
657 printf(" (%d snapshots)", snapshot
->count
);
659 if (ioctl(fd
, HAMMERIOC_ADD_SNAPSHOT
, snapshot
) < 0) {
660 printf(" Ioctl to migrate snapshots failed: %s\n",
662 } else if (snapshot
->head
.error
== EALREADY
) {
665 } else if (snapshot
->head
.error
) {
666 printf(" Ioctl to migrate snapshots failed: %s\n",
667 strerror(snapshot
->head
.error
));
669 printf("index %d\n", snapshot
->index
);
672 snapshot
->head
.error
= 0;
682 val
= strtol(ptr
, &ptr
, 0);
696 errx(1, "illegal suffix converting %s\n", ptr
);
703 dividing_slash(const char *path
)
705 int len
= strlen(path
);
706 if (len
&& path
[len
-1] == '/')
713 * Check whether the desired period has elapsed since the last successful
714 * run. The run may take a while and cross a boundary so we remember the
715 * current time_t so we can save it later on.
717 * Periods in minutes, hours, or days are assumed to have been crossed
718 * if the local time crosses a minute, hour, or day boundary regardless
719 * of how close the last operation actually was.
722 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
733 localtime_r(savep
, &tp1
);
736 * Retrieve the start time of the last successful operation.
738 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
739 fp
= fopen(check_path
, "r");
743 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
749 lastt
= strtol(buf
, NULL
, 0);
750 localtime_r(&lastt
, &tp2
);
753 * Normalize the times. e.g. if asked to do something on a 1-day
754 * interval the operation will be performed as soon as the day
755 * turns over relative to the previous operation, even if the previous
756 * operation ran a few seconds ago just before midnight.
758 if (arg1
% 60 == 0) {
762 if (arg1
% (60 * 60) == 0) {
766 if (arg1
% (24 * 60 * 60) == 0) {
771 baset
= mktime(&tp1
);
772 lastt
= mktime(&tp2
);
775 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
778 if ((int)(baset
- lastt
) >= arg1
)
784 * Store the start time of the last successful operation.
787 save_period(const char *snapshots_path
, const char *cmd
,
794 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
795 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
796 fp
= fopen(ncheck_path
, "w");
798 fprintf(fp
, "0x%08llx\n", (long long)savet
);
800 rename(ncheck_path
, ocheck_path
);
803 fprintf(stderr
, "hammer: Unable to create period-file %s: %s\n",
804 ncheck_path
, strerror(errno
));
809 * Simply count the number of softlinks in the snapshots dir
812 check_softlinks(int fd
, int new_config
, const char *snapshots_path
)
821 * Old-style softlink-based snapshots
823 if ((dir
= opendir(snapshots_path
)) != NULL
) {
824 while ((den
= readdir(dir
)) != NULL
) {
825 if (den
->d_name
[0] == '.')
827 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
828 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
836 * New-style snapshots are stored as filesystem meta-data,
840 struct hammer_ioc_snapshot snapshot
;
842 bzero(&snapshot
, sizeof(snapshot
));
844 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
845 err(2, "hammer cleanup: check_softlink "
849 res
+= snapshot
.count
;
850 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
856 * Clean up expired softlinks in the snapshots dir
859 cleanup_softlinks(int fd
, int new_config
,
860 const char *snapshots_path
, int arg2
, char *arg3
)
868 if (arg3
!= NULL
&& strstr(arg3
, "any") != NULL
)
871 if ((dir
= opendir(snapshots_path
)) != NULL
) {
872 while ((den
= readdir(dir
)) != NULL
) {
873 if (den
->d_name
[0] == '.')
875 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
876 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
877 (anylink
|| strncmp(den
->d_name
, "snap-", 5) == 0)
879 if (check_expired(den
->d_name
, arg2
)) {
881 printf(" expire %s\n",
893 * New-style snapshots are stored as filesystem meta-data,
897 struct hammer_ioc_snapshot snapshot
;
898 struct hammer_ioc_snapshot dsnapshot
;
899 struct hammer_snapshot_data
*snap
;
906 bzero(&snapshot
, sizeof(snapshot
));
907 bzero(&dsnapshot
, sizeof(dsnapshot
));
909 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
910 err(2, "hammer cleanup: check_softlink "
914 for (i
= 0; i
< snapshot
.count
; ++i
) {
915 snap
= &snapshot
.snaps
[i
];
916 t
= snap
->ts
/ 1000000ULL;
918 if ((int)dt
> arg2
|| snap
->tid
== 0) {
919 dsnapshot
.snaps
[dsnapshot
.count
++] =
922 if ((int)dt
> arg2
&& VerboseOpt
) {
924 strftime(snapts
, sizeof(snapts
),
925 "%Y-%m-%d %H:%M:%S %Z", tp
);
926 printf(" expire 0x%016jx %s %s\n",
927 (uintmax_t)snap
->tid
,
931 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
)
932 delete_snapshots(fd
, &dsnapshot
);
934 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
937 delete_snapshots(fd
, &dsnapshot
);
942 delete_snapshots(int fd
, struct hammer_ioc_snapshot
*dsnapshot
)
945 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, dsnapshot
) < 0) {
946 printf(" Ioctl to delete snapshots failed: %s\n",
950 if (dsnapshot
->head
.error
) {
951 printf(" Ioctl to delete snapshots failed at "
953 dsnapshot
->snaps
[dsnapshot
->index
].tid
,
954 strerror(dsnapshot
->head
.error
));
955 if (++dsnapshot
->index
< dsnapshot
->count
)
960 dsnapshot
->index
= 0;
961 dsnapshot
->count
= 0;
962 dsnapshot
->head
.error
= 0;
966 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
967 * expiration in seconds (arg2) and return non-zero if the softlink
971 check_expired(const char *fpath
, int arg2
)
982 while (*fpath
&& *fpath
!= '-' && *fpath
!= '.')
987 r
= sscanf(fpath
, "%4d%2d%2d-%2d%2d",
988 &year
, &month
, &day
, &hour
, &minute
);
991 bzero(&tm
, sizeof(tm
));
996 tm
.tm_mon
= month
- 1;
997 tm
.tm_year
= year
- 1900;
1012 create_snapshot(const char *path
, const char *snapshots_path
)
1016 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
1021 cleanup_prune(const char *path __unused
, const char *snapshots_path
,
1022 int arg1 __unused
, int arg2
, int snapshots_disabled
)
1025 * If snapshots have been disabled run prune-everything instead
1028 if (snapshots_disabled
&& arg2
) {
1029 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1030 snapshots_path
, arg2
, path
);
1031 } else if (snapshots_disabled
) {
1032 runcmd(NULL
, "hammer prune-everything %s", path
);
1034 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
1035 snapshots_path
, arg2
, snapshots_path
);
1037 runcmd(NULL
, "hammer prune %s", snapshots_path
);
1043 cleanup_rebalance(const char *path
, const char *snapshots_path
,
1044 int arg1 __unused
, int arg2
)
1046 if (VerboseOpt
== 0) {
1052 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1053 snapshots_path
, arg2
, path
);
1054 if (VerboseOpt
== 0) {
1058 if (VerboseOpt
== 0)
1064 cleanup_reblock(const char *path
, const char *snapshots_path
,
1065 int arg1 __unused
, int arg2
)
1067 if (VerboseOpt
== 0) {
1073 * When reblocking the B-Tree always reblock everything in normal
1077 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1078 snapshots_path
, arg2
, path
);
1079 if (VerboseOpt
== 0) {
1085 * When reblocking the inodes always reblock everything in normal
1089 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1090 snapshots_path
, arg2
, path
);
1091 if (VerboseOpt
== 0) {
1097 * When reblocking the directories always reblock everything in normal
1101 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1102 snapshots_path
, arg2
, path
);
1103 if (VerboseOpt
== 0) {
1109 * Do not reblock all the data in normal mode.
1112 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1113 snapshots_path
, arg2
, path
);
1114 if (VerboseOpt
== 0)
1120 cleanup_recopy(const char *path
, const char *snapshots_path
,
1121 int arg1 __unused
, int arg2
)
1123 if (VerboseOpt
== 0) {
1128 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1129 snapshots_path
, arg2
, path
);
1130 if (VerboseOpt
== 0) {
1135 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1136 snapshots_path
, arg2
, path
);
1137 if (VerboseOpt
== 0) {
1142 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1143 snapshots_path
, arg2
, path
);
1144 if (VerboseOpt
== 0) {
1149 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1150 snapshots_path
, arg2
, path
);
1151 if (VerboseOpt
== 0)
1158 runcmd(int *resp
, const char *ctl
, ...)
1170 * Generate the command
1173 vasprintf(&cmd
, ctl
, va
);
1176 printf(" %s\n", cmd
);
1179 * Break us down into arguments. We do not just use system() here
1180 * because it blocks SIGINT and friends.
1184 av
= malloc(sizeof(char *) * nmax
);
1186 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
1187 if (n
== nmax
- 1) {
1189 av
= realloc(av
, sizeof(char *) * nmax
);
1199 if ((pid
= fork()) == 0) {
1200 if (VerboseOpt
< 2) {
1201 int fd
= open("/dev/null", O_RDWR
);
1207 } else if (pid
< 0) {
1212 while (waitpid(pid
, &status
, 0) != pid
)
1214 res
= WEXITSTATUS(status
);