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)
54 * All hammer commands create and maintain cycle files in the snapshots
65 static void do_cleanup(const char *path
);
66 static void config_init(const char *path
, struct hammer_ioc_config
*config
);
67 static void migrate_config(FILE *fp
, struct hammer_ioc_config
*config
);
68 static void migrate_snapshots(int fd
, const char *snapshots_path
);
69 static void migrate_one_snapshot(int fd
, const char *fpath
,
70 struct hammer_ioc_snapshot
*snapshot
);
71 static int strtosecs(char *ptr
);
72 static const char *dividing_slash(const char *path
);
73 static int check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
75 static void save_period(const char *snapshots_path
, const char *cmd
,
77 static int check_softlinks(int fd
, int new_config
, const char *snapshots_path
);
78 static void cleanup_softlinks(int fd
, int new_config
,
79 const char *snapshots_path
, int arg2
, char *arg3
);
80 static int check_expired(const char *fpath
, int arg2
);
82 static int create_snapshot(const char *path
, const char *snapshots_path
);
83 static int cleanup_rebalance(const char *path
, const char *snapshots_path
,
85 static int cleanup_prune(const char *path
, const char *snapshots_path
,
86 int arg1
, int arg2
, int snapshots_disabled
);
87 static int cleanup_reblock(const char *path
, const char *snapshots_path
,
89 static int cleanup_recopy(const char *path
, const char *snapshots_path
,
92 static void runcmd(int *resp
, const char *ctl
, ...);
95 * WARNING: Do not make the SNAPSHOTS_BASE "/var/snapshots" because
96 * it will interfere with the older HAMMER VERS < 3 snapshots directory
99 #define SNAPSHOTS_BASE "/var/hammer" /* HAMMER VERS >= 3 */
102 struct didpfs
*FirstPFS
;
105 hammer_cmd_cleanup(char **av
, int ac
)
107 char *fstype
, *fs
, *path
;
108 struct statfs
*stfsbuf
;
113 mntsize
= getmntinfo(&stfsbuf
, MNT_NOWAIT
);
115 for (i
=0; i
< mntsize
; i
++) {
117 * We will cleanup in the case fstype is hammer.
118 * If we have null-mounted PFS, we check the
119 * mount source. If it looks like a PFS, we
120 * proceed to cleanup also.
122 fstype
= stfsbuf
[i
].f_fstypename
;
123 fs
= stfsbuf
[i
].f_mntfromname
;
124 if ((strcmp(fstype
, "hammer") == 0) ||
125 ((strcmp(fstype
, "null") == 0) &&
126 (strstr(fs
, "/@@0x") != NULL
||
127 strstr(fs
, "/@@-1") != NULL
))) {
128 path
= stfsbuf
[i
].f_mntonname
;
145 do_cleanup(const char *path
)
147 struct hammer_ioc_pseudofs_rw pfs
;
148 struct hammer_ioc_config config
;
149 struct hammer_ioc_version version
;
150 union hammer_ioc_mrecord_any mrec_tmp
;
151 char *snapshots_path
;
164 struct didpfs
*didpfs
;
165 int snapshots_disabled
= 0;
166 int prune_warning
= 0;
168 int snapshots_from_pfs
= 0;
173 bzero(&pfs
, sizeof(pfs
));
174 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
175 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
176 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
179 printf("cleanup %-20s -", path
);
180 fd
= open(path
, O_RDONLY
);
182 printf(" unable to access directory: %s\n", strerror(errno
));
185 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
186 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
190 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
191 printf(" unrecognized HAMMER version\n");
195 bzero(&version
, sizeof(version
));
196 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
197 printf(" HAMMER filesystem but couldn't retrieve version!\n");
202 bzero(&config
, sizeof(config
));
203 if (version
.cur_version
>= 3) {
204 if (ioctl(fd
, HAMMERIOC_GET_CONFIG
, &config
) == 0 &&
205 config
.head
.error
== 0) {
211 * Make sure we have not already handled this PFS. Several nullfs
212 * mounts might alias the same PFS.
214 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
215 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
216 printf(" PFS #%d already handled\n", pfs
.pfs_id
);
221 didpfs
= malloc(sizeof(*didpfs
));
222 didpfs
->next
= FirstPFS
;
224 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
227 * Calculate the old snapshots directory for HAMMER VERSION < 3
229 * If the directory is explicitly specified in the PFS config
230 * we flag it and will not migrate it later.
232 if (mrec_tmp
.pfs
.pfsd
.snapshots
[0] == '/') {
233 asprintf(&snapshots_path
, "%s", mrec_tmp
.pfs
.pfsd
.snapshots
);
234 snapshots_from_pfs
= 1;
235 } else if (mrec_tmp
.pfs
.pfsd
.snapshots
[0]) {
236 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
239 } else if (mrec_tmp
.pfs
.pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
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");
247 asprintf(&snapshots_path
,
248 "%s%ssnapshots", path
, dividing_slash(path
));
252 * Check for old-style config file
254 asprintf(&config_path
, "%s/config", snapshots_path
);
255 fp
= fopen(config_path
, "r");
258 * Handle upgrades to hammer version 3, move the config
259 * file into meta-data.
261 * For the old config read the file into the config structure,
262 * we will parse it out of the config structure regardless.
264 if (version
.cur_version
>= 3) {
266 printf("(migrating) ");
268 migrate_config(fp
, &config
);
269 migrate_snapshots(fd
, snapshots_path
);
271 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
272 printf(" cannot init meta-data config!\n");
277 } else if (new_config
== 0) {
278 config_init(path
, &config
);
279 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
280 printf(" cannot init meta-data config!\n");
288 * Create missing snapshots directory for HAMMER VERSION < 3
290 if (stat(snapshots_path
, &st
) < 0) {
291 if (mkdir(snapshots_path
, 0755) != 0) {
292 free(snapshots_path
);
293 printf(" unable to create snapshot dir \"%s\": %s\n",
294 snapshots_path
, strerror(errno
));
301 * Create missing config file for HAMMER VERSION < 3
304 config_init(path
, &config
);
305 fp
= fopen(config_path
, "w");
307 fwrite(config
.config
.text
, 1,
308 strlen(config
.config
.text
), fp
);
312 migrate_config(fp
, &config
);
318 * If snapshots_from_pfs is not set we calculate the new snapshots
319 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
320 * the old snapshots directory over.
322 * People who have set an explicit snapshots directory will have
323 * to migrate the data manually into /var/hammer, or not bother at
324 * all. People running slaves may wish to migrate it and then
325 * clear the snapshots specification in the PFS config for the
328 if (new_config
&& snapshots_from_pfs
== 0) {
331 assert(path
[0] == '/');
332 if (strcmp(path
, "/") == 0)
333 asprintf(&npath
, "%s/root", SNAPSHOTS_BASE
);
335 asprintf(&npath
, "%s/%s", SNAPSHOTS_BASE
, path
+ 1);
336 if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
337 if (stat(snapshots_path
, &st
) < 0 && errno
== ENOENT
) {
338 printf(" HAMMER UPGRADE: Creating snapshots\n"
339 "\tCreating snapshots in %s\n",
341 runcmd(&r
, "mkdir -p %s", npath
);
343 printf(" HAMMER UPGRADE: Moving snapshots\n"
344 "\tMoving snapshots from %s to %s\n",
345 snapshots_path
, npath
);
346 runcmd(&r
, "mkdir -p %s", npath
);
347 runcmd(&r
, "cpdup %s %s", snapshots_path
, npath
);
349 printf("Unable to move snapshots directory!\n");
350 printf("Please fix this critical error.\n");
351 printf("Aborting cleanup of %s\n", path
);
355 runcmd(&r
, "rm -rf %s", snapshots_path
);
358 free(snapshots_path
);
359 snapshots_path
= npath
;
363 * Lock the PFS. fd is the base directory of the mounted PFS.
365 if (flock(fd
, LOCK_EX
|LOCK_NB
) == -1) {
366 if (errno
== EWOULDBLOCK
)
367 printf(" PFS #%d locked by other process\n", pfs
.pfs_id
);
369 printf(" can not lock %s: %s\n", config_path
, strerror(errno
));
374 printf(" handle PFS #%d using %s\n", pfs
.pfs_id
, snapshots_path
);
377 * Process the config file
379 cbase
= config
.config
.text
;
381 while ((cptr
= strchr(cbase
, '\n')) != NULL
) {
382 bcopy(cbase
, buf
, cptr
- cbase
);
383 buf
[cptr
- cbase
] = 0;
386 cmd
= strtok(buf
, WS
);
390 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
391 arg1
= strtosecs(ptr
);
392 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
393 arg2
= strtosecs(ptr
);
394 arg3
= strtok(NULL
, WS
);
398 printf("%20s - ", cmd
);
402 if (strcmp(cmd
, "snapshots") == 0) {
405 check_softlinks(fd
, new_config
,
407 printf("only removing old snapshots\n");
409 cleanup_softlinks(fd
, new_config
,
413 printf("disabled\n");
414 snapshots_disabled
= 1;
417 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
419 cleanup_softlinks(fd
, new_config
,
422 r
= create_snapshot(path
, snapshots_path
);
426 } else if (arg1
== 0) {
428 * The commands following this check can't handle
429 * a period of 0, so call the feature disabled and
430 * ignore the directive.
432 printf("disabled\n");
433 } else if (strcmp(cmd
, "prune") == 0) {
434 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
436 printf("run - WARNING snapshot "
438 "but snapshots disabled\n");
442 r
= cleanup_prune(path
, snapshots_path
,
443 arg1
, arg2
, snapshots_disabled
);
447 } else if (strcmp(cmd
, "rebalance") == 0) {
449 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
454 r
= cleanup_rebalance(path
, snapshots_path
,
459 } else if (strcmp(cmd
, "reblock") == 0) {
460 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
465 r
= cleanup_reblock(path
, snapshots_path
,
470 } else if (strcmp(cmd
, "recopy") == 0) {
471 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
476 r
= cleanup_recopy(path
, snapshots_path
,
482 printf("unknown directive\n");
486 save_period(snapshots_path
, cmd
, savet
);
490 * Add new rebalance feature if the config doesn't have it.
491 * (old style config only).
493 if (new_config
== 0 && found_rebal
== 0) {
494 if ((fp
= fopen(config_path
, "r+")) != NULL
) {
496 fprintf(fp
, "rebalance 1d 5m\n");
502 * Cleanup, and delay a little
509 * Initialize new config data (new or old style)
512 config_init(const char *path
, struct hammer_ioc_config
*config
)
514 const char *snapshots
;
516 if (strcmp(path
, "/tmp") == 0 ||
517 strcmp(path
, "/var/tmp") == 0 ||
518 strcmp(path
, "/usr/obj") == 0) {
519 snapshots
= "snapshots 0d 0d\n";
521 snapshots
= "snapshots 1d 60d\n";
523 bzero(config
->config
.text
, sizeof(config
->config
.text
));
524 snprintf(config
->config
.text
, sizeof(config
->config
.text
) - 1, "%s%s",
530 "rebalance 1d 5m\n");
534 * Migrate configuration data from the old snapshots/config
535 * file to the new mata-data format.
538 migrate_config(FILE *fp
, struct hammer_ioc_config
*config
)
542 n
= fread(config
->config
.text
, 1, sizeof(config
->config
.text
) - 1, fp
);
544 bzero(config
->config
.text
+ n
, sizeof(config
->config
.text
) - n
);
548 * Migrate snapshot softlinks in the snapshots directory to the
549 * new meta-data format. The softlinks are left intact, but
550 * this way the pruning code won't lose track of them if you
551 * happen to blow away the snapshots directory.
554 migrate_snapshots(int fd
, const char *snapshots_path
)
556 struct hammer_ioc_snapshot snapshot
;
562 bzero(&snapshot
, sizeof(snapshot
));
564 if ((dir
= opendir(snapshots_path
)) != NULL
) {
565 while ((den
= readdir(dir
)) != NULL
) {
566 if (den
->d_name
[0] == '.')
568 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
569 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
570 migrate_one_snapshot(fd
, fpath
, &snapshot
);
576 migrate_one_snapshot(fd
, NULL
, &snapshot
);
581 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
582 * otherwise it is flushed when it fills up.
585 migrate_one_snapshot(int fd
, const char *fpath
,
586 struct hammer_ioc_snapshot
*snapshot
)
589 struct hammer_snapshot_data
*snap
;
603 tid
= (hammer_tid_t
)(int64_t)-1;
606 while (*ptr
&& *ptr
!= '-' && *ptr
!= '.')
610 r
= sscanf(ptr
, "%4d%2d%2d-%2d%2d",
611 &year
, &month
, &day
, &hour
, &minute
);
614 bzero(&tm
, sizeof(tm
));
619 tm
.tm_mon
= month
- 1;
620 tm
.tm_year
= year
- 1900;
623 bzero(linkbuf
, sizeof(linkbuf
));
624 if (readlink(fpath
, linkbuf
, sizeof(linkbuf
) - 1) > 0 &&
625 (ptr
= strrchr(linkbuf
, '@')) != NULL
&&
626 ptr
> linkbuf
&& ptr
[-1] == '@') {
627 tid
= strtoull(ptr
+ 1, NULL
, 16);
629 if (t
!= (time_t)-1 && tid
!= (hammer_tid_t
)(int64_t)-1) {
630 snap
= &snapshot
->snaps
[snapshot
->count
];
631 bzero(snap
, sizeof(*snap
));
633 snap
->ts
= (u_int64_t
)t
* 1000000ULL;
634 snprintf(snap
->label
, sizeof(snap
->label
),
640 if ((fpath
== NULL
&& snapshot
->count
) ||
641 snapshot
->count
== HAMMER_SNAPS_PER_IOCTL
) {
642 printf(" (%d snapshots)", snapshot
->count
);
644 if (ioctl(fd
, HAMMERIOC_ADD_SNAPSHOT
, snapshot
) < 0) {
645 printf(" Ioctl to migrate snapshots failed: %s\n",
647 } else if (snapshot
->head
.error
== EALREADY
) {
650 } else if (snapshot
->head
.error
) {
651 printf(" Ioctl to migrate snapshots failed: %s\n",
652 strerror(snapshot
->head
.error
));
654 printf("index %d\n", snapshot
->index
);
657 snapshot
->head
.error
= 0;
667 val
= strtol(ptr
, &ptr
, 0);
681 errx(1, "illegal suffix converting %s\n", ptr
);
688 dividing_slash(const char *path
)
690 int len
= strlen(path
);
691 if (len
&& path
[len
-1] == '/')
698 * Check whether the desired period has elapsed since the last successful
699 * run. The run may take a while and cross a boundary so we remember the
700 * current time_t so we can save it later on.
702 * Periods in minutes, hours, or days are assumed to have been crossed
703 * if the local time crosses a minute, hour, or day boundary regardless
704 * of how close the last operation actually was.
707 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
718 localtime_r(savep
, &tp1
);
721 * Retrieve the start time of the last successful operation.
723 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
724 fp
= fopen(check_path
, "r");
728 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
734 lastt
= strtol(buf
, NULL
, 0);
735 localtime_r(&lastt
, &tp2
);
738 * Normalize the times. e.g. if asked to do something on a 1-day
739 * interval the operation will be performed as soon as the day
740 * turns over relative to the previous operation, even if the previous
741 * operation ran a few seconds ago just before midnight.
743 if (arg1
% 60 == 0) {
747 if (arg1
% (60 * 60) == 0) {
751 if (arg1
% (24 * 60 * 60) == 0) {
756 baset
= mktime(&tp1
);
757 lastt
= mktime(&tp2
);
760 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
763 if ((int)(baset
- lastt
) >= arg1
)
769 * Store the start time of the last successful operation.
772 save_period(const char *snapshots_path
, const char *cmd
,
779 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
780 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
781 fp
= fopen(ncheck_path
, "w");
783 fprintf(fp
, "0x%08llx\n", (long long)savet
);
785 rename(ncheck_path
, ocheck_path
);
788 fprintf(stderr
, "hammer: Unable to create period-file %s: %s\n",
789 ncheck_path
, strerror(errno
));
794 * Simply count the number of softlinks in the snapshots dir
797 check_softlinks(int fd
, int new_config
, const char *snapshots_path
)
806 * Old-style softlink-based snapshots
808 if ((dir
= opendir(snapshots_path
)) != NULL
) {
809 while ((den
= readdir(dir
)) != NULL
) {
810 if (den
->d_name
[0] == '.')
812 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
813 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
821 * New-style snapshots are stored as filesystem meta-data,
825 struct hammer_ioc_snapshot snapshot
;
827 bzero(&snapshot
, sizeof(snapshot
));
829 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
830 err(2, "hammer cleanup: check_softlink "
834 res
+= snapshot
.count
;
835 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
841 * Clean up expired softlinks in the snapshots dir
844 cleanup_softlinks(int fd
, int new_config
,
845 const char *snapshots_path
, int arg2
, char *arg3
)
853 if (arg3
!= NULL
&& strstr(arg3
, "any") != NULL
)
856 if ((dir
= opendir(snapshots_path
)) != NULL
) {
857 while ((den
= readdir(dir
)) != NULL
) {
858 if (den
->d_name
[0] == '.')
860 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
861 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
862 (anylink
|| strncmp(den
->d_name
, "snap-", 5) == 0)
864 if (check_expired(den
->d_name
, arg2
)) {
866 printf(" expire %s\n",
878 * New-style snapshots are stored as filesystem meta-data,
882 struct hammer_ioc_snapshot snapshot
;
883 struct hammer_ioc_snapshot dsnapshot
;
884 struct hammer_snapshot_data
*snap
;
890 bzero(&snapshot
, sizeof(snapshot
));
891 bzero(&dsnapshot
, sizeof(dsnapshot
));
893 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
894 err(2, "hammer cleanup: check_softlink "
898 for (i
= 0; i
< snapshot
.count
; ++i
) {
899 snap
= &snapshot
.snaps
[i
];
900 t
= time(NULL
) - snap
->ts
/ 1000000ULL;
901 if ((int)t
> arg2
&& snap
->tid
!= 0) {
902 dsnapshot
.snaps
[dsnapshot
.count
++] =
905 if ((int)t
> arg2
&& VerboseOpt
) {
907 strftime(snapts
, sizeof(snapts
),
908 "%Y-%m-%d %H:%M:%S %Z", tp
);
909 printf(" expire 0x%016jx %s %s\n",
910 (uintmax_t)snap
->tid
,
914 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
) {
915 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, &dsnapshot
) < 0) {
916 printf(" Ioctl to delete snapshots failed: %s index %d\n", strerror(errno
), dsnapshot
.index
);
917 } else if (dsnapshot
.head
.error
) {
918 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot
.head
.error
));
923 dsnapshot
.head
.error
= 0;
926 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
928 if (dsnapshot
.count
) {
929 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, &dsnapshot
) < 0) {
930 printf(" Ioctl to delete snapshots failed: %s\n", strerror(errno
));
931 } else if (dsnapshot
.head
.error
) {
932 printf(" Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot
.head
.error
));
936 dsnapshot
.head
.error
= 0;
942 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
943 * expiration in seconds (arg2) and return non-zero if the softlink
947 check_expired(const char *fpath
, int arg2
)
958 while (*fpath
&& *fpath
!= '-' && *fpath
!= '.')
963 r
= sscanf(fpath
, "%4d%2d%2d-%2d%2d",
964 &year
, &month
, &day
, &hour
, &minute
);
967 bzero(&tm
, sizeof(tm
));
972 tm
.tm_mon
= month
- 1;
973 tm
.tm_year
= year
- 1900;
988 create_snapshot(const char *path
, const char *snapshots_path
)
992 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
997 cleanup_prune(const char *path __unused
, const char *snapshots_path
,
998 int arg1 __unused
, int arg2
, int snapshots_disabled
)
1001 * If snapshots have been disabled run prune-everything instead
1004 if (snapshots_disabled
&& arg2
) {
1005 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1006 snapshots_path
, arg2
, path
);
1007 } else if (snapshots_disabled
) {
1008 runcmd(NULL
, "hammer prune-everything %s", path
);
1010 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
1011 snapshots_path
, arg2
, snapshots_path
);
1013 runcmd(NULL
, "hammer prune %s", snapshots_path
);
1019 cleanup_rebalance(const char *path
, const char *snapshots_path
,
1020 int arg1 __unused
, int arg2
)
1022 if (VerboseOpt
== 0) {
1028 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1029 snapshots_path
, arg2
, path
);
1030 if (VerboseOpt
== 0) {
1034 if (VerboseOpt
== 0)
1040 cleanup_reblock(const char *path
, const char *snapshots_path
,
1041 int arg1 __unused
, int arg2
)
1043 if (VerboseOpt
== 0) {
1049 * When reblocking the B-Tree always reblock everything in normal
1053 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1054 snapshots_path
, arg2
, path
);
1055 if (VerboseOpt
== 0) {
1061 * When reblocking the inodes always reblock everything in normal
1065 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1066 snapshots_path
, arg2
, path
);
1067 if (VerboseOpt
== 0) {
1073 * When reblocking the directories always reblock everything in normal
1077 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1078 snapshots_path
, arg2
, path
);
1079 if (VerboseOpt
== 0) {
1085 * Do not reblock all the data in normal mode.
1088 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1089 snapshots_path
, arg2
, path
);
1090 if (VerboseOpt
== 0)
1096 cleanup_recopy(const char *path
, const char *snapshots_path
,
1097 int arg1 __unused
, int arg2
)
1099 if (VerboseOpt
== 0) {
1104 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1105 snapshots_path
, arg2
, path
);
1106 if (VerboseOpt
== 0) {
1111 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1112 snapshots_path
, arg2
, path
);
1113 if (VerboseOpt
== 0) {
1118 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1119 snapshots_path
, arg2
, path
);
1120 if (VerboseOpt
== 0) {
1125 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1126 snapshots_path
, arg2
, path
);
1127 if (VerboseOpt
== 0)
1134 runcmd(int *resp
, const char *ctl
, ...)
1146 * Generate the command
1149 vasprintf(&cmd
, ctl
, va
);
1152 printf(" %s\n", cmd
);
1155 * Break us down into arguments. We do not just use system() here
1156 * because it blocks SIGINT and friends.
1160 av
= malloc(sizeof(char *) * nmax
);
1162 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
1163 if (n
== nmax
- 1) {
1165 av
= realloc(av
, sizeof(char *) * nmax
);
1175 if ((pid
= fork()) == 0) {
1176 if (VerboseOpt
< 2) {
1177 int fd
= open("/dev/null", O_RDWR
);
1183 } else if (pid
< 0) {
1188 while (waitpid(pid
, &status
, 0) != pid
)
1190 res
= WEXITSTATUS(status
);