Home | History | Annotate | Line # | Download | only in fsck_ffs
      1  1.6  dholland /*	$NetBSD: wapbl.c,v 1.6 2022/07/25 05:15:08 dholland Exp $	*/
      2  1.2    simonb 
      3  1.2    simonb /*-
      4  1.2    simonb  * Copyright (c) 2005,2008 The NetBSD Foundation, Inc.
      5  1.2    simonb  * All rights reserved.
      6  1.2    simonb  *
      7  1.2    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2    simonb  * by Wasabi Systems, Inc.
      9  1.2    simonb  *
     10  1.2    simonb  * Redistribution and use in source and binary forms, with or without
     11  1.2    simonb  * modification, are permitted provided that the following conditions
     12  1.2    simonb  * are met:
     13  1.2    simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.2    simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.2    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2    simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.2    simonb  *    documentation and/or other materials provided with the distribution.
     18  1.2    simonb  *
     19  1.2    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2    simonb  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2    simonb  */
     31  1.2    simonb 
     32  1.2    simonb /* This file contains fsck support for wapbl
     33  1.2    simonb  */
     34  1.3     joerg #define WAPBL_INTERNAL
     35  1.2    simonb 
     36  1.2    simonb #include <sys/cdefs.h>
     37  1.6  dholland __KERNEL_RCSID(0, "$NetBSD: wapbl.c,v 1.6 2022/07/25 05:15:08 dholland Exp $");
     38  1.2    simonb 
     39  1.2    simonb #include <sys/stat.h>
     40  1.2    simonb #include <sys/time.h>
     41  1.2    simonb #include <sys/uio.h>
     42  1.2    simonb 
     43  1.2    simonb #include <assert.h>
     44  1.2    simonb #include <errno.h>
     45  1.2    simonb #include <inttypes.h>
     46  1.2    simonb #include <stdio.h>
     47  1.2    simonb #include <stdbool.h>
     48  1.2    simonb #include <stdlib.h>
     49  1.2    simonb #include <string.h>
     50  1.2    simonb #include <unistd.h>
     51  1.2    simonb 
     52  1.2    simonb #include <ufs/ufs/dinode.h>
     53  1.2    simonb #include <ufs/ufs/dir.h>
     54  1.2    simonb #include <ufs/ufs/ufs_bswap.h>
     55  1.2    simonb #include <ufs/ufs/ufs_wapbl.h>
     56  1.2    simonb #include <ufs/ffs/fs.h>
     57  1.2    simonb #include <ufs/ffs/ffs_extern.h>
     58  1.2    simonb 
     59  1.2    simonb #include <sys/wapbl.h>
     60  1.2    simonb 
     61  1.2    simonb #include "fsck.h"
     62  1.2    simonb #include "fsutil.h"
     63  1.2    simonb #include "extern.h"
     64  1.2    simonb #include "exitvalues.h"
     65  1.2    simonb 
     66  1.2    simonb int
     67  1.2    simonb wapbl_write(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
     68  1.2    simonb {
     69  1.2    simonb 
     70  1.2    simonb 	WAPBL_PRINTF(WAPBL_PRINT_IO,
     71  1.2    simonb 		("wapbl_write: %zd bytes at block %"PRId64" on fd 0x%x\n",
     72  1.2    simonb 		len, pbn, fswritefd));
     73  1.2    simonb 	bwrite(fswritefd, data, pbn, len);
     74  1.2    simonb 	return 0;
     75  1.2    simonb }
     76  1.2    simonb 
     77  1.2    simonb int
     78  1.2    simonb wapbl_read(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
     79  1.2    simonb {
     80  1.2    simonb 
     81  1.2    simonb 	WAPBL_PRINTF(WAPBL_PRINT_IO,
     82  1.2    simonb 		("wapbl_read: %zd bytes at block %"PRId64" on fd 0x%x\n",
     83  1.2    simonb 		len, pbn, fsreadfd));
     84  1.2    simonb 	bread(fsreadfd, data, pbn, len);
     85  1.2    simonb 	return 0;
     86  1.2    simonb }
     87  1.2    simonb 
     88  1.2    simonb struct wapbl_replay *wapbl_replay;
     89  1.2    simonb 
     90  1.2    simonb void
     91  1.2    simonb replay_wapbl(void)
     92  1.2    simonb {
     93  1.2    simonb 	int error;
     94  1.2    simonb 
     95  1.6  dholland 	if (nflag) {
     96  1.6  dholland 		/*
     97  1.6  dholland 		 * XXX: we ought to have a mode where we can replay
     98  1.6  dholland 		 * the journal to memory, similar to what happens in
     99  1.6  dholland 		 * the kernel with a readonly mount. For now though
    100  1.6  dholland 		 * just print that we aren't doing it so as to avoid
    101  1.6  dholland 		 * lying to the user.
    102  1.6  dholland 		 */
    103  1.6  dholland 		pwarn("CANNOT REPLAY JOURNAL IN -n MODE; continuing anyway\n");
    104  1.6  dholland 	} else {
    105  1.2    simonb 		error = wapbl_replay_write(wapbl_replay, 0);
    106  1.2    simonb 		if (error) {
    107  1.2    simonb 			pfatal("UNABLE TO REPLAY JOURNAL BLOCKS");
    108  1.2    simonb 			if (reply("CONTINUE") == 0) {
    109  1.2    simonb 				exit(FSCK_EXIT_CHECK_FAILED);
    110  1.2    simonb 			}
    111  1.2    simonb 		} else {
    112  1.2    simonb 			wapbl_replay_stop(wapbl_replay);
    113  1.2    simonb 		}
    114  1.2    simonb 	}
    115  1.2    simonb 	{
    116  1.2    simonb 		int i;
    117  1.2    simonb 		for (i = 0; i < wapbl_replay->wr_inodescnt; i++) {
    118  1.2    simonb 			WAPBL_PRINTF(WAPBL_PRINT_REPLAY,("wapbl_replay: "
    119  1.2    simonb 			    "not cleaning inode %"PRIu32" mode %"PRIo32"\n",
    120  1.2    simonb 			    wapbl_replay->wr_inodes[i].wr_inumber,
    121  1.2    simonb 			    wapbl_replay->wr_inodes[i].wr_imode));
    122  1.2    simonb 		}
    123  1.2    simonb 	}
    124  1.2    simonb }
    125  1.2    simonb 
    126  1.2    simonb void
    127  1.2    simonb cleanup_wapbl(void)
    128  1.2    simonb {
    129  1.2    simonb 
    130  1.2    simonb 	if (wapbl_replay) {
    131  1.2    simonb 		if (wapbl_replay_isopen(wapbl_replay))
    132  1.2    simonb 			wapbl_replay_stop(wapbl_replay);
    133  1.2    simonb 		wapbl_replay_free(wapbl_replay);
    134  1.2    simonb 		wapbl_replay = 0;
    135  1.2    simonb 	}
    136  1.2    simonb }
    137  1.2    simonb 
    138  1.2    simonb int
    139  1.2    simonb read_wapbl(char *buf, long size, daddr_t blk)
    140  1.2    simonb {
    141  1.2    simonb 
    142  1.2    simonb 	if (!wapbl_replay || !wapbl_replay_isopen(wapbl_replay))
    143  1.2    simonb 		return 0;
    144  1.2    simonb 	return wapbl_replay_read(wapbl_replay, buf, blk, size);
    145  1.2    simonb }
    146  1.2    simonb 
    147  1.2    simonb int
    148  1.2    simonb is_journal_inode(ino_t ino)
    149  1.2    simonb {
    150  1.2    simonb 	union dinode *dp;
    151  1.2    simonb 
    152  1.2    simonb 	dp = ginode(ino);
    153  1.2    simonb 	if ((iswap32(DIP(dp, flags)) & SF_LOG) != 0 &&
    154  1.2    simonb 	    sblock->fs_journal_version == UFS_WAPBL_VERSION &&
    155  1.2    simonb 	    sblock->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM &&
    156  1.2    simonb 	    sblock->fs_journallocs[UFS_WAPBL_INFS_INO] == ino)
    157  1.2    simonb 		return 1;
    158  1.2    simonb 
    159  1.2    simonb 	return 0;
    160  1.2    simonb }
    161  1.4    bouyer 
    162  1.4    bouyer int
    163  1.4    bouyer check_wapbl(void)
    164  1.4    bouyer {
    165  1.4    bouyer 	uint64_t addr = 0, count = 0, blksize = 0;
    166  1.4    bouyer 	int error;
    167  1.4    bouyer 	int ret = 0;
    168  1.4    bouyer 	if (debug)
    169  1.4    bouyer 		wapbl_debug_print = WAPBL_PRINT_ERROR | WAPBL_PRINT_REPLAY;
    170  1.4    bouyer 	if (debug > 1)
    171  1.4    bouyer 		wapbl_debug_print |= WAPBL_PRINT_IO;
    172  1.4    bouyer 
    173  1.4    bouyer 	if (sblock->fs_flags & FS_DOWAPBL) {
    174  1.4    bouyer 		if (sblock->fs_journal_version != UFS_WAPBL_VERSION) {
    175  1.4    bouyer 			pfatal("INVALID JOURNAL VERSION %d",
    176  1.4    bouyer 			    sblock->fs_journal_version);
    177  1.4    bouyer 			if (reply("CONTINUE") == 0) {
    178  1.4    bouyer 				exit(FSCK_EXIT_CHECK_FAILED);
    179  1.4    bouyer 			}
    180  1.4    bouyer 			pwarn("CLEARING EXISTING JOURNAL\n");
    181  1.4    bouyer 			sblock->fs_flags &= ~FS_DOWAPBL;
    182  1.5   mlelstv 			sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
    183  1.4    bouyer 			sbdirty();
    184  1.4    bouyer 			ret = FSCK_EXIT_CHECK_FAILED;
    185  1.4    bouyer 		} else {
    186  1.4    bouyer 			switch(sblock->fs_journal_location) {
    187  1.4    bouyer 			case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    188  1.4    bouyer 				addr =
    189  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_EPART_ADDR];
    190  1.4    bouyer 				count =
    191  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_EPART_COUNT];
    192  1.4    bouyer 				blksize =
    193  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    194  1.4    bouyer 				break;
    195  1.4    bouyer 			case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    196  1.4    bouyer 				addr =
    197  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_INFS_ADDR];
    198  1.4    bouyer 				count =
    199  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_INFS_COUNT];
    200  1.4    bouyer 				blksize =
    201  1.4    bouyer 				  sblock->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    202  1.4    bouyer 				break;
    203  1.4    bouyer 			default:
    204  1.4    bouyer 				pfatal("INVALID JOURNAL LOCATION %d",
    205  1.4    bouyer 				    sblock->fs_journal_location);
    206  1.4    bouyer 				if (reply("CONTINUE") == 0) {
    207  1.4    bouyer 					exit(FSCK_EXIT_CHECK_FAILED);
    208  1.4    bouyer 				}
    209  1.4    bouyer 				pwarn("CLEARING EXISTING JOURNAL\n");
    210  1.4    bouyer 				sblock->fs_flags &= ~FS_DOWAPBL;
    211  1.5   mlelstv 				sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
    212  1.4    bouyer 				sblock->fs_journal_location =
    213  1.4    bouyer 				    UFS_WAPBL_JOURNALLOC_NONE;
    214  1.4    bouyer 				sbdirty();
    215  1.4    bouyer 				ret = FSCK_EXIT_CHECK_FAILED;
    216  1.4    bouyer 				break;
    217  1.4    bouyer 			}
    218  1.4    bouyer 			if (sblock->fs_flags & FS_DOWAPBL) {
    219  1.4    bouyer 				error = wapbl_replay_start(
    220  1.4    bouyer 				    &wapbl_replay, 0, addr, count, blksize);
    221  1.4    bouyer 				if (error) {
    222  1.4    bouyer 					pfatal(
    223  1.4    bouyer 					   "UNABLE TO READ JOURNAL FOR REPLAY");
    224  1.4    bouyer 					if (reply("CONTINUE") == 0) {
    225  1.4    bouyer 						exit(FSCK_EXIT_CHECK_FAILED);
    226  1.4    bouyer 					}
    227  1.4    bouyer 					pwarn("CLEARING EXISTING JOURNAL\n");
    228  1.4    bouyer 					sblock->fs_flags &= ~FS_DOWAPBL;
    229  1.5   mlelstv 					sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
    230  1.4    bouyer 					sbdirty();
    231  1.4    bouyer 					ret = FSCK_EXIT_CHECK_FAILED;
    232  1.4    bouyer 				}
    233  1.4    bouyer 			}
    234  1.4    bouyer 		}
    235  1.4    bouyer 	}
    236  1.4    bouyer 	/*
    237  1.4    bouyer 	 * at this time fs_journal_flags can only be 0,
    238  1.4    bouyer 	 * UFS_WAPBL_FLAGS_CREATE_LOG or UFS_WAPBL_FLAGS_CLEAR_LOG.
    239  1.4    bouyer 	 * We can't have both flags at the same time.
    240  1.4    bouyer 	 */
    241  1.4    bouyer 	switch (sblock->fs_journal_flags) {
    242  1.4    bouyer 	case 0:
    243  1.4    bouyer 		break;
    244  1.4    bouyer 	case UFS_WAPBL_FLAGS_CREATE_LOG:
    245  1.4    bouyer 	case UFS_WAPBL_FLAGS_CLEAR_LOG:
    246  1.4    bouyer 		switch(sblock->fs_journal_location) {
    247  1.4    bouyer 		case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    248  1.4    bouyer 		case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    249  1.4    bouyer 			break;
    250  1.4    bouyer 		case UFS_WAPBL_JOURNALLOC_NONE:
    251  1.4    bouyer 			if (sblock->fs_journal_flags ==
    252  1.4    bouyer 			    UFS_WAPBL_FLAGS_CLEAR_LOG)
    253  1.4    bouyer 				break;
    254  1.4    bouyer 			/* FALLTHROUGH */
    255  1.4    bouyer 		default:
    256  1.4    bouyer 			pfatal("INVALID JOURNAL LOCATION %d",
    257  1.4    bouyer 			    sblock->fs_journal_location);
    258  1.4    bouyer 			if (reply("CONTINUE") == 0) {
    259  1.4    bouyer 				exit(FSCK_EXIT_CHECK_FAILED);
    260  1.4    bouyer 			}
    261  1.4    bouyer 			pwarn("CLEARING JOURNAL FLAGS\n");
    262  1.4    bouyer 			sblock->fs_journal_flags = 0;
    263  1.4    bouyer 			sblock->fs_journal_location =
    264  1.4    bouyer 			    UFS_WAPBL_JOURNALLOC_NONE;
    265  1.4    bouyer 			sbdirty();
    266  1.4    bouyer 			ret = FSCK_EXIT_CHECK_FAILED;
    267  1.4    bouyer 			break;
    268  1.4    bouyer 		}
    269  1.4    bouyer 		break;
    270  1.4    bouyer 	default:
    271  1.4    bouyer 		pfatal("INVALID JOURNAL FLAGS %d",
    272  1.4    bouyer 		    sblock->fs_journal_flags);
    273  1.4    bouyer 		if (reply("CONTINUE") == 0) {
    274  1.4    bouyer 			exit(FSCK_EXIT_CHECK_FAILED);
    275  1.4    bouyer 		}
    276  1.4    bouyer 		pwarn("CLEARING JOURNAL FLAGS\n");
    277  1.4    bouyer 		sblock->fs_journal_flags = 0;
    278  1.4    bouyer 		sblock->fs_journal_location =
    279  1.4    bouyer 		    UFS_WAPBL_JOURNALLOC_NONE;
    280  1.4    bouyer 		sbdirty();
    281  1.4    bouyer 		ret = FSCK_EXIT_CHECK_FAILED;
    282  1.4    bouyer 		break;
    283  1.4    bouyer 	}
    284  1.4    bouyer 	return ret;
    285  1.4    bouyer }
    286