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
35 * Clean up specific HAMMER filesystems or all HAMMER filesystems.
37 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
40 * Each HAMMER filesystem may contain a configuration file. If no
41 * configuration file is present one will be created with the following
44 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj)
47 * #dedup 1d 5m (not enabled by default)
51 * All hammer commands create and maintain cycle files in the snapshots
54 * For HAMMER version 2- the configuration file is a named 'config' in
55 * the snapshots directory, which defaults to <pfs>/snapshots.
56 * For HAMMER version 3+ the configuration file is saved in filesystem
57 * meta-data. The snapshots directory defaults to /var/hammer/<pfs>
58 * (/var/hammer/root for root mount).
70 static void do_cleanup(const char *path
);
71 static void config_init(const char *path
, struct hammer_ioc_config
*config
);
72 static void migrate_config(FILE *fp
, struct hammer_ioc_config
*config
);
73 static void migrate_snapshots(int fd
, const char *snapshots_path
);
74 static void migrate_one_snapshot(int fd
, const char *fpath
,
75 struct hammer_ioc_snapshot
*snapshot
);
76 static int strtosecs(char *ptr
);
77 static const char *dividing_slash(const char *path
);
78 static int check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
80 static void save_period(const char *snapshots_path
, const char *cmd
,
82 static int check_softlinks(int fd
, int new_config
, const char *snapshots_path
);
83 static void cleanup_softlinks(int fd
, int new_config
,
84 const char *snapshots_path
, int arg2
, char *arg3
);
85 static void delete_snapshots(int fd
, struct hammer_ioc_snapshot
*dsnapshot
);
86 static int check_expired(const char *fpath
, int arg2
);
88 static int create_snapshot(const char *path
, const char *snapshots_path
);
89 static int cleanup_rebalance(const char *path
, const char *snapshots_path
,
91 static int cleanup_prune(const char *path
, const char *snapshots_path
,
92 int arg1
, int arg2
, int snapshots_disabled
);
93 static int cleanup_reblock(const char *path
, const char *snapshots_path
,
95 static int cleanup_recopy(const char *path
, const char *snapshots_path
,
97 static int cleanup_dedup(const char *path
, const char *snapshots_path
,
100 static void runcmd(int *resp
, const char *ctl
, ...) __printflike(2, 3);
104 static struct didpfs
*FirstPFS
;
107 hammer_cmd_cleanup(char **av
, int ac
)
109 char *fstype
, *fs
, *path
;
110 struct statfs
*stfsbuf
;
115 mntsize
= getmntinfo(&stfsbuf
, MNT_NOWAIT
);
117 for (i
=0; i
< mntsize
; i
++) {
119 * We will cleanup in the case fstype is hammer.
120 * If we have null-mounted PFS, we check the
121 * mount source. If it looks like a PFS, we
122 * proceed to cleanup also.
124 fstype
= stfsbuf
[i
].f_fstypename
;
125 fs
= stfsbuf
[i
].f_mntfromname
;
126 if ((strcmp(fstype
, "hammer") == 0) ||
127 ((strcmp(fstype
, "null") == 0) &&
128 (strstr(fs
, "/@@0x") != NULL
||
129 strstr(fs
, "/@@-1") != NULL
))) {
130 path
= stfsbuf
[i
].f_mntonname
;
146 do_cleanup(const char *path
)
148 struct hammer_ioc_pseudofs_rw pfs
;
149 struct hammer_ioc_config config
;
150 struct hammer_ioc_version version
;
151 union hammer_ioc_mrecord_any mrec_tmp
;
152 char *snapshots_path
= NULL
;
165 struct didpfs
*didpfs
;
166 int snapshots_disabled
= 0;
167 int prune_warning
= 0;
169 int snapshots_from_pfs
= 0;
174 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
175 clrpfs(&pfs
, &mrec_tmp
.pfs
.pfsd
, -1);
177 printf("cleanup %-20s -", path
);
178 fd
= open(path
, O_RDONLY
);
180 printf(" unable to access directory: %s\n", strerror(errno
));
183 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
184 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
188 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
189 printf(" unrecognized HAMMER version\n");
193 bzero(&version
, sizeof(version
));
194 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
195 printf(" HAMMER filesystem but couldn't retrieve version!\n");
199 HammerVersion
= version
.cur_version
;
201 bzero(&config
, sizeof(config
));
202 if (version
.cur_version
>= 3) {
203 if (ioctl(fd
, HAMMERIOC_GET_CONFIG
, &config
) == 0 &&
204 config
.head
.error
== 0) {
210 * Make sure we have not already handled this PFS. Several nullfs
211 * mounts might alias the same PFS.
213 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
214 if (hammer_uuid_compare(&didpfs
->uuid
,
215 &mrec_tmp
.pfs
.pfsd
.unique_uuid
) == 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 (hammer_is_pfs_slave(&mrec_tmp
.pfs
.pfsd
)) {
240 if (version
.cur_version
< 3) {
241 printf(" WARNING: must configure snapshot dir for PFS slave\n");
242 printf("\tWe suggest <fs>/var/slaves/<name> where "
243 "<fs> is the base HAMMER fs\n");
244 printf("\tcontaining the slave\n");
249 asprintf(&snapshots_path
,
250 "%s%ssnapshots", path
, dividing_slash(path
));
254 * Check for old-style config file
256 if (snapshots_path
) {
257 asprintf(&config_path
, "%s/config", snapshots_path
);
258 fp
= fopen(config_path
, "r");
262 * Handle upgrades to hammer version 3, move the config
263 * file into meta-data.
265 * For the old config read the file into the config structure,
266 * we will parse it out of the config structure regardless.
268 if (version
.cur_version
>= 3) {
270 printf("(migrating) ");
272 migrate_config(fp
, &config
);
273 migrate_snapshots(fd
, snapshots_path
);
275 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
276 printf(" cannot init meta-data config!\n");
281 } else if (new_config
== 0) {
282 config_init(path
, &config
);
283 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
284 printf(" cannot init meta-data config!\n");
292 * Create missing snapshots directory for HAMMER VERSION < 3
294 if (stat(snapshots_path
, &st
) < 0) {
295 if (mkdir(snapshots_path
, 0755) != 0) {
296 free(snapshots_path
);
297 printf(" unable to create snapshot dir \"%s\": %s\n",
298 snapshots_path
, strerror(errno
));
305 * Create missing config file for HAMMER VERSION < 3
308 config_init(path
, &config
);
309 fp
= fopen(config_path
, "w");
311 fwrite(config
.config
.text
, 1,
312 strlen(config
.config
.text
), fp
);
316 migrate_config(fp
, &config
);
322 * If snapshots_from_pfs is not set we calculate the new snapshots
323 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
324 * the old snapshots directory over.
326 * People who have set an explicit snapshots directory will have
327 * to migrate the data manually into /var/hammer, or not bother at
328 * all. People running slaves may wish to migrate it and then
329 * clear the snapshots specification in the PFS config for the
332 if (new_config
&& snapshots_from_pfs
== 0) {
335 if (path
[0] != '/') {
336 printf(" path must start with '/'\n");
339 if (strcmp(path
, "/") == 0)
340 asprintf(&npath
, "%s/root", SNAPSHOTS_BASE
);
342 asprintf(&npath
, "%s/%s", SNAPSHOTS_BASE
, path
+ 1);
343 if (snapshots_path
) {
344 if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
345 if (stat(snapshots_path
, &st
) < 0 && errno
== ENOENT
) {
346 printf(" HAMMER UPGRADE: Creating snapshots\n"
347 "\tCreating snapshots in %s\n",
349 runcmd(&r
, "mkdir -p %s", npath
);
351 printf(" HAMMER UPGRADE: Moving snapshots\n"
352 "\tMoving snapshots from %s to %s\n",
353 snapshots_path
, npath
);
354 runcmd(&r
, "mkdir -p %s", npath
);
355 runcmd(&r
, "cpdup %s %s", snapshots_path
, npath
);
357 printf("Unable to move snapshots directory!\n");
358 printf("Please fix this critical error.\n");
359 printf("Aborting cleanup of %s\n", path
);
363 runcmd(&r
, "rm -rf %s", snapshots_path
);
366 free(snapshots_path
);
367 } else if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
368 runcmd(&r
, "mkdir -p %s", npath
);
370 snapshots_path
= npath
;
374 * Lock the PFS. fd is the base directory of the mounted PFS.
376 if (flock(fd
, LOCK_EX
|LOCK_NB
) == -1) {
377 if (errno
== EWOULDBLOCK
)
378 printf(" PFS#%d locked by other process\n", pfs
.pfs_id
);
380 printf(" can not lock %s: %s\n", config_path
, strerror(errno
));
385 printf(" handle PFS#%d using %s\n", pfs
.pfs_id
, snapshots_path
);
387 struct pidfh
*pfh
= NULL
;
388 static char pidfile
[PIDFILE_BUFSIZE
];
390 snprintf (pidfile
, PIDFILE_BUFSIZE
, "%s/hammer.cleanup.%d",
391 pidfile_loc
, getpid());
392 pfh
= pidfile_open(pidfile
, 0644, NULL
);
394 warn ("Unable to open or create %s", pidfile
);
398 * Process the config file
400 cbase
= config
.config
.text
;
402 while ((cptr
= strchr(cbase
, '\n')) != NULL
) {
403 bcopy(cbase
, buf
, cptr
- cbase
);
404 buf
[cptr
- cbase
] = 0;
407 cmd
= strtok(buf
, WS
);
408 if (cmd
== NULL
|| cmd
[0] == '#')
414 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
415 arg1
= strtosecs(ptr
);
416 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
417 arg2
= strtosecs(ptr
);
418 arg3
= strtok(NULL
, WS
);
422 printf("%20s - ", cmd
);
426 if (strcmp(cmd
, "snapshots") == 0) {
429 check_softlinks(fd
, new_config
,
431 printf("only removing old snapshots\n");
433 cleanup_softlinks(fd
, new_config
,
437 printf("disabled\n");
438 snapshots_disabled
= 1;
441 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
443 cleanup_softlinks(fd
, new_config
,
446 r
= create_snapshot(path
, snapshots_path
);
450 } else if (arg1
== 0) {
452 * The commands following this check can't handle
453 * a period of 0, so call the feature disabled and
454 * ignore the directive.
456 printf("disabled\n");
457 } else if (strcmp(cmd
, "prune") == 0) {
458 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
460 printf("run - WARNING snapshot "
462 "but snapshots disabled\n");
466 r
= cleanup_prune(path
, snapshots_path
,
467 arg1
, arg2
, snapshots_disabled
);
471 } else if (strcmp(cmd
, "rebalance") == 0) {
473 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
478 r
= cleanup_rebalance(path
, snapshots_path
,
483 } else if (strcmp(cmd
, "reblock") == 0) {
484 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
489 r
= cleanup_reblock(path
, snapshots_path
,
494 } else if (strcmp(cmd
, "recopy") == 0) {
495 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
500 r
= cleanup_recopy(path
, snapshots_path
,
505 } else if (strcmp(cmd
, "dedup") == 0) {
506 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
511 r
= cleanup_dedup(path
, snapshots_path
,
517 printf("unknown directive\n");
521 save_period(snapshots_path
, cmd
, savet
);
525 * Add new rebalance feature if the config doesn't have it.
526 * (old style config only).
528 if (new_config
== 0 && found_rebal
== 0) {
529 if ((fp
= fopen(config_path
, "r+")) != NULL
) {
531 fprintf(fp
, "rebalance 1d 5m\n");
537 * Cleanup, and delay a little
539 * NOTE: pidfile_remove() closes, removes, and frees the pfh.
540 * pidfile_close() closes and frees.
548 * Initialize new config data (new or old style)
552 config_init(const char *path
, struct hammer_ioc_config
*config
)
554 const char *snapshots
;
556 if (strcmp(path
, "/tmp") == 0 ||
557 strcmp(path
, "/var/tmp") == 0 ||
558 strcmp(path
, "/usr/obj") == 0) {
559 snapshots
= "snapshots 0d 0d\n";
561 snapshots
= "snapshots 1d 60d\n";
563 bzero(config
->config
.text
, sizeof(config
->config
.text
));
564 snprintf(config
->config
.text
, sizeof(config
->config
.text
) - 1, "%s%s",
574 * Migrate configuration data from the old snapshots/config
575 * file to the new meta-data format.
579 migrate_config(FILE *fp
, struct hammer_ioc_config
*config
)
583 n
= fread(config
->config
.text
, 1, sizeof(config
->config
.text
) - 1, fp
);
585 bzero(config
->config
.text
+ n
, sizeof(config
->config
.text
) - n
);
589 * Migrate snapshot softlinks in the snapshots directory to the
590 * new meta-data format. The softlinks are left intact, but
591 * this way the pruning code won't lose track of them if you
592 * happen to blow away the snapshots directory.
596 migrate_snapshots(int fd
, const char *snapshots_path
)
598 struct hammer_ioc_snapshot snapshot
;
604 bzero(&snapshot
, sizeof(snapshot
));
606 if ((dir
= opendir(snapshots_path
)) != NULL
) {
607 while ((den
= readdir(dir
)) != NULL
) {
608 if (den
->d_name
[0] == '.')
610 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
611 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
612 migrate_one_snapshot(fd
, fpath
, &snapshot
);
617 migrate_one_snapshot(fd
, NULL
, &snapshot
);
622 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
623 * otherwise it is flushed when it fills up.
627 migrate_one_snapshot(int fd
, const char *fpath
,
628 struct hammer_ioc_snapshot
*snapshot
)
631 hammer_snapshot_data_t snap
;
645 tid
= (hammer_tid_t
)(int64_t)-1;
647 /* fpath may contain directory components */
648 if ((ptr
= strrchr(fpath
, '/')) != NULL
)
652 while (*ptr
&& *ptr
!= '-' && *ptr
!= '.')
656 r
= sscanf(ptr
, "%4d%2d%2d-%2d%2d",
657 &year
, &month
, &day
, &hour
, &minute
);
660 bzero(&tm
, sizeof(tm
));
665 tm
.tm_mon
= month
- 1;
666 tm
.tm_year
= year
- 1900;
669 bzero(linkbuf
, sizeof(linkbuf
));
670 if (readlink(fpath
, linkbuf
, sizeof(linkbuf
) - 1) > 0 &&
671 (ptr
= strrchr(linkbuf
, '@')) != NULL
&&
672 ptr
> linkbuf
&& ptr
[-1] == '@') {
673 tid
= strtoull(ptr
+ 1, NULL
, 16);
675 if (t
!= (time_t)-1 && tid
!= (hammer_tid_t
)(int64_t)-1) {
676 snap
= &snapshot
->snaps
[snapshot
->count
];
677 bzero(snap
, sizeof(*snap
));
679 snap
->ts
= (uint64_t)t
* 1000000ULL;
680 snprintf(snap
->label
, sizeof(snap
->label
),
684 printf(" non-canonical snapshot softlink: %s->%s\n",
689 if ((fpath
== NULL
&& snapshot
->count
) ||
690 snapshot
->count
== HAMMER_SNAPS_PER_IOCTL
) {
691 printf(" (%d snapshots)", snapshot
->count
);
693 if (ioctl(fd
, HAMMERIOC_ADD_SNAPSHOT
, snapshot
) < 0) {
694 printf(" Ioctl to migrate snapshots failed: %s\n",
696 } else if (snapshot
->head
.error
== EALREADY
) {
699 } else if (snapshot
->head
.error
) {
700 printf(" Ioctl to migrate snapshots failed: %s\n",
701 strerror(snapshot
->head
.error
));
703 printf("index %d\n", snapshot
->index
);
706 snapshot
->head
.error
= 0;
716 val
= strtol(ptr
, &ptr
, 0);
730 errx(1, "illegal suffix converting %s", ptr
);
739 dividing_slash(const char *path
)
741 int len
= strlen(path
);
742 if (len
&& path
[len
-1] == '/')
749 * Check whether the desired period has elapsed since the last successful
750 * run. The run may take a while and cross a boundary so we remember the
751 * current time_t so we can save it later on.
753 * Periods in minutes, hours, or days are assumed to have been crossed
754 * if the local time crosses a minute, hour, or day boundary regardless
755 * of how close the last operation actually was.
757 * If ForceOpt is set always return true.
761 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
772 localtime_r(savep
, &tp1
);
781 * Retrieve the start time of the last successful operation.
783 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
784 fp
= fopen(check_path
, "r");
788 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
794 lastt
= strtol(buf
, NULL
, 0);
795 localtime_r(&lastt
, &tp2
);
798 * Normalize the times. e.g. if asked to do something on a 1-day
799 * interval the operation will be performed as soon as the day
800 * turns over relative to the previous operation, even if the previous
801 * operation ran a few seconds ago just before midnight.
803 if (arg1
% 60 == 0) {
807 if (arg1
% (60 * 60) == 0) {
811 if (arg1
% (24 * 60 * 60) == 0) {
816 baset
= mktime(&tp1
);
817 lastt
= mktime(&tp2
);
820 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
823 if ((int)(baset
- lastt
) >= arg1
)
829 * Store the start time of the last successful operation.
833 save_period(const char *snapshots_path
, const char *cmd
,
840 asprintf(&ocheck_path
, "%s/.%s.period", snapshots_path
, cmd
);
841 asprintf(&ncheck_path
, "%s/.%s.period.new", snapshots_path
, cmd
);
842 fp
= fopen(ncheck_path
, "w");
844 fprintf(fp
, "0x%08llx\n", (long long)savet
);
846 rename(ncheck_path
, ocheck_path
);
849 fprintf(stderr
, "hammer: Unable to create period-file %s: %s\n",
850 ncheck_path
, strerror(errno
));
855 * Simply count the number of softlinks in the snapshots dir
859 check_softlinks(int fd
, int new_config
, const char *snapshots_path
)
868 * Old-style softlink-based snapshots
870 if ((dir
= opendir(snapshots_path
)) != NULL
) {
871 while ((den
= readdir(dir
)) != NULL
) {
872 if (den
->d_name
[0] == '.')
874 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
875 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
883 * New-style snapshots are stored as filesystem meta-data,
887 struct hammer_ioc_snapshot snapshot
;
889 bzero(&snapshot
, sizeof(snapshot
));
891 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
892 err(2, "hammer cleanup: check_softlink "
896 res
+= snapshot
.count
;
897 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
903 * Clean up expired softlinks in the snapshots dir
907 cleanup_softlinks(int fd
, int new_config
,
908 const char *snapshots_path
, int arg2
, char *arg3
)
916 if (arg3
!= NULL
&& strstr(arg3
, "any") != NULL
)
919 if ((dir
= opendir(snapshots_path
)) != NULL
) {
920 while ((den
= readdir(dir
)) != NULL
) {
921 if (den
->d_name
[0] == '.')
923 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
924 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
925 (anylink
|| strncmp(den
->d_name
, "snap-", 5) == 0)) {
926 if (check_expired(den
->d_name
, arg2
)) {
928 printf(" expire %s\n",
940 * New-style snapshots are stored as filesystem meta-data,
944 struct hammer_ioc_snapshot snapshot
;
945 struct hammer_ioc_snapshot dsnapshot
;
946 hammer_snapshot_data_t snap
;
953 bzero(&snapshot
, sizeof(snapshot
));
954 bzero(&dsnapshot
, sizeof(dsnapshot
));
956 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
957 err(2, "hammer cleanup: check_softlink "
961 for (i
= 0; i
< snapshot
.count
; ++i
) {
962 snap
= &snapshot
.snaps
[i
];
963 t
= snap
->ts
/ 1000000ULL;
965 if ((int)dt
> arg2
|| snap
->tid
== 0) {
966 dsnapshot
.snaps
[dsnapshot
.count
++] =
969 if ((int)dt
> arg2
&& VerboseOpt
) {
971 strftime(snapts
, sizeof(snapts
),
972 "%Y-%m-%d %H:%M:%S %Z", tp
);
973 printf(" expire 0x%016jx %s %s\n",
974 (uintmax_t)snap
->tid
,
978 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
)
979 delete_snapshots(fd
, &dsnapshot
);
981 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
984 delete_snapshots(fd
, &dsnapshot
);
990 delete_snapshots(int fd
, struct hammer_ioc_snapshot
*dsnapshot
)
993 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, dsnapshot
) < 0) {
994 printf(" Ioctl to delete snapshots failed: %s\n",
998 if (dsnapshot
->head
.error
) {
999 printf(" Ioctl to delete snapshots failed at "
1000 "snap=%016jx: %s\n",
1001 dsnapshot
->snaps
[dsnapshot
->index
].tid
,
1002 strerror(dsnapshot
->head
.error
));
1003 if (++dsnapshot
->index
< dsnapshot
->count
)
1008 dsnapshot
->index
= 0;
1009 dsnapshot
->count
= 0;
1010 dsnapshot
->head
.error
= 0;
1014 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
1015 * expiration in seconds (arg2) and return non-zero if the softlink
1020 check_expired(const char *fpath
, int arg2
)
1031 while (*fpath
&& *fpath
!= '-' && *fpath
!= '.')
1036 r
= sscanf(fpath
, "%4d%2d%2d-%2d%2d",
1037 &year
, &month
, &day
, &hour
, &minute
);
1040 bzero(&tm
, sizeof(tm
));
1045 tm
.tm_mon
= month
- 1;
1046 tm
.tm_year
= year
- 1900;
1048 if (t
== (time_t)-1)
1062 create_snapshot(const char *path
, const char *snapshots_path
)
1066 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
1072 cleanup_prune(const char *path
, const char *snapshots_path
,
1073 int arg1 __unused
, int arg2
, int snapshots_disabled
)
1075 const char *path_or_snapshots_path
;
1078 * If the snapshots_path (e.g. /var/hammer/...) has no snapshots
1079 * in it then prune will get confused and prune the filesystem
1080 * containing the snapshots_path instead of the requested
1081 * filesystem. De-confuse prune. We need a better way.
1083 if (hammer_softprune_testdir(snapshots_path
))
1084 path_or_snapshots_path
= snapshots_path
;
1086 path_or_snapshots_path
= path
;
1089 * If snapshots have been disabled run prune-everything instead
1092 if (snapshots_disabled
&& arg2
) {
1094 "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1095 snapshots_path
, arg2
, path
);
1096 } else if (snapshots_disabled
) {
1097 runcmd(NULL
, "hammer prune-everything %s", path
);
1099 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
1100 snapshots_path
, arg2
, path_or_snapshots_path
);
1102 runcmd(NULL
, "hammer prune %s", path_or_snapshots_path
);
1109 cleanup_rebalance(const char *path
, const char *snapshots_path
,
1110 int arg1 __unused
, int arg2
)
1112 if (VerboseOpt
== 0) {
1118 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1119 snapshots_path
, arg2
, path
);
1120 if (VerboseOpt
== 0) {
1124 if (VerboseOpt
== 0)
1131 cleanup_reblock(const char *path
, const char *snapshots_path
,
1132 int arg1 __unused
, int arg2
)
1134 if (VerboseOpt
== 0) {
1140 * When reblocking the B-Tree always reblock everything in normal
1144 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1145 snapshots_path
, arg2
, path
);
1146 if (VerboseOpt
== 0) {
1152 * When reblocking the inodes always reblock everything in normal
1156 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1157 snapshots_path
, arg2
, path
);
1158 if (VerboseOpt
== 0) {
1164 * When reblocking the directories always reblock everything in normal
1168 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1169 snapshots_path
, arg2
, path
);
1170 if (VerboseOpt
== 0) {
1176 * Do not reblock all the data in normal mode.
1179 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1180 snapshots_path
, arg2
, path
);
1181 if (VerboseOpt
== 0)
1188 cleanup_recopy(const char *path
, const char *snapshots_path
,
1189 int arg1 __unused
, int arg2
)
1191 if (VerboseOpt
== 0) {
1196 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1197 snapshots_path
, arg2
, path
);
1198 if (VerboseOpt
== 0) {
1203 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1204 snapshots_path
, arg2
, path
);
1205 if (VerboseOpt
== 0) {
1210 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1211 snapshots_path
, arg2
, path
);
1212 if (VerboseOpt
== 0) {
1217 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1218 snapshots_path
, arg2
, path
);
1219 if (VerboseOpt
== 0)
1226 cleanup_dedup(const char *path
, const char *snapshots_path __unused
,
1227 int arg1 __unused
, int arg2
)
1229 if (VerboseOpt
== 0) {
1234 runcmd(NULL
, "hammer -t %d dedup %s", arg2
, path
);
1235 if (VerboseOpt
== 0) {
1239 if (VerboseOpt
== 0)
1246 runcmd(int *resp
, const char *ctl
, ...)
1258 * Generate the command
1261 vasprintf(&cmd
, ctl
, va
);
1264 printf(" %s\n", cmd
);
1267 * Break us down into arguments. We do not just use system() here
1268 * because it blocks SIGINT and friends.
1272 av
= malloc(sizeof(char *) * nmax
);
1274 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
1275 if (n
== nmax
- 1) {
1277 av
= realloc(av
, sizeof(char *) * nmax
);
1287 if ((pid
= fork()) == 0) {
1288 if (VerboseOpt
< 2) {
1289 int fd
= open("/dev/null", O_RDWR
);
1295 } else if (pid
< 0) {
1300 while (waitpid(pid
, &status
, 0) != pid
)
1302 res
= WEXITSTATUS(status
);