18fae3551SRodney W. Grimes /*- 28fae3551SRodney W. Grimes * Copyright (c) 1980, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 68fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 78fae3551SRodney W. Grimes * are met: 88fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 98fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 108fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 128fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 148fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 158fae3551SRodney W. Grimes * without specific prior written permission. 168fae3551SRodney W. Grimes * 178fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 188fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 218fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278fae3551SRodney W. Grimes * SUCH DAMAGE. 288fae3551SRodney W. Grimes */ 298fae3551SRodney W. Grimes 308fae3551SRodney W. Grimes #ifndef lint 317580ffbbSPhilippe Charnier #if 0 328fae3551SRodney W. Grimes static char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 337580ffbbSPhilippe Charnier #endif 347580ffbbSPhilippe Charnier static const char rcsid[] = 357f3dea24SPeter Wemm "$FreeBSD$"; 368fae3551SRodney W. Grimes #endif /* not lint */ 378fae3551SRodney W. Grimes 388fae3551SRodney W. Grimes #include <sys/param.h> 39941ee632SPoul-Henning Kamp #include <sys/queue.h> 408fae3551SRodney W. Grimes 418fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #include <protocols/dumprestore.h> 448fae3551SRodney W. Grimes 458fae3551SRodney W. Grimes #include <errno.h> 468fae3551SRodney W. Grimes #include <fcntl.h> 4789fdc4e1SMike Barcroft #include <limits.h> 488fae3551SRodney W. Grimes #include <stdio.h> 498fae3551SRodney W. Grimes #include <stdlib.h> 508fae3551SRodney W. Grimes #include <string.h> 517649cb00SKirk McKusick #include <time.h> 52617dbd3cSIan Dowse #include <timeconv.h> 538fae3551SRodney W. Grimes 548fae3551SRodney W. Grimes #include "dump.h" 558fae3551SRodney W. Grimes 56941ee632SPoul-Henning Kamp struct dumptime { 57941ee632SPoul-Henning Kamp struct dumpdates dt_value; 58941ee632SPoul-Henning Kamp SLIST_ENTRY(dumptime) dt_list; 59941ee632SPoul-Henning Kamp }; 60941ee632SPoul-Henning Kamp SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead); 614c8762f0SPedro F. Giffuni struct dumpdates **ddatev = NULL; 628fae3551SRodney W. Grimes int nddates = 0; 638fae3551SRodney W. Grimes 642db673abSWarner Losh static void dumprecout(FILE *, const struct dumpdates *); 652db673abSWarner Losh static int getrecord(FILE *, struct dumpdates *); 662db673abSWarner Losh static int makedumpdate(struct dumpdates *, const char *); 672db673abSWarner Losh static void readdumptimes(FILE *); 688fae3551SRodney W. Grimes 698fae3551SRodney W. Grimes void 702db673abSWarner Losh initdumptimes(void) 718fae3551SRodney W. Grimes { 728fae3551SRodney W. Grimes FILE *df; 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes if ((df = fopen(dumpdates, "r")) == NULL) { 758fae3551SRodney W. Grimes if (errno != ENOENT) { 76b2f6bdeeSDavid E. O'Brien msg("WARNING: cannot read %s: %s\n", dumpdates, 778fae3551SRodney W. Grimes strerror(errno)); 788cc6e4d8SDavid E. O'Brien return; 798fae3551SRodney W. Grimes } 808fae3551SRodney W. Grimes /* 818fae3551SRodney W. Grimes * Dumpdates does not exist, make an empty one. 828fae3551SRodney W. Grimes */ 838fae3551SRodney W. Grimes msg("WARNING: no file `%s', making an empty one\n", dumpdates); 848fae3551SRodney W. Grimes if ((df = fopen(dumpdates, "w")) == NULL) { 85b2f6bdeeSDavid E. O'Brien msg("WARNING: cannot create %s: %s\n", dumpdates, 868fae3551SRodney W. Grimes strerror(errno)); 878cc6e4d8SDavid E. O'Brien return; 888fae3551SRodney W. Grimes } 898fae3551SRodney W. Grimes (void) fclose(df); 908fae3551SRodney W. Grimes if ((df = fopen(dumpdates, "r")) == NULL) { 918fae3551SRodney W. Grimes quit("cannot read %s even after creating it: %s\n", 928fae3551SRodney W. Grimes dumpdates, strerror(errno)); 938fae3551SRodney W. Grimes /* NOTREACHED */ 948fae3551SRodney W. Grimes } 958fae3551SRodney W. Grimes } 968fae3551SRodney W. Grimes (void) flock(fileno(df), LOCK_SH); 978fae3551SRodney W. Grimes readdumptimes(df); 988fae3551SRodney W. Grimes (void) fclose(df); 998fae3551SRodney W. Grimes } 1008fae3551SRodney W. Grimes 1018fae3551SRodney W. Grimes static void 1022db673abSWarner Losh readdumptimes(FILE *df) 1038fae3551SRodney W. Grimes { 104d2334e27SIan Dowse int i; 105d2334e27SIan Dowse struct dumptime *dtwalk; 1068fae3551SRodney W. Grimes 1078fae3551SRodney W. Grimes for (;;) { 1088fae3551SRodney W. Grimes dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 10980ca1f34SXin LI if (getrecord(df, &(dtwalk->dt_value)) < 0) { 11080ca1f34SXin LI free(dtwalk); 1118fae3551SRodney W. Grimes break; 11280ca1f34SXin LI } 1138fae3551SRodney W. Grimes nddates++; 114941ee632SPoul-Henning Kamp SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 1158fae3551SRodney W. Grimes } 1168fae3551SRodney W. Grimes 1178fae3551SRodney W. Grimes /* 1188fae3551SRodney W. Grimes * arrayify the list, leaving enough room for the additional 1198fae3551SRodney W. Grimes * record that we may have to add to the ddate structure 1208fae3551SRodney W. Grimes */ 1214c8762f0SPedro F. Giffuni ddatev = calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 122941ee632SPoul-Henning Kamp dtwalk = SLIST_FIRST(&dthead); 123941ee632SPoul-Henning Kamp for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 1248fae3551SRodney W. Grimes ddatev[i] = &dtwalk->dt_value; 1258fae3551SRodney W. Grimes } 1268fae3551SRodney W. Grimes 1278fae3551SRodney W. Grimes void 1282db673abSWarner Losh getdumptime(void) 1298fae3551SRodney W. Grimes { 130d2334e27SIan Dowse struct dumpdates *ddp; 131d2334e27SIan Dowse int i; 1328fae3551SRodney W. Grimes char *fname; 1338fae3551SRodney W. Grimes 1348fae3551SRodney W. Grimes fname = disk; 1358fae3551SRodney W. Grimes #ifdef FDEBUG 136f72ab793SKirk McKusick msg("Looking for name %s in dumpdates = %s for level = %d\n", 1378fae3551SRodney W. Grimes fname, dumpdates, level); 1388fae3551SRodney W. Grimes #endif 1398fae3551SRodney W. Grimes spcl.c_ddate = 0; 140f72ab793SKirk McKusick lastlevel = 0; 1418fae3551SRodney W. Grimes 1428fae3551SRodney W. Grimes initdumptimes(); 1438fae3551SRodney W. Grimes /* 1448fae3551SRodney W. Grimes * Go find the entry with the same name for a lower increment 1458fae3551SRodney W. Grimes * and older date 1468fae3551SRodney W. Grimes */ 1478fae3551SRodney W. Grimes ITITERATE(i, ddp) { 1488fae3551SRodney W. Grimes if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 1498fae3551SRodney W. Grimes continue; 1508fae3551SRodney W. Grimes if (ddp->dd_level >= level) 1518fae3551SRodney W. Grimes continue; 1521c85e6a3SKirk McKusick if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate)) 1538fae3551SRodney W. Grimes continue; 1541c85e6a3SKirk McKusick spcl.c_ddate = _time_to_time64(ddp->dd_ddate); 1558fae3551SRodney W. Grimes lastlevel = ddp->dd_level; 1568fae3551SRodney W. Grimes } 1578fae3551SRodney W. Grimes } 1588fae3551SRodney W. Grimes 1598fae3551SRodney W. Grimes void 1602db673abSWarner Losh putdumptime(void) 1618fae3551SRodney W. Grimes { 1628fae3551SRodney W. Grimes FILE *df; 163d2334e27SIan Dowse struct dumpdates *dtwalk; 164d2334e27SIan Dowse int i; 1658fae3551SRodney W. Grimes int fd; 1668fae3551SRodney W. Grimes char *fname; 1675b3817c6SMatthew Dillon char *tmsg; 1688fae3551SRodney W. Grimes 1698fae3551SRodney W. Grimes if(uflag == 0) 1708fae3551SRodney W. Grimes return; 1718fae3551SRodney W. Grimes if ((df = fopen(dumpdates, "r+")) == NULL) 1728fae3551SRodney W. Grimes quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 1738fae3551SRodney W. Grimes fd = fileno(df); 1748fae3551SRodney W. Grimes (void) flock(fd, LOCK_EX); 1758fae3551SRodney W. Grimes fname = disk; 1764c8762f0SPedro F. Giffuni free(ddatev); 1774c8762f0SPedro F. Giffuni ddatev = NULL; 1788fae3551SRodney W. Grimes nddates = 0; 1798fae3551SRodney W. Grimes readdumptimes(df); 1808fae3551SRodney W. Grimes if (fseek(df, 0L, 0) < 0) 1818fae3551SRodney W. Grimes quit("fseek: %s\n", strerror(errno)); 1828fae3551SRodney W. Grimes spcl.c_ddate = 0; 1838fae3551SRodney W. Grimes ITITERATE(i, dtwalk) { 1848fae3551SRodney W. Grimes if (strncmp(fname, dtwalk->dd_name, 1858fae3551SRodney W. Grimes sizeof (dtwalk->dd_name)) != 0) 1868fae3551SRodney W. Grimes continue; 1878fae3551SRodney W. Grimes if (dtwalk->dd_level != level) 1888fae3551SRodney W. Grimes continue; 1898fae3551SRodney W. Grimes goto found; 1908fae3551SRodney W. Grimes } 1918fae3551SRodney W. Grimes /* 1928fae3551SRodney W. Grimes * construct the new upper bound; 1938fae3551SRodney W. Grimes * Enough room has been allocated. 1948fae3551SRodney W. Grimes */ 1958fae3551SRodney W. Grimes dtwalk = ddatev[nddates] = 1968fae3551SRodney W. Grimes (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 1978fae3551SRodney W. Grimes nddates += 1; 1988fae3551SRodney W. Grimes found: 1998fae3551SRodney W. Grimes (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 2008fae3551SRodney W. Grimes dtwalk->dd_level = level; 2011c85e6a3SKirk McKusick dtwalk->dd_ddate = _time64_to_time(spcl.c_date); 2028fae3551SRodney W. Grimes 2038fae3551SRodney W. Grimes ITITERATE(i, dtwalk) { 2048fae3551SRodney W. Grimes dumprecout(df, dtwalk); 2058fae3551SRodney W. Grimes } 2068fae3551SRodney W. Grimes if (fflush(df)) 2078fae3551SRodney W. Grimes quit("%s: %s\n", dumpdates, strerror(errno)); 2088fae3551SRodney W. Grimes if (ftruncate(fd, ftell(df))) 2098fae3551SRodney W. Grimes quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 2108fae3551SRodney W. Grimes (void) fclose(df); 2115b3817c6SMatthew Dillon if (spcl.c_date == 0) { 2125b3817c6SMatthew Dillon tmsg = "the epoch\n"; 2135b3817c6SMatthew Dillon } else { 2141c85e6a3SKirk McKusick time_t t = _time64_to_time(spcl.c_date); 2155b3817c6SMatthew Dillon tmsg = ctime(&t); 2165b3817c6SMatthew Dillon } 217f72ab793SKirk McKusick msg("level %d dump on %s", level, tmsg); 2188fae3551SRodney W. Grimes } 2198fae3551SRodney W. Grimes 2208fae3551SRodney W. Grimes static void 2212db673abSWarner Losh dumprecout(FILE *file, const struct dumpdates *what) 2228fae3551SRodney W. Grimes { 2238fae3551SRodney W. Grimes 22423ad9069SKirk McKusick if (strlen(what->dd_name) > DUMPFMTLEN) 22523ad9069SKirk McKusick quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n", 22623ad9069SKirk McKusick what->dd_name, DUMPFMTLEN); 22723ad9069SKirk McKusick if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name, 2282db673abSWarner Losh what->dd_level, ctime(&what->dd_ddate)) < 0) 2298fae3551SRodney W. Grimes quit("%s: %s\n", dumpdates, strerror(errno)); 2308fae3551SRodney W. Grimes } 2318fae3551SRodney W. Grimes 2328fae3551SRodney W. Grimes int recno; 2338fae3551SRodney W. Grimes 2348fae3551SRodney W. Grimes static int 2352db673abSWarner Losh getrecord(FILE *df, struct dumpdates *ddatep) 2368fae3551SRodney W. Grimes { 2378fae3551SRodney W. Grimes char tbuf[BUFSIZ]; 2388fae3551SRodney W. Grimes 2398fae3551SRodney W. Grimes recno = 0; 2408fae3551SRodney W. Grimes if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 2418fae3551SRodney W. Grimes return(-1); 2428fae3551SRodney W. Grimes recno++; 2438fae3551SRodney W. Grimes if (makedumpdate(ddatep, tbuf) < 0) 2448fae3551SRodney W. Grimes msg("Unknown intermediate format in %s, line %d\n", 2458fae3551SRodney W. Grimes dumpdates, recno); 2468fae3551SRodney W. Grimes 2478fae3551SRodney W. Grimes #ifdef FDEBUG 248f72ab793SKirk McKusick msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level, 2498fae3551SRodney W. Grimes ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 2508fae3551SRodney W. Grimes #endif 2518fae3551SRodney W. Grimes return(0); 2528fae3551SRodney W. Grimes } 2538fae3551SRodney W. Grimes 2548fae3551SRodney W. Grimes static int 2552db673abSWarner Losh makedumpdate(struct dumpdates *ddp, const char *tbuf) 2568fae3551SRodney W. Grimes { 2578fae3551SRodney W. Grimes char un_buf[128]; 2588fae3551SRodney W. Grimes 2598fae3551SRodney W. Grimes (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 2608fae3551SRodney W. Grimes ddp->dd_ddate = unctime(un_buf); 2618fae3551SRodney W. Grimes if (ddp->dd_ddate < 0) 2628fae3551SRodney W. Grimes return(-1); 2638fae3551SRodney W. Grimes return(0); 2648fae3551SRodney W. Grimes } 265