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. 138fae3551SRodney W. Grimes * 4. 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 #include <sys/time.h> 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 438fae3551SRodney W. Grimes 448fae3551SRodney W. Grimes #include <protocols/dumprestore.h> 458fae3551SRodney W. Grimes 468fae3551SRodney W. Grimes #include <errno.h> 478fae3551SRodney W. Grimes #include <fcntl.h> 4889fdc4e1SMike Barcroft #include <limits.h> 498fae3551SRodney W. Grimes #include <stdio.h> 508fae3551SRodney W. Grimes #include <stdlib.h> 518fae3551SRodney W. Grimes #include <string.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); 618fae3551SRodney W. Grimes struct dumpdates **ddatev = 0; 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)); 1098fae3551SRodney W. Grimes if (getrecord(df, &(dtwalk->dt_value)) < 0) 1108fae3551SRodney W. Grimes break; 1118fae3551SRodney W. Grimes nddates++; 112941ee632SPoul-Henning Kamp SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 1138fae3551SRodney W. Grimes } 1148fae3551SRodney W. Grimes 1158fae3551SRodney W. Grimes /* 1168fae3551SRodney W. Grimes * arrayify the list, leaving enough room for the additional 1178fae3551SRodney W. Grimes * record that we may have to add to the ddate structure 1188fae3551SRodney W. Grimes */ 1198fae3551SRodney W. Grimes ddatev = (struct dumpdates **) 1208fae3551SRodney W. Grimes calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 121941ee632SPoul-Henning Kamp dtwalk = SLIST_FIRST(&dthead); 122941ee632SPoul-Henning Kamp for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 1238fae3551SRodney W. Grimes ddatev[i] = &dtwalk->dt_value; 1248fae3551SRodney W. Grimes } 1258fae3551SRodney W. Grimes 1268fae3551SRodney W. Grimes void 1272db673abSWarner Losh getdumptime(void) 1288fae3551SRodney W. Grimes { 129d2334e27SIan Dowse struct dumpdates *ddp; 130d2334e27SIan Dowse int i; 1318fae3551SRodney W. Grimes char *fname; 1328fae3551SRodney W. Grimes 1338fae3551SRodney W. Grimes fname = disk; 1348fae3551SRodney W. Grimes #ifdef FDEBUG 135f72ab793SKirk McKusick msg("Looking for name %s in dumpdates = %s for level = %d\n", 1368fae3551SRodney W. Grimes fname, dumpdates, level); 1378fae3551SRodney W. Grimes #endif 1388fae3551SRodney W. Grimes spcl.c_ddate = 0; 139f72ab793SKirk McKusick lastlevel = 0; 1408fae3551SRodney W. Grimes 1418fae3551SRodney W. Grimes initdumptimes(); 1428fae3551SRodney W. Grimes /* 1438fae3551SRodney W. Grimes * Go find the entry with the same name for a lower increment 1448fae3551SRodney W. Grimes * and older date 1458fae3551SRodney W. Grimes */ 1468fae3551SRodney W. Grimes ITITERATE(i, ddp) { 1478fae3551SRodney W. Grimes if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 1488fae3551SRodney W. Grimes continue; 1498fae3551SRodney W. Grimes if (ddp->dd_level >= level) 1508fae3551SRodney W. Grimes continue; 1511c85e6a3SKirk McKusick if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate)) 1528fae3551SRodney W. Grimes continue; 1531c85e6a3SKirk McKusick spcl.c_ddate = _time_to_time64(ddp->dd_ddate); 1548fae3551SRodney W. Grimes lastlevel = ddp->dd_level; 1558fae3551SRodney W. Grimes } 1568fae3551SRodney W. Grimes } 1578fae3551SRodney W. Grimes 1588fae3551SRodney W. Grimes void 1592db673abSWarner Losh putdumptime(void) 1608fae3551SRodney W. Grimes { 1618fae3551SRodney W. Grimes FILE *df; 162d2334e27SIan Dowse struct dumpdates *dtwalk; 163d2334e27SIan Dowse int i; 1648fae3551SRodney W. Grimes int fd; 1658fae3551SRodney W. Grimes char *fname; 1665b3817c6SMatthew Dillon char *tmsg; 1678fae3551SRodney W. Grimes 1688fae3551SRodney W. Grimes if(uflag == 0) 1698fae3551SRodney W. Grimes return; 1708fae3551SRodney W. Grimes if ((df = fopen(dumpdates, "r+")) == NULL) 1718fae3551SRodney W. Grimes quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 1728fae3551SRodney W. Grimes fd = fileno(df); 1738fae3551SRodney W. Grimes (void) flock(fd, LOCK_EX); 1748fae3551SRodney W. Grimes fname = disk; 1758fae3551SRodney W. Grimes free((char *)ddatev); 1768fae3551SRodney W. Grimes ddatev = 0; 1778fae3551SRodney W. Grimes nddates = 0; 1788fae3551SRodney W. Grimes readdumptimes(df); 1798fae3551SRodney W. Grimes if (fseek(df, 0L, 0) < 0) 1808fae3551SRodney W. Grimes quit("fseek: %s\n", strerror(errno)); 1818fae3551SRodney W. Grimes spcl.c_ddate = 0; 1828fae3551SRodney W. Grimes ITITERATE(i, dtwalk) { 1838fae3551SRodney W. Grimes if (strncmp(fname, dtwalk->dd_name, 1848fae3551SRodney W. Grimes sizeof (dtwalk->dd_name)) != 0) 1858fae3551SRodney W. Grimes continue; 1868fae3551SRodney W. Grimes if (dtwalk->dd_level != level) 1878fae3551SRodney W. Grimes continue; 1888fae3551SRodney W. Grimes goto found; 1898fae3551SRodney W. Grimes } 1908fae3551SRodney W. Grimes /* 1918fae3551SRodney W. Grimes * construct the new upper bound; 1928fae3551SRodney W. Grimes * Enough room has been allocated. 1938fae3551SRodney W. Grimes */ 1948fae3551SRodney W. Grimes dtwalk = ddatev[nddates] = 1958fae3551SRodney W. Grimes (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 1968fae3551SRodney W. Grimes nddates += 1; 1978fae3551SRodney W. Grimes found: 1988fae3551SRodney W. Grimes (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 1998fae3551SRodney W. Grimes dtwalk->dd_level = level; 2001c85e6a3SKirk McKusick dtwalk->dd_ddate = _time64_to_time(spcl.c_date); 2018fae3551SRodney W. Grimes 2028fae3551SRodney W. Grimes ITITERATE(i, dtwalk) { 2038fae3551SRodney W. Grimes dumprecout(df, dtwalk); 2048fae3551SRodney W. Grimes } 2058fae3551SRodney W. Grimes if (fflush(df)) 2068fae3551SRodney W. Grimes quit("%s: %s\n", dumpdates, strerror(errno)); 2078fae3551SRodney W. Grimes if (ftruncate(fd, ftell(df))) 2088fae3551SRodney W. Grimes quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 2098fae3551SRodney W. Grimes (void) fclose(df); 2105b3817c6SMatthew Dillon if (spcl.c_date == 0) { 2115b3817c6SMatthew Dillon tmsg = "the epoch\n"; 2125b3817c6SMatthew Dillon } else { 2131c85e6a3SKirk McKusick time_t t = _time64_to_time(spcl.c_date); 2145b3817c6SMatthew Dillon tmsg = ctime(&t); 2155b3817c6SMatthew Dillon } 216f72ab793SKirk McKusick msg("level %d dump on %s", level, tmsg); 2178fae3551SRodney W. Grimes } 2188fae3551SRodney W. Grimes 2198fae3551SRodney W. Grimes static void 2202db673abSWarner Losh dumprecout(FILE *file, const struct dumpdates *what) 2218fae3551SRodney W. Grimes { 2228fae3551SRodney W. Grimes 2232db673abSWarner Losh if (fprintf(file, DUMPOUTFMT, what->dd_name, 2242db673abSWarner Losh what->dd_level, ctime(&what->dd_ddate)) < 0) 2258fae3551SRodney W. Grimes quit("%s: %s\n", dumpdates, strerror(errno)); 2268fae3551SRodney W. Grimes } 2278fae3551SRodney W. Grimes 2288fae3551SRodney W. Grimes int recno; 2298fae3551SRodney W. Grimes 2308fae3551SRodney W. Grimes static int 2312db673abSWarner Losh getrecord(FILE *df, struct dumpdates *ddatep) 2328fae3551SRodney W. Grimes { 2338fae3551SRodney W. Grimes char tbuf[BUFSIZ]; 2348fae3551SRodney W. Grimes 2358fae3551SRodney W. Grimes recno = 0; 2368fae3551SRodney W. Grimes if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 2378fae3551SRodney W. Grimes return(-1); 2388fae3551SRodney W. Grimes recno++; 2398fae3551SRodney W. Grimes if (makedumpdate(ddatep, tbuf) < 0) 2408fae3551SRodney W. Grimes msg("Unknown intermediate format in %s, line %d\n", 2418fae3551SRodney W. Grimes dumpdates, recno); 2428fae3551SRodney W. Grimes 2438fae3551SRodney W. Grimes #ifdef FDEBUG 244f72ab793SKirk McKusick msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level, 2458fae3551SRodney W. Grimes ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 2468fae3551SRodney W. Grimes #endif 2478fae3551SRodney W. Grimes return(0); 2488fae3551SRodney W. Grimes } 2498fae3551SRodney W. Grimes 2508fae3551SRodney W. Grimes static int 2512db673abSWarner Losh makedumpdate(struct dumpdates *ddp, const char *tbuf) 2528fae3551SRodney W. Grimes { 2538fae3551SRodney W. Grimes char un_buf[128]; 2548fae3551SRodney W. Grimes 2558fae3551SRodney W. Grimes (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 2568fae3551SRodney W. Grimes ddp->dd_ddate = unctime(un_buf); 2578fae3551SRodney W. Grimes if (ddp->dd_ddate < 0) 2588fae3551SRodney W. Grimes return(-1); 2598fae3551SRodney W. Grimes return(0); 2608fae3551SRodney W. Grimes } 261