hammer utility - Add force support to cleanup
[dragonfly.git] / sbin / hammer / cmd_cleanup.c
CommitLineData
6a6e350f
MD
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5e435c92 3 *
6a6e350f
MD
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
5e435c92 33 *
bb8e52c0 34 * $DragonFly: src/sbin/hammer/cmd_cleanup.c,v 1.6 2008/10/07 22:28:41 thomas Exp $
6a6e350f
MD
35 */
36/*
16265794 37 * Clean up specific HAMMER filesystems or all HAMMER filesystems.
6a6e350f 38 *
f6532f03 39 * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
16265794 40 * are cleaned.
6a6e350f 41 *
16265794
TN
42 * Each HAMMER filesystem may contain a configuration file. If no
43 * configuration file is present one will be created with the following
6a6e350f
MD
44 * defaults:
45 *
5e435c92 46 * snapshots 1d 60d (0d 0d for /tmp, /var/tmp, /usr/obj)
6a6e350f 47 * prune 1d 5m
f13fea8b 48 * rebalance 1d 5m
6a6e350f 49 * reblock 1d 5m
f6532f03 50 * recopy 30d 10m
6a6e350f
MD
51 *
52 * All hammer commands create and maintain cycle files in the snapshots
53 * directory.
16265794
TN
54 *
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).
6a6e350f
MD
60 */
61
62#include "hammer.h"
63
c453712a
MD
64struct didpfs {
65 struct didpfs *next;
66 uuid_t uuid;
67};
68
6a6e350f 69static void do_cleanup(const char *path);
83f2a3aa
MD
70static void config_init(const char *path, struct hammer_ioc_config *config);
71static void migrate_config(FILE *fp, struct hammer_ioc_config *config);
72static void migrate_snapshots(int fd, const char *snapshots_path);
73static void migrate_one_snapshot(int fd, const char *fpath,
74 struct hammer_ioc_snapshot *snapshot);
6a6e350f
MD
75static int strtosecs(char *ptr);
76static const char *dividing_slash(const char *path);
77static int check_period(const char *snapshots_path, const char *cmd, int arg1,
78 time_t *savep);
79static void save_period(const char *snapshots_path, const char *cmd,
80 time_t savet);
83f2a3aa
MD
81static int check_softlinks(int fd, int new_config, const char *snapshots_path);
82static void cleanup_softlinks(int fd, int new_config,
83 const char *snapshots_path, int arg2, char *arg3);
ec2aa11d 84static void delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot);
ff1c9800 85static int check_expired(const char *fpath, int arg2);
6a6e350f 86
b5ec5ad4 87static int create_snapshot(const char *path, const char *snapshots_path);
0b8bd7da 88static int cleanup_rebalance(const char *path, const char *snapshots_path,
83f2a3aa 89 int arg1, int arg2);
6a6e350f 90static int cleanup_prune(const char *path, const char *snapshots_path,
83f2a3aa 91 int arg1, int arg2, int snapshots_disabled);
6a6e350f 92static int cleanup_reblock(const char *path, const char *snapshots_path,
83f2a3aa 93 int arg1, int arg2);
6a6e350f 94static int cleanup_recopy(const char *path, const char *snapshots_path,
83f2a3aa 95 int arg1, int arg2);
6a6e350f
MD
96
97static void runcmd(int *resp, const char *ctl, ...);
98
b5ec5ad4
MD
99/*
100 * WARNING: Do not make the SNAPSHOTS_BASE "/var/snapshots" because
101 * it will interfere with the older HAMMER VERS < 3 snapshots directory
102 * for the /var PFS.
103 */
104#define SNAPSHOTS_BASE "/var/hammer" /* HAMMER VERS >= 3 */
6a6e350f 105#define WS " \t\r\n"
6a6e350f 106
c453712a 107struct didpfs *FirstPFS;
6a6e350f
MD
108
109void
110hammer_cmd_cleanup(char **av, int ac)
111{
eac446c5
MD
112 char *fstype, *fs, *path;
113 struct statfs *stfsbuf;
114 int mntsize, i;
6a6e350f
MD
115
116 tzset();
117 if (ac == 0) {
eac446c5
MD
118 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
119 if (mntsize > 0) {
120 for (i=0; i < mntsize; i++) {
121 /*
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.
126 */
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;
134 do_cleanup(path);
135 }
574066d3 136 }
6a6e350f 137 }
eac446c5 138
6a6e350f
MD
139 } else {
140 while (ac) {
141 do_cleanup(*av);
142 --ac;
143 ++av;
144 }
145 }
146}
147
148static
149void
150do_cleanup(const char *path)
151{
152 struct hammer_ioc_pseudofs_rw pfs;
83f2a3aa
MD
153 struct hammer_ioc_config config;
154 struct hammer_ioc_version version;
6a6e350f 155 union hammer_ioc_mrecord_any mrec_tmp;
82893617 156 char *snapshots_path = NULL;
6a6e350f
MD
157 char *config_path;
158 struct stat st;
159 char *cmd;
160 char *ptr;
161 int arg1;
162 int arg2;
5e435c92 163 char *arg3;
6a6e350f
MD
164 time_t savet;
165 char buf[256];
83f2a3aa
MD
166 char *cbase;
167 char *cptr;
82893617 168 FILE *fp = NULL;
c453712a 169 struct didpfs *didpfs;
c6c298a7
MD
170 int snapshots_disabled = 0;
171 int prune_warning = 0;
83f2a3aa 172 int new_config = 0;
b5ec5ad4 173 int snapshots_from_pfs = 0;
6a6e350f
MD
174 int fd;
175 int r;
0b8bd7da 176 int found_rebal = 0;
6a6e350f
MD
177
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);
182 pfs.pfs_id = -1;
183
184 printf("cleanup %-20s -", path);
185 fd = open(path, O_RDONLY);
186 if (fd < 0) {
187 printf(" unable to access directory: %s\n", strerror(errno));
188 return;
189 }
83f2a3aa 190 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
6a6e350f 191 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
83f2a3aa 192 close(fd);
6a6e350f
MD
193 return;
194 }
6a6e350f
MD
195 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
196 printf(" unrecognized HAMMER version\n");
83f2a3aa
MD
197 close(fd);
198 return;
199 }
200 bzero(&version, sizeof(version));
201 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
202 printf(" HAMMER filesystem but couldn't retrieve version!\n");
203 close(fd);
6a6e350f
MD
204 return;
205 }
206
83f2a3aa
MD
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) {
211 new_config = 1;
212 }
213 }
214
6a6e350f
MD
215 /*
216 * Make sure we have not already handled this PFS. Several nullfs
217 * mounts might alias the same PFS.
218 */
c453712a
MD
219 for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
220 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
bb8e52c0 221 printf(" PFS #%d already handled\n", pfs.pfs_id);
83f2a3aa 222 close(fd);
c453712a
MD
223 return;
224 }
6a6e350f 225 }
c453712a
MD
226 didpfs = malloc(sizeof(*didpfs));
227 didpfs->next = FirstPFS;
228 FirstPFS = didpfs;
229 didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
6a6e350f
MD
230
231 /*
b5ec5ad4
MD
232 * Calculate the old snapshots directory for HAMMER VERSION < 3
233 *
234 * If the directory is explicitly specified in the PFS config
235 * we flag it and will not migrate it later.
ff1c9800
MD
236 */
237 if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
238 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
b5ec5ad4 239 snapshots_from_pfs = 1;
ff1c9800
MD
240 } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
241 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
83f2a3aa 242 close(fd);
ff1c9800
MD
243 return;
244 } else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
82893617
TN
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");
250 close(fd);
251 return;
252 }
ff1c9800
MD
253 } else {
254 asprintf(&snapshots_path,
255 "%s%ssnapshots", path, dividing_slash(path));
256 }
257
258 /*
b5ec5ad4 259 * Check for old-style config file
6a6e350f 260 */
82893617
TN
261 if (snapshots_path) {
262 asprintf(&config_path, "%s/config", snapshots_path);
263 fp = fopen(config_path, "r");
264 }
83f2a3aa
MD
265
266 /*
267 * Handle upgrades to hammer version 3, move the config
268 * file into meta-data.
269 *
270 * For the old config read the file into the config structure,
271 * we will parse it out of the config structure regardless.
272 */
273 if (version.cur_version >= 3) {
274 if (fp) {
275 printf("(migrating) ");
276 fflush(stdout);
277 migrate_config(fp, &config);
278 migrate_snapshots(fd, snapshots_path);
279 fclose(fp);
280 if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
281 printf(" cannot init meta-data config!\n");
282 close(fd);
283 return;
284 }
285 remove(config_path);
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");
290 close(fd);
291 return;
292 }
6a6e350f 293 }
83f2a3aa
MD
294 new_config = 1;
295 } else {
b5ec5ad4
MD
296 /*
297 * Create missing snapshots directory for HAMMER VERSION < 3
298 */
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));
304 close(fd);
305 return;
306 }
307 }
308
309 /*
310 * Create missing config file for HAMMER VERSION < 3
311 */
83f2a3aa
MD
312 if (fp == NULL) {
313 config_init(path, &config);
314 fp = fopen(config_path, "w");
315 if (fp) {
316 fwrite(config.config.text, 1,
317 strlen(config.config.text), fp);
318 fclose(fp);
319 }
6a6e350f 320 } else {
83f2a3aa
MD
321 migrate_config(fp, &config);
322 fclose(fp);
6a6e350f 323 }
6a6e350f
MD
324 }
325
b5ec5ad4
MD
326 /*
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.
330 *
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
335 * slave.
336 */
337 if (new_config && snapshots_from_pfs == 0) {
338 char *npath;
339
340 assert(path[0] == '/');
341 if (strcmp(path, "/") == 0)
342 asprintf(&npath, "%s/root", SNAPSHOTS_BASE);
343 else
344 asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1);
82893617
TN
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",
350 npath);
351 runcmd(&r, "mkdir -p %s", npath);
352 } else {
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);
358 if (r != 0) {
359 printf("Unable to move snapshots directory!\n");
360 printf("Please fix this critical error.\n");
361 printf("Aborting cleanup of %s\n", path);
362 close(fd);
363 return;
364 }
365 runcmd(&r, "rm -rf %s", snapshots_path);
b5ec5ad4 366 }
b5ec5ad4 367 }
82893617
TN
368 free(snapshots_path);
369 } else if (stat(npath, &st) < 0 && errno == ENOENT) {
370 runcmd(&r, "mkdir -p %s", npath);
b5ec5ad4 371 }
b5ec5ad4
MD
372 snapshots_path = npath;
373 }
374
375 /*
376 * Lock the PFS. fd is the base directory of the mounted PFS.
377 */
83f2a3aa 378 if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
5bd5f172
SS
379 if (errno == EWOULDBLOCK)
380 printf(" PFS #%d locked by other process\n", pfs.pfs_id);
381 else
382 printf(" can not lock %s: %s\n", config_path, strerror(errno));
83f2a3aa 383 close(fd);
5bd5f172
SS
384 return;
385 }
386
ff1c9800 387 printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
6a6e350f
MD
388
389 /*
390 * Process the config file
391 */
83f2a3aa
MD
392 cbase = config.config.text;
393
394 while ((cptr = strchr(cbase, '\n')) != NULL) {
395 bcopy(cbase, buf, cptr - cbase);
396 buf[cptr - cbase] = 0;
397 cbase = cptr + 1;
398
6a6e350f 399 cmd = strtok(buf, WS);
743332ab
MD
400 if (cmd == NULL || cmd[0] == '#')
401 continue;
402
6a6e350f
MD
403 arg1 = 0;
404 arg2 = 0;
5e435c92 405 arg3 = NULL;
6a6e350f
MD
406 if ((ptr = strtok(NULL, WS)) != NULL) {
407 arg1 = strtosecs(ptr);
5e435c92 408 if ((ptr = strtok(NULL, WS)) != NULL) {
6a6e350f 409 arg2 = strtosecs(ptr);
5e435c92
MD
410 arg3 = strtok(NULL, WS);
411 }
6a6e350f
MD
412 }
413
414 printf("%20s - ", cmd);
415 fflush(stdout);
416
6a6e350f
MD
417 r = 1;
418 if (strcmp(cmd, "snapshots") == 0) {
5e435c92 419 if (arg1 == 0) {
83f2a3aa
MD
420 if (arg2 &&
421 check_softlinks(fd, new_config,
422 snapshots_path)) {
5e435c92
MD
423 printf("only removing old snapshots\n");
424 prune_warning = 1;
83f2a3aa
MD
425 cleanup_softlinks(fd, new_config,
426 snapshots_path,
5e435c92
MD
427 arg2, arg3);
428 } else {
429 printf("disabled\n");
430 snapshots_disabled = 1;
431 }
432 } else
6a6e350f
MD
433 if (check_period(snapshots_path, cmd, arg1, &savet)) {
434 printf("run\n");
83f2a3aa
MD
435 cleanup_softlinks(fd, new_config,
436 snapshots_path,
5e435c92 437 arg2, arg3);
b5ec5ad4 438 r = create_snapshot(path, snapshots_path);
6a6e350f
MD
439 } else {
440 printf("skip\n");
441 }
5e435c92
MD
442 } else if (arg1 == 0) {
443 /*
444 * The commands following this check can't handle
445 * a period of 0, so call the feature disabled and
446 * ignore the directive.
447 */
448 printf("disabled\n");
6a6e350f
MD
449 } else if (strcmp(cmd, "prune") == 0) {
450 if (check_period(snapshots_path, cmd, arg1, &savet)) {
5e435c92
MD
451 if (prune_warning) {
452 printf("run - WARNING snapshot "
453 "softlinks present "
454 "but snapshots disabled\n");
455 } else {
c6c298a7 456 printf("run\n");
5e435c92 457 }
6a6e350f 458 r = cleanup_prune(path, snapshots_path,
c6c298a7 459 arg1, arg2, snapshots_disabled);
6a6e350f
MD
460 } else {
461 printf("skip\n");
462 }
0b8bd7da
MD
463 } else if (strcmp(cmd, "rebalance") == 0) {
464 found_rebal = 1;
465 if (check_period(snapshots_path, cmd, arg1, &savet)) {
466 printf("run");
467 fflush(stdout);
468 if (VerboseOpt)
469 printf("\n");
470 r = cleanup_rebalance(path, snapshots_path,
471 arg1, arg2);
472 } else {
473 printf("skip\n");
474 }
6a6e350f
MD
475 } else if (strcmp(cmd, "reblock") == 0) {
476 if (check_period(snapshots_path, cmd, arg1, &savet)) {
477 printf("run");
478 fflush(stdout);
479 if (VerboseOpt)
480 printf("\n");
481 r = cleanup_reblock(path, snapshots_path,
482 arg1, arg2);
483 } else {
484 printf("skip\n");
485 }
486 } else if (strcmp(cmd, "recopy") == 0) {
487 if (check_period(snapshots_path, cmd, arg1, &savet)) {
488 printf("run");
489 fflush(stdout);
490 if (VerboseOpt)
491 printf("\n");
492 r = cleanup_recopy(path, snapshots_path,
493 arg1, arg2);
494 } else {
495 printf("skip\n");
496 }
497 } else {
498 printf("unknown directive\n");
499 r = 1;
500 }
501 if (r == 0)
502 save_period(snapshots_path, cmd, savet);
503 }
0b8bd7da
MD
504
505 /*
83f2a3aa 506 * Add new rebalance feature if the config doesn't have it.
b5ec5ad4 507 * (old style config only).
0b8bd7da 508 */
83f2a3aa 509 if (new_config == 0 && found_rebal == 0) {
0b8bd7da
MD
510 if ((fp = fopen(config_path, "r+")) != NULL) {
511 fseek(fp, 0L, 2);
512 fprintf(fp, "rebalance 1d 5m\n");
513 fclose(fp);
514 }
515 }
83f2a3aa
MD
516
517 /*
518 * Cleanup, and delay a little
519 */
520 close(fd);
6a6e350f
MD
521 usleep(1000);
522}
523
83f2a3aa
MD
524/*
525 * Initialize new config data (new or old style)
526 */
527static void
528config_init(const char *path, struct hammer_ioc_config *config)
529{
530 const char *snapshots;
531
532 if (strcmp(path, "/tmp") == 0 ||
533 strcmp(path, "/var/tmp") == 0 ||
534 strcmp(path, "/usr/obj") == 0) {
535 snapshots = "snapshots 0d 0d\n";
536 } else {
537 snapshots = "snapshots 1d 60d\n";
538 }
539 bzero(config->config.text, sizeof(config->config.text));
540 snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s",
541 snapshots,
542 "prune 1d 5m\n"
543 "rebalance 1d 5m\n"
544 "reblock 1d 5m\n"
f6532f03 545 "recopy 30d 10m\n");
83f2a3aa
MD
546}
547
548/*
549 * Migrate configuration data from the old snapshots/config
16265794 550 * file to the new meta-data format.
83f2a3aa
MD
551 */
552static void
553migrate_config(FILE *fp, struct hammer_ioc_config *config)
554{
555 int n;
556
557 n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp);
558 if (n >= 0)
559 bzero(config->config.text + n, sizeof(config->config.text) - n);
560}
561
562/*
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.
567 */
568static void
569migrate_snapshots(int fd, const char *snapshots_path)
570{
571 struct hammer_ioc_snapshot snapshot;
572 struct dirent *den;
573 struct stat st;
574 DIR *dir;
575 char *fpath;
576
577 bzero(&snapshot, sizeof(snapshot));
578
579 if ((dir = opendir(snapshots_path)) != NULL) {
580 while ((den = readdir(dir)) != NULL) {
581 if (den->d_name[0] == '.')
582 continue;
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);
586 }
587 free(fpath);
588 }
589 closedir(dir);
590 }
591 migrate_one_snapshot(fd, NULL, &snapshot);
592
593}
594
595/*
596 * Migrate a single snapshot. If fpath is NULL the ioctl is flushed,
597 * otherwise it is flushed when it fills up.
598 */
599static void
600migrate_one_snapshot(int fd, const char *fpath,
601 struct hammer_ioc_snapshot *snapshot)
602{
603 if (fpath) {
604 struct hammer_snapshot_data *snap;
605 struct tm tm;
606 time_t t;
607 int year;
608 int month;
609 int day = 0;
610 int hour = 0;
611 int minute = 0;
612 int r;
613 char linkbuf[1024];
614 const char *ptr;
615 hammer_tid_t tid;
616
617 t = (time_t)-1;
618 tid = (hammer_tid_t)(int64_t)-1;
619
f510bf3e
YT
620 /* fpath may contain directory components */
621 if ((ptr = strrchr(fpath, '/')) != NULL)
622 ++ptr;
623 else
624 ptr = fpath;
83f2a3aa
MD
625 while (*ptr && *ptr != '-' && *ptr != '.')
626 ++ptr;
627 if (*ptr)
628 ++ptr;
629 r = sscanf(ptr, "%4d%2d%2d-%2d%2d",
630 &year, &month, &day, &hour, &minute);
631
632 if (r >= 3) {
633 bzero(&tm, sizeof(tm));
634 tm.tm_isdst = -1;
635 tm.tm_min = minute;
636 tm.tm_hour = hour;
637 tm.tm_mday = day;
638 tm.tm_mon = month - 1;
639 tm.tm_year = year - 1900;
640 t = mktime(&tm);
641 }
642 bzero(linkbuf, sizeof(linkbuf));
643 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 &&
644 (ptr = strrchr(linkbuf, '@')) != NULL &&
645 ptr > linkbuf && ptr[-1] == '@') {
646 tid = strtoull(ptr + 1, NULL, 16);
647 }
648 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) {
649 snap = &snapshot->snaps[snapshot->count];
650 bzero(snap, sizeof(*snap));
651 snap->tid = tid;
652 snap->ts = (u_int64_t)t * 1000000ULL;
653 snprintf(snap->label, sizeof(snap->label),
654 "migrated");
655 ++snapshot->count;
f510bf3e
YT
656 } else {
657 printf(" non-canonical snapshot softlink: %s->%s\n",
658 fpath, linkbuf);
83f2a3aa
MD
659 }
660 }
661
662 if ((fpath == NULL && snapshot->count) ||
663 snapshot->count == HAMMER_SNAPS_PER_IOCTL) {
664 printf(" (%d snapshots)", snapshot->count);
665again:
666 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) {
667 printf(" Ioctl to migrate snapshots failed: %s\n",
668 strerror(errno));
669 } else if (snapshot->head.error == EALREADY) {
670 ++snapshot->index;
671 goto again;
672 } else if (snapshot->head.error) {
068da2c2 673 printf(" Ioctl to migrate snapshots failed: %s\n",
83f2a3aa
MD
674 strerror(snapshot->head.error));
675 }
676 printf("index %d\n", snapshot->index);
677 snapshot->index = 0;
678 snapshot->count = 0;
679 snapshot->head.error = 0;
680 }
681}
682
6a6e350f
MD
683static
684int
685strtosecs(char *ptr)
686{
687 int val;
688
689 val = strtol(ptr, &ptr, 0);
690 switch(*ptr) {
691 case 'd':
692 val *= 24;
693 /* fall through */
694 case 'h':
695 val *= 60;
696 /* fall through */
697 case 'm':
698 val *= 60;
699 /* fall through */
700 case 's':
701 break;
702 default:
703 errx(1, "illegal suffix converting %s\n", ptr);
704 break;
705 }
706 return(val);
707}
708
709static const char *
710dividing_slash(const char *path)
711{
712 int len = strlen(path);
713 if (len && path[len-1] == '/')
714 return("");
715 else
716 return("/");
717}
718
719/*
720 * Check whether the desired period has elapsed since the last successful
721 * run. The run may take a while and cross a boundary so we remember the
722 * current time_t so we can save it later on.
723 *
724 * Periods in minutes, hours, or days are assumed to have been crossed
725 * if the local time crosses a minute, hour, or day boundary regardless
726 * of how close the last operation actually was.
bdc54107
MD
727 *
728 * If ForceOpt is set always return true.
6a6e350f
MD
729 */
730static int
731check_period(const char *snapshots_path, const char *cmd, int arg1,
732 time_t *savep)
733{
734 char *check_path;
735 struct tm tp1;
736 struct tm tp2;
737 FILE *fp;
738 time_t baset, lastt;
739 char buf[256];
740
741 time(savep);
742 localtime_r(savep, &tp1);
743
744 /*
bdc54107
MD
745 * Force run if -F
746 */
747 if (ForceOpt)
748 return(1);
749
750 /*
6a6e350f
MD
751 * Retrieve the start time of the last successful operation.
752 */
753 asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
754 fp = fopen(check_path, "r");
755 free(check_path);
756 if (fp == NULL)
757 return(1);
758 if (fgets(buf, sizeof(buf), fp) == NULL) {
759 fclose(fp);
760 return(1);
761 }
762 fclose(fp);
763
764 lastt = strtol(buf, NULL, 0);
765 localtime_r(&lastt, &tp2);
766
767 /*
768 * Normalize the times. e.g. if asked to do something on a 1-day
769 * interval the operation will be performed as soon as the day
770 * turns over relative to the previous operation, even if the previous
771 * operation ran a few seconds ago just before midnight.
772 */
773 if (arg1 % 60 == 0) {
774 tp1.tm_sec = 0;
775 tp2.tm_sec = 0;
776 }
777 if (arg1 % (60 * 60) == 0) {
778 tp1.tm_min = 0;
779 tp2.tm_min = 0;
780 }
781 if (arg1 % (24 * 60 * 60) == 0) {
782 tp1.tm_hour = 0;
783 tp2.tm_hour = 0;
784 }
785
786 baset = mktime(&tp1);
787 lastt = mktime(&tp2);
788
789#if 0
790 printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
791#endif
792
793 if ((int)(baset - lastt) >= arg1)
794 return(1);
795 return(0);
796}
797
798/*
799 * Store the start time of the last successful operation.
800 */
801static void
802save_period(const char *snapshots_path, const char *cmd,
803 time_t savet)
804{
805 char *ocheck_path;
806 char *ncheck_path;
807 FILE *fp;
808
809 asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
810 asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
811 fp = fopen(ncheck_path, "w");
3b9fbdea
MD
812 if (fp) {
813 fprintf(fp, "0x%08llx\n", (long long)savet);
814 if (fclose(fp) == 0)
815 rename(ncheck_path, ocheck_path);
816 remove(ncheck_path);
817 } else {
818 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
819 ncheck_path, strerror(errno));
820 }
6a6e350f
MD
821}
822
ff1c9800
MD
823/*
824 * Simply count the number of softlinks in the snapshots dir
825 */
c6c298a7 826static int
83f2a3aa 827check_softlinks(int fd, int new_config, const char *snapshots_path)
c6c298a7
MD
828{
829 struct dirent *den;
830 struct stat st;
831 DIR *dir;
832 char *fpath;
833 int res = 0;
834
83f2a3aa
MD
835 /*
836 * Old-style softlink-based snapshots
837 */
c6c298a7
MD
838 if ((dir = opendir(snapshots_path)) != NULL) {
839 while ((den = readdir(dir)) != NULL) {
840 if (den->d_name[0] == '.')
841 continue;
842 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
843 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
844 ++res;
845 free(fpath);
846 }
847 closedir(dir);
848 }
83f2a3aa
MD
849
850 /*
851 * New-style snapshots are stored as filesystem meta-data,
852 * count those too.
853 */
854 if (new_config) {
855 struct hammer_ioc_snapshot snapshot;
856
857 bzero(&snapshot, sizeof(snapshot));
858 do {
859 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
860 err(2, "hammer cleanup: check_softlink "
861 "snapshot error");
862 /* not reached */
863 }
864 res += snapshot.count;
865 } while (snapshot.head.error == 0 && snapshot.count);
866 }
867 return (res);
c6c298a7
MD
868}
869
6a6e350f 870/*
ff1c9800
MD
871 * Clean up expired softlinks in the snapshots dir
872 */
873static void
83f2a3aa
MD
874cleanup_softlinks(int fd, int new_config,
875 const char *snapshots_path, int arg2, char *arg3)
ff1c9800
MD
876{
877 struct dirent *den;
878 struct stat st;
879 DIR *dir;
880 char *fpath;
5e435c92
MD
881 int anylink = 0;
882
09e1b0d6 883 if (arg3 != NULL && strstr(arg3, "any") != NULL)
5e435c92 884 anylink = 1;
ff1c9800
MD
885
886 if ((dir = opendir(snapshots_path)) != NULL) {
887 while ((den = readdir(dir)) != NULL) {
888 if (den->d_name[0] == '.')
889 continue;
890 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
891 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
5e435c92
MD
892 (anylink || strncmp(den->d_name, "snap-", 5) == 0)
893 ) {
ff1c9800
MD
894 if (check_expired(den->d_name, arg2)) {
895 if (VerboseOpt) {
896 printf(" expire %s\n",
897 fpath);
898 }
899 remove(fpath);
900 }
901 }
902 free(fpath);
903 }
904 closedir(dir);
905 }
83f2a3aa
MD
906
907 /*
908 * New-style snapshots are stored as filesystem meta-data,
909 * count those too.
910 */
911 if (new_config) {
912 struct hammer_ioc_snapshot snapshot;
913 struct hammer_ioc_snapshot dsnapshot;
914 struct hammer_snapshot_data *snap;
915 struct tm *tp;
916 time_t t;
ec2aa11d 917 time_t dt;
83f2a3aa
MD
918 char snapts[32];
919 u_int32_t i;
920
921 bzero(&snapshot, sizeof(snapshot));
922 bzero(&dsnapshot, sizeof(dsnapshot));
923 do {
924 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
925 err(2, "hammer cleanup: check_softlink "
926 "snapshot error");
927 /* not reached */
928 }
929 for (i = 0; i < snapshot.count; ++i) {
930 snap = &snapshot.snaps[i];
ec2aa11d
MD
931 t = snap->ts / 1000000ULL;
932 dt = time(NULL) - t;
933 if ((int)dt > arg2 || snap->tid == 0) {
83f2a3aa
MD
934 dsnapshot.snaps[dsnapshot.count++] =
935 *snap;
936 }
ec2aa11d 937 if ((int)dt > arg2 && VerboseOpt) {
83f2a3aa
MD
938 tp = localtime(&t);
939 strftime(snapts, sizeof(snapts),
940 "%Y-%m-%d %H:%M:%S %Z", tp);
941 printf(" expire 0x%016jx %s %s\n",
942 (uintmax_t)snap->tid,
943 snapts,
944 snap->label);
945 }
ec2aa11d
MD
946 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
947 delete_snapshots(fd, &dsnapshot);
83f2a3aa
MD
948 }
949 } while (snapshot.head.error == 0 && snapshot.count);
950
ec2aa11d
MD
951 if (dsnapshot.count)
952 delete_snapshots(fd, &dsnapshot);
953 }
954}
955
956static void
957delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot)
958{
959 for (;;) {
960 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) {
961 printf(" Ioctl to delete snapshots failed: %s\n",
962 strerror(errno));
963 break;
83f2a3aa 964 }
ec2aa11d
MD
965 if (dsnapshot->head.error) {
966 printf(" Ioctl to delete snapshots failed at "
967 "snap=%016jx: %s\n",
968 dsnapshot->snaps[dsnapshot->index].tid,
969 strerror(dsnapshot->head.error));
970 if (++dsnapshot->index < dsnapshot->count)
971 continue;
972 }
973 break;
83f2a3aa 974 }
ec2aa11d
MD
975 dsnapshot->index = 0;
976 dsnapshot->count = 0;
977 dsnapshot->head.error = 0;
ff1c9800
MD
978}
979
980/*
981 * Take a softlink path in the form snap-yyyymmdd-hhmm and the
982 * expiration in seconds (arg2) and return non-zero if the softlink
983 * has expired.
984 */
985static int
986check_expired(const char *fpath, int arg2)
987{
988 struct tm tm;
989 time_t t;
990 int year;
991 int month;
5e435c92
MD
992 int day = 0;
993 int hour = 0;
994 int minute = 0;
ff1c9800
MD
995 int r;
996
5e435c92
MD
997 while (*fpath && *fpath != '-' && *fpath != '.')
998 ++fpath;
999 if (*fpath)
1000 ++fpath;
1001
1002 r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
ff1c9800 1003 &year, &month, &day, &hour, &minute);
5e435c92
MD
1004
1005 if (r >= 3) {
ff1c9800
MD
1006 bzero(&tm, sizeof(tm));
1007 tm.tm_isdst = -1;
1008 tm.tm_min = minute;
1009 tm.tm_hour = hour;
1010 tm.tm_mday = day;
1011 tm.tm_mon = month - 1;
1012 tm.tm_year = year - 1900;
5e435c92
MD
1013 t = mktime(&tm);
1014 if (t == (time_t)-1)
1015 return(0);
1016 t = time(NULL) - t;
ff1c9800
MD
1017 if ((int)t > arg2)
1018 return(1);
1019 }
1020 return(0);
1021}
1022
1023/*
6a6e350f
MD
1024 * Issue a snapshot.
1025 */
1026static int
b5ec5ad4 1027create_snapshot(const char *path, const char *snapshots_path)
6a6e350f
MD
1028{
1029 int r;
1030
b5ec5ad4 1031 runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
6a6e350f
MD
1032 return(r);
1033}
1034
1035static int
1036cleanup_prune(const char *path __unused, const char *snapshots_path,
c6c298a7 1037 int arg1 __unused, int arg2, int snapshots_disabled)
6a6e350f 1038{
c6c298a7
MD
1039 /*
1040 * If snapshots have been disabled run prune-everything instead
1041 * of prune.
1042 */
1043 if (snapshots_disabled && arg2) {
1044 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1045 snapshots_path, arg2, path);
1046 } else if (snapshots_disabled) {
1047 runcmd(NULL, "hammer prune-everything %s", path);
1048 } else if (arg2) {
6a6e350f
MD
1049 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
1050 snapshots_path, arg2, snapshots_path);
1051 } else {
1052 runcmd(NULL, "hammer prune %s", snapshots_path);
1053 }
1054 return(0);
1055}
1056
1057static int
0b8bd7da
MD
1058cleanup_rebalance(const char *path, const char *snapshots_path,
1059 int arg1 __unused, int arg2)
1060{
1061 if (VerboseOpt == 0) {
1062 printf(".");
1063 fflush(stdout);
1064 }
1065
1066 runcmd(NULL,
1067 "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1068 snapshots_path, arg2, path);
1069 if (VerboseOpt == 0) {
1070 printf(".");
1071 fflush(stdout);
1072 }
1073 if (VerboseOpt == 0)
1074 printf("\n");
1075 return(0);
1076}
1077
1078static int
6a6e350f
MD
1079cleanup_reblock(const char *path, const char *snapshots_path,
1080 int arg1 __unused, int arg2)
1081{
1082 if (VerboseOpt == 0) {
1083 printf(".");
1084 fflush(stdout);
1085 }
797a0b63
MD
1086
1087 /*
1088 * When reblocking the B-Tree always reblock everything in normal
1089 * mode.
1090 */
1091 runcmd(NULL,
1092 "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1093 snapshots_path, arg2, path);
1094 if (VerboseOpt == 0) {
1095 printf(".");
1096 fflush(stdout);
1097 }
1098
1099 /*
1100 * When reblocking the inodes always reblock everything in normal
1101 * mode.
1102 */
6a6e350f 1103 runcmd(NULL,
797a0b63 1104 "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
6a6e350f
MD
1105 snapshots_path, arg2, path);
1106 if (VerboseOpt == 0) {
1107 printf(".");
1108 fflush(stdout);
1109 }
797a0b63
MD
1110
1111 /*
1112 * When reblocking the directories always reblock everything in normal
1113 * mode.
1114 */
6a6e350f 1115 runcmd(NULL,
797a0b63 1116 "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
6a6e350f
MD
1117 snapshots_path, arg2, path);
1118 if (VerboseOpt == 0) {
1119 printf(".");
1120 fflush(stdout);
1121 }
797a0b63
MD
1122
1123 /*
1124 * Do not reblock all the data in normal mode.
1125 */
6a6e350f
MD
1126 runcmd(NULL,
1127 "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1128 snapshots_path, arg2, path);
1129 if (VerboseOpt == 0)
1130 printf("\n");
1131 return(0);
1132}
1133
1134static int
1135cleanup_recopy(const char *path, const char *snapshots_path,
1136 int arg1 __unused, int arg2)
1137{
1138 if (VerboseOpt == 0) {
1139 printf(".");
1140 fflush(stdout);
1141 }
1142 runcmd(NULL,
1143 "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1144 snapshots_path, arg2, path);
1145 if (VerboseOpt == 0) {
1146 printf(".");
1147 fflush(stdout);
1148 }
1149 runcmd(NULL,
1150 "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1151 snapshots_path, arg2, path);
1152 if (VerboseOpt == 0) {
1153 printf(".");
1154 fflush(stdout);
1155 }
1156 runcmd(NULL,
797a0b63
MD
1157 "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1158 snapshots_path, arg2, path);
1159 if (VerboseOpt == 0) {
1160 printf(".");
1161 fflush(stdout);
1162 }
1163 runcmd(NULL,
6a6e350f
MD
1164 "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1165 snapshots_path, arg2, path);
1166 if (VerboseOpt == 0)
1167 printf("\n");
1168 return(0);
1169}
1170
1171static
1172void
1173runcmd(int *resp, const char *ctl, ...)
1174{
1175 va_list va;
1176 char *cmd;
1177 char *arg;
1178 char **av;
1179 int n;
1180 int nmax;
1181 int res;
1182 pid_t pid;
1183
1184 /*
1185 * Generate the command
1186 */
1187 va_start(va, ctl);
1188 vasprintf(&cmd, ctl, va);
1189 va_end(va);
1190 if (VerboseOpt)
1191 printf(" %s\n", cmd);
1192
1193 /*
1194 * Break us down into arguments. We do not just use system() here
1195 * because it blocks SIGINT and friends.
1196 */
1197 n = 0;
1198 nmax = 16;
1199 av = malloc(sizeof(char *) * nmax);
1200
1201 for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
c453712a 1202 if (n == nmax - 1) {
6a6e350f
MD
1203 nmax += 16;
1204 av = realloc(av, sizeof(char *) * nmax);
1205 }
1206 av[n++] = arg;
1207 }
c453712a 1208 av[n++] = NULL;
6a6e350f
MD
1209
1210 /*
1211 * Run the command.
1212 */
445faa69 1213 RunningIoctl = 1;
6a6e350f
MD
1214 if ((pid = fork()) == 0) {
1215 if (VerboseOpt < 2) {
1216 int fd = open("/dev/null", O_RDWR);
1217 dup2(fd, 1);
1218 close(fd);
1219 }
1220 execvp(av[0], av);
1221 _exit(127);
1222 } else if (pid < 0) {
1223 res = 127;
1224 } else {
1225 int status;
445faa69 1226
6a6e350f
MD
1227 while (waitpid(pid, &status, 0) != pid)
1228 ;
1229 res = WEXITSTATUS(status);
1230 }
445faa69
MD
1231 RunningIoctl = 0;
1232 if (DidInterrupt)
1233 _exit(1);
6a6e350f
MD
1234
1235 free(cmd);
1236 free(av);
1237 if (resp)
1238 *resp = res;
1239}