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);
103 * WARNING: Do not make the SNAPSHOTS_BASE "/var/snapshots" because
104 * it will interfere with the older HAMMER VERS < 3 snapshots directory
107 #define SNAPSHOTS_BASE "/var/hammer" /* HAMMER VERS >= 3 */
110 struct didpfs
*FirstPFS
;
113 hammer_cmd_cleanup(char **av
, int ac
)
115 char *fstype
, *fs
, *path
;
116 struct statfs
*stfsbuf
;
121 mntsize
= getmntinfo(&stfsbuf
, MNT_NOWAIT
);
123 for (i
=0; i
< mntsize
; i
++) {
125 * We will cleanup in the case fstype is hammer.
126 * If we have null-mounted PFS, we check the
127 * mount source. If it looks like a PFS, we
128 * proceed to cleanup also.
130 fstype
= stfsbuf
[i
].f_fstypename
;
131 fs
= stfsbuf
[i
].f_mntfromname
;
132 if ((strcmp(fstype
, "hammer") == 0) ||
133 ((strcmp(fstype
, "null") == 0) &&
134 (strstr(fs
, "/@@0x") != NULL
||
135 strstr(fs
, "/@@-1") != NULL
))) {
136 path
= stfsbuf
[i
].f_mntonname
;
153 do_cleanup(const char *path
)
155 struct hammer_ioc_pseudofs_rw pfs
;
156 struct hammer_ioc_config config
;
157 struct hammer_ioc_version version
;
158 union hammer_ioc_mrecord_any mrec_tmp
;
159 char *snapshots_path
= NULL
;
172 struct didpfs
*didpfs
;
173 int snapshots_disabled
= 0;
174 int prune_warning
= 0;
176 int snapshots_from_pfs
= 0;
181 bzero(&pfs
, sizeof(pfs
));
182 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
183 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
184 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
187 printf("cleanup %-20s -", path
);
188 fd
= open(path
, O_RDONLY
);
190 printf(" unable to access directory: %s\n", strerror(errno
));
193 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
194 printf(" not a HAMMER filesystem: %s\n", strerror(errno
));
198 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
199 printf(" unrecognized HAMMER version\n");
203 bzero(&version
, sizeof(version
));
204 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
205 printf(" HAMMER filesystem but couldn't retrieve version!\n");
210 bzero(&config
, sizeof(config
));
211 if (version
.cur_version
>= 3) {
212 if (ioctl(fd
, HAMMERIOC_GET_CONFIG
, &config
) == 0 &&
213 config
.head
.error
== 0) {
219 * Make sure we have not already handled this PFS. Several nullfs
220 * mounts might alias the same PFS.
222 for (didpfs
= FirstPFS
; didpfs
; didpfs
= didpfs
->next
) {
223 if (bcmp(&didpfs
->uuid
, &mrec_tmp
.pfs
.pfsd
.unique_uuid
, sizeof(uuid_t
)) == 0) {
224 printf(" PFS #%d already handled\n", pfs
.pfs_id
);
229 didpfs
= malloc(sizeof(*didpfs
));
230 didpfs
->next
= FirstPFS
;
232 didpfs
->uuid
= mrec_tmp
.pfs
.pfsd
.unique_uuid
;
235 * Calculate the old snapshots directory for HAMMER VERSION < 3
237 * If the directory is explicitly specified in the PFS config
238 * we flag it and will not migrate it later.
240 if (mrec_tmp
.pfs
.pfsd
.snapshots
[0] == '/') {
241 asprintf(&snapshots_path
, "%s", mrec_tmp
.pfs
.pfsd
.snapshots
);
242 snapshots_from_pfs
= 1;
243 } else if (mrec_tmp
.pfs
.pfsd
.snapshots
[0]) {
244 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
247 } else if (mrec_tmp
.pfs
.pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
248 if (version
.cur_version
< 3) {
249 printf(" WARNING: must configure snapshot dir for PFS slave\n");
250 printf("\tWe suggest <fs>/var/slaves/<name> where "
251 "<fs> is the base HAMMER fs\n");
252 printf("\tcontaining the slave\n");
257 asprintf(&snapshots_path
,
258 "%s%ssnapshots", path
, dividing_slash(path
));
262 * Check for old-style config file
264 if (snapshots_path
) {
265 asprintf(&config_path
, "%s/config", snapshots_path
);
266 fp
= fopen(config_path
, "r");
270 * Handle upgrades to hammer version 3, move the config
271 * file into meta-data.
273 * For the old config read the file into the config structure,
274 * we will parse it out of the config structure regardless.
276 if (version
.cur_version
>= 3) {
278 printf("(migrating) ");
280 migrate_config(fp
, &config
);
281 migrate_snapshots(fd
, snapshots_path
);
283 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
284 printf(" cannot init meta-data config!\n");
289 } else if (new_config
== 0) {
290 config_init(path
, &config
);
291 if (ioctl(fd
, HAMMERIOC_SET_CONFIG
, &config
) < 0) {
292 printf(" cannot init meta-data config!\n");
300 * Create missing snapshots directory for HAMMER VERSION < 3
302 if (stat(snapshots_path
, &st
) < 0) {
303 if (mkdir(snapshots_path
, 0755) != 0) {
304 free(snapshots_path
);
305 printf(" unable to create snapshot dir \"%s\": %s\n",
306 snapshots_path
, strerror(errno
));
313 * Create missing config file for HAMMER VERSION < 3
316 config_init(path
, &config
);
317 fp
= fopen(config_path
, "w");
319 fwrite(config
.config
.text
, 1,
320 strlen(config
.config
.text
), fp
);
324 migrate_config(fp
, &config
);
330 * If snapshots_from_pfs is not set we calculate the new snapshots
331 * directory default (in /var) for HAMMER VERSION >= 3 and migrate
332 * the old snapshots directory over.
334 * People who have set an explicit snapshots directory will have
335 * to migrate the data manually into /var/hammer, or not bother at
336 * all. People running slaves may wish to migrate it and then
337 * clear the snapshots specification in the PFS config for the
340 if (new_config
&& snapshots_from_pfs
== 0) {
343 if (path
[0] != '/') {
344 printf(" path must start with '/'\n");
347 if (strcmp(path
, "/") == 0)
348 asprintf(&npath
, "%s/root", SNAPSHOTS_BASE
);
350 asprintf(&npath
, "%s/%s", SNAPSHOTS_BASE
, path
+ 1);
351 if (snapshots_path
) {
352 if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
353 if (stat(snapshots_path
, &st
) < 0 && errno
== ENOENT
) {
354 printf(" HAMMER UPGRADE: Creating snapshots\n"
355 "\tCreating snapshots in %s\n",
357 runcmd(&r
, "mkdir -p %s", npath
);
359 printf(" HAMMER UPGRADE: Moving snapshots\n"
360 "\tMoving snapshots from %s to %s\n",
361 snapshots_path
, npath
);
362 runcmd(&r
, "mkdir -p %s", npath
);
363 runcmd(&r
, "cpdup %s %s", snapshots_path
, npath
);
365 printf("Unable to move snapshots directory!\n");
366 printf("Please fix this critical error.\n");
367 printf("Aborting cleanup of %s\n", path
);
371 runcmd(&r
, "rm -rf %s", snapshots_path
);
374 free(snapshots_path
);
375 } else if (stat(npath
, &st
) < 0 && errno
== ENOENT
) {
376 runcmd(&r
, "mkdir -p %s", npath
);
378 snapshots_path
= npath
;
382 * Lock the PFS. fd is the base directory of the mounted PFS.
384 if (flock(fd
, LOCK_EX
|LOCK_NB
) == -1) {
385 if (errno
== EWOULDBLOCK
)
386 printf(" PFS #%d locked by other process\n", pfs
.pfs_id
);
388 printf(" can not lock %s: %s\n", config_path
, strerror(errno
));
393 printf(" handle PFS #%d using %s\n", pfs
.pfs_id
, snapshots_path
);
395 struct pidfh
*pfh
= NULL
;
396 static char pidfile
[PIDFILE_BUFSIZE
];
398 snprintf (pidfile
, PIDFILE_BUFSIZE
, "%s/hammer.cleanup.%d",
399 pidfile_loc
, getpid());
400 pfh
= pidfile_open(pidfile
, 0644, NULL
);
402 warn ("Unable to open or create %s", pidfile
);
407 * Process the config file
409 cbase
= config
.config
.text
;
411 while ((cptr
= strchr(cbase
, '\n')) != NULL
) {
412 bcopy(cbase
, buf
, cptr
- cbase
);
413 buf
[cptr
- cbase
] = 0;
416 cmd
= strtok(buf
, WS
);
417 if (cmd
== NULL
|| cmd
[0] == '#')
423 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
424 arg1
= strtosecs(ptr
);
425 if ((ptr
= strtok(NULL
, WS
)) != NULL
) {
426 arg2
= strtosecs(ptr
);
427 arg3
= strtok(NULL
, WS
);
431 printf("%20s - ", cmd
);
435 if (strcmp(cmd
, "snapshots") == 0) {
438 check_softlinks(fd
, new_config
,
440 printf("only removing old snapshots\n");
442 cleanup_softlinks(fd
, new_config
,
446 printf("disabled\n");
447 snapshots_disabled
= 1;
450 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
452 cleanup_softlinks(fd
, new_config
,
455 r
= create_snapshot(path
, snapshots_path
);
459 } else if (arg1
== 0) {
461 * The commands following this check can't handle
462 * a period of 0, so call the feature disabled and
463 * ignore the directive.
465 printf("disabled\n");
466 } else if (strcmp(cmd
, "prune") == 0) {
467 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
469 printf("run - WARNING snapshot "
471 "but snapshots disabled\n");
475 r
= cleanup_prune(path
, snapshots_path
,
476 arg1
, arg2
, snapshots_disabled
);
480 } else if (strcmp(cmd
, "rebalance") == 0) {
482 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
487 r
= cleanup_rebalance(path
, snapshots_path
,
492 } else if (strcmp(cmd
, "reblock") == 0) {
493 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
498 r
= cleanup_reblock(path
, snapshots_path
,
503 } else if (strcmp(cmd
, "recopy") == 0) {
504 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
509 r
= cleanup_recopy(path
, snapshots_path
,
514 } else if (strcmp(cmd
, "dedup") == 0) {
515 if (check_period(snapshots_path
, cmd
, arg1
, &savet
)) {
520 r
= cleanup_dedup(path
, snapshots_path
,
526 printf("unknown directive\n");
530 save_period(snapshots_path
, cmd
, savet
);
534 * Add new rebalance feature if the config doesn't have it.
535 * (old style config only).
537 if (new_config
== 0 && found_rebal
== 0) {
538 if ((fp
= fopen(config_path
, "r+")) != NULL
) {
540 fprintf(fp
, "rebalance 1d 5m\n");
546 * Cleanup, and delay a little
555 * Initialize new config data (new or old style)
558 config_init(const char *path
, struct hammer_ioc_config
*config
)
560 const char *snapshots
;
562 if (strcmp(path
, "/tmp") == 0 ||
563 strcmp(path
, "/var/tmp") == 0 ||
564 strcmp(path
, "/usr/obj") == 0) {
565 snapshots
= "snapshots 0d 0d\n";
567 snapshots
= "snapshots 1d 60d\n";
569 bzero(config
->config
.text
, sizeof(config
->config
.text
));
570 snprintf(config
->config
.text
, sizeof(config
->config
.text
) - 1, "%s%s",
580 * Migrate configuration data from the old snapshots/config
581 * file to the new meta-data format.
584 migrate_config(FILE *fp
, struct hammer_ioc_config
*config
)
588 n
= fread(config
->config
.text
, 1, sizeof(config
->config
.text
) - 1, fp
);
590 bzero(config
->config
.text
+ n
, sizeof(config
->config
.text
) - n
);
594 * Migrate snapshot softlinks in the snapshots directory to the
595 * new meta-data format. The softlinks are left intact, but
596 * this way the pruning code won't lose track of them if you
597 * happen to blow away the snapshots directory.
600 migrate_snapshots(int fd
, const char *snapshots_path
)
602 struct hammer_ioc_snapshot snapshot
;
608 bzero(&snapshot
, sizeof(snapshot
));
610 if ((dir
= opendir(snapshots_path
)) != NULL
) {
611 while ((den
= readdir(dir
)) != NULL
) {
612 if (den
->d_name
[0] == '.')
614 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
615 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
616 migrate_one_snapshot(fd
, fpath
, &snapshot
);
622 migrate_one_snapshot(fd
, NULL
, &snapshot
);
627 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
628 * otherwise it is flushed when it fills up.
631 migrate_one_snapshot(int fd
, const char *fpath
,
632 struct hammer_ioc_snapshot
*snapshot
)
635 struct hammer_snapshot_data
*snap
;
649 tid
= (hammer_tid_t
)(int64_t)-1;
651 /* fpath may contain directory components */
652 if ((ptr
= strrchr(fpath
, '/')) != NULL
)
656 while (*ptr
&& *ptr
!= '-' && *ptr
!= '.')
660 r
= sscanf(ptr
, "%4d%2d%2d-%2d%2d",
661 &year
, &month
, &day
, &hour
, &minute
);
664 bzero(&tm
, sizeof(tm
));
669 tm
.tm_mon
= month
- 1;
670 tm
.tm_year
= year
- 1900;
673 bzero(linkbuf
, sizeof(linkbuf
));
674 if (readlink(fpath
, linkbuf
, sizeof(linkbuf
) - 1) > 0 &&
675 (ptr
= strrchr(linkbuf
, '@')) != NULL
&&
676 ptr
> linkbuf
&& ptr
[-1] == '@') {
677 tid
= strtoull(ptr
+ 1, NULL
, 16);
679 if (t
!= (time_t)-1 && tid
!= (hammer_tid_t
)(int64_t)-1) {
680 snap
= &snapshot
->snaps
[snapshot
->count
];
681 bzero(snap
, sizeof(*snap
));
683 snap
->ts
= (uint64_t)t
* 1000000ULL;
684 snprintf(snap
->label
, sizeof(snap
->label
),
688 printf(" non-canonical snapshot softlink: %s->%s\n",
693 if ((fpath
== NULL
&& snapshot
->count
) ||
694 snapshot
->count
== HAMMER_SNAPS_PER_IOCTL
) {
695 printf(" (%d snapshots)", snapshot
->count
);
697 if (ioctl(fd
, HAMMERIOC_ADD_SNAPSHOT
, snapshot
) < 0) {
698 printf(" Ioctl to migrate snapshots failed: %s\n",
700 } else if (snapshot
->head
.error
== EALREADY
) {
703 } else if (snapshot
->head
.error
) {
704 printf(" Ioctl to migrate snapshots failed: %s\n",
705 strerror(snapshot
->head
.error
));
707 printf("index %d\n", snapshot
->index
);
710 snapshot
->head
.error
= 0;
720 val
= strtol(ptr
, &ptr
, 0);
734 errx(1, "illegal suffix converting %s\n", ptr
);
741 dividing_slash(const char *path
)
743 int len
= strlen(path
);
744 if (len
&& path
[len
-1] == '/')
751 * Check whether the desired period has elapsed since the last successful
752 * run. The run may take a while and cross a boundary so we remember the
753 * current time_t so we can save it later on.
755 * Periods in minutes, hours, or days are assumed to have been crossed
756 * if the local time crosses a minute, hour, or day boundary regardless
757 * of how close the last operation actually was.
759 * If ForceOpt is set always return true.
762 check_period(const char *snapshots_path
, const char *cmd
, int arg1
,
773 localtime_r(savep
, &tp1
);
782 * Retrieve the start time of the last successful operation.
784 asprintf(&check_path
, "%s/.%s.period", snapshots_path
, cmd
);
785 fp
= fopen(check_path
, "r");
789 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
795 lastt
= strtol(buf
, NULL
, 0);
796 localtime_r(&lastt
, &tp2
);
799 * Normalize the times. e.g. if asked to do something on a 1-day
800 * interval the operation will be performed as soon as the day
801 * turns over relative to the previous operation, even if the previous
802 * operation ran a few seconds ago just before midnight.
804 if (arg1
% 60 == 0) {
808 if (arg1
% (60 * 60) == 0) {
812 if (arg1
% (24 * 60 * 60) == 0) {
817 baset
= mktime(&tp1
);
818 lastt
= mktime(&tp2
);
821 printf("%lld vs %lld\n", (long long)(baset
- lastt
), (long long)arg1
);
824 if ((int)(baset
- lastt
) >= arg1
)
830 * 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
858 check_softlinks(int fd
, int new_config
, const char *snapshots_path
)
867 * Old-style softlink-based snapshots
869 if ((dir
= opendir(snapshots_path
)) != NULL
) {
870 while ((den
= readdir(dir
)) != NULL
) {
871 if (den
->d_name
[0] == '.')
873 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
874 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
))
882 * New-style snapshots are stored as filesystem meta-data,
886 struct hammer_ioc_snapshot snapshot
;
888 bzero(&snapshot
, sizeof(snapshot
));
890 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
891 err(2, "hammer cleanup: check_softlink "
895 res
+= snapshot
.count
;
896 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
902 * Clean up expired softlinks in the snapshots dir
905 cleanup_softlinks(int fd
, int new_config
,
906 const char *snapshots_path
, int arg2
, char *arg3
)
914 if (arg3
!= NULL
&& strstr(arg3
, "any") != NULL
)
917 if ((dir
= opendir(snapshots_path
)) != NULL
) {
918 while ((den
= readdir(dir
)) != NULL
) {
919 if (den
->d_name
[0] == '.')
921 asprintf(&fpath
, "%s/%s", snapshots_path
, den
->d_name
);
922 if (lstat(fpath
, &st
) == 0 && S_ISLNK(st
.st_mode
) &&
923 (anylink
|| strncmp(den
->d_name
, "snap-", 5) == 0)) {
924 if (check_expired(den
->d_name
, arg2
)) {
926 printf(" expire %s\n",
938 * New-style snapshots are stored as filesystem meta-data,
942 struct hammer_ioc_snapshot snapshot
;
943 struct hammer_ioc_snapshot dsnapshot
;
944 struct hammer_snapshot_data
*snap
;
951 bzero(&snapshot
, sizeof(snapshot
));
952 bzero(&dsnapshot
, sizeof(dsnapshot
));
954 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
955 err(2, "hammer cleanup: check_softlink "
959 for (i
= 0; i
< snapshot
.count
; ++i
) {
960 snap
= &snapshot
.snaps
[i
];
961 t
= snap
->ts
/ 1000000ULL;
963 if ((int)dt
> arg2
|| snap
->tid
== 0) {
964 dsnapshot
.snaps
[dsnapshot
.count
++] =
967 if ((int)dt
> arg2
&& VerboseOpt
) {
969 strftime(snapts
, sizeof(snapts
),
970 "%Y-%m-%d %H:%M:%S %Z", tp
);
971 printf(" expire 0x%016jx %s %s\n",
972 (uintmax_t)snap
->tid
,
976 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
)
977 delete_snapshots(fd
, &dsnapshot
);
979 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
982 delete_snapshots(fd
, &dsnapshot
);
987 delete_snapshots(int fd
, struct hammer_ioc_snapshot
*dsnapshot
)
990 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, dsnapshot
) < 0) {
991 printf(" Ioctl to delete snapshots failed: %s\n",
995 if (dsnapshot
->head
.error
) {
996 printf(" Ioctl to delete snapshots failed at "
998 dsnapshot
->snaps
[dsnapshot
->index
].tid
,
999 strerror(dsnapshot
->head
.error
));
1000 if (++dsnapshot
->index
< dsnapshot
->count
)
1005 dsnapshot
->index
= 0;
1006 dsnapshot
->count
= 0;
1007 dsnapshot
->head
.error
= 0;
1011 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
1012 * expiration in seconds (arg2) and return non-zero if the softlink
1016 check_expired(const char *fpath
, int arg2
)
1027 while (*fpath
&& *fpath
!= '-' && *fpath
!= '.')
1032 r
= sscanf(fpath
, "%4d%2d%2d-%2d%2d",
1033 &year
, &month
, &day
, &hour
, &minute
);
1036 bzero(&tm
, sizeof(tm
));
1041 tm
.tm_mon
= month
- 1;
1042 tm
.tm_year
= year
- 1900;
1044 if (t
== (time_t)-1)
1057 create_snapshot(const char *path
, const char *snapshots_path
)
1061 runcmd(&r
, "hammer snapshot %s %s", path
, snapshots_path
);
1066 cleanup_prune(const char *path
, const char *snapshots_path
,
1067 int arg1 __unused
, int arg2
, int snapshots_disabled
)
1069 const char *path_or_snapshots_path
;
1070 struct softprune
*base
= NULL
;
1071 struct hammer_ioc_prune dummy_template
;
1073 bzero(&dummy_template
, sizeof(dummy_template
));
1074 hammer_softprune_scandir(&base
, &dummy_template
, snapshots_path
);
1077 * If the snapshots_path (e.g. /var/hammer/...) has no snapshots
1078 * in it then prune will get confused and prune the filesystem
1079 * containing the snapshots_path instead of the requested
1080 * filesystem. De-confuse prune. We need a better way.
1082 path_or_snapshots_path
= base
? snapshots_path
: path
;
1085 * If snapshots have been disabled run prune-everything instead
1088 if (snapshots_disabled
&& arg2
) {
1090 "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1091 snapshots_path
, arg2
, path
);
1092 } else if (snapshots_disabled
) {
1093 runcmd(NULL
, "hammer prune-everything %s", path
);
1095 runcmd(NULL
, "hammer -c %s/.prune.cycle -t %d prune %s",
1096 snapshots_path
, arg2
, path_or_snapshots_path
);
1098 runcmd(NULL
, "hammer prune %s", path_or_snapshots_path
);
1104 cleanup_rebalance(const char *path
, const char *snapshots_path
,
1105 int arg1 __unused
, int arg2
)
1107 if (VerboseOpt
== 0) {
1113 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1114 snapshots_path
, arg2
, path
);
1115 if (VerboseOpt
== 0) {
1119 if (VerboseOpt
== 0)
1125 cleanup_reblock(const char *path
, const char *snapshots_path
,
1126 int arg1 __unused
, int arg2
)
1128 if (VerboseOpt
== 0) {
1134 * When reblocking the B-Tree always reblock everything in normal
1138 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1139 snapshots_path
, arg2
, path
);
1140 if (VerboseOpt
== 0) {
1146 * When reblocking the inodes always reblock everything in normal
1150 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1151 snapshots_path
, arg2
, path
);
1152 if (VerboseOpt
== 0) {
1158 * When reblocking the directories always reblock everything in normal
1162 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1163 snapshots_path
, arg2
, path
);
1164 if (VerboseOpt
== 0) {
1170 * Do not reblock all the data in normal mode.
1173 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1174 snapshots_path
, arg2
, path
);
1175 if (VerboseOpt
== 0)
1181 cleanup_recopy(const char *path
, const char *snapshots_path
,
1182 int arg1 __unused
, int arg2
)
1184 if (VerboseOpt
== 0) {
1189 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1190 snapshots_path
, arg2
, path
);
1191 if (VerboseOpt
== 0) {
1196 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1197 snapshots_path
, arg2
, path
);
1198 if (VerboseOpt
== 0) {
1203 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1204 snapshots_path
, arg2
, path
);
1205 if (VerboseOpt
== 0) {
1210 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1211 snapshots_path
, arg2
, path
);
1212 if (VerboseOpt
== 0)
1218 cleanup_dedup(const char *path
, const char *snapshots_path __unused
,
1219 int arg1 __unused
, int arg2
)
1221 if (VerboseOpt
== 0) {
1226 runcmd(NULL
, "hammer -t %d dedup %s", arg2
, path
);
1227 if (VerboseOpt
== 0) {
1231 if (VerboseOpt
== 0)
1238 runcmd(int *resp
, const char *ctl
, ...)
1250 * Generate the command
1253 vasprintf(&cmd
, ctl
, va
);
1256 printf(" %s\n", cmd
);
1259 * Break us down into arguments. We do not just use system() here
1260 * because it blocks SIGINT and friends.
1264 av
= malloc(sizeof(char *) * nmax
);
1266 for (arg
= strtok(cmd
, WS
); arg
; arg
= strtok(NULL
, WS
)) {
1267 if (n
== nmax
- 1) {
1269 av
= realloc(av
, sizeof(char *) * nmax
);
1279 if ((pid
= fork()) == 0) {
1280 if (VerboseOpt
< 2) {
1281 int fd
= open("/dev/null", O_RDWR
);
1287 } else if (pid
< 0) {
1292 while (waitpid(pid
, &status
, 0) != pid
)
1294 res
= WEXITSTATUS(status
);