1 /* $NetBSD: utilities.c,v 1.41 2015/08/12 18:28:00 dholland Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/mount.h> 35 36 #define buf ubuf 37 #define vnode uvnode 38 #include <ufs/lfs/lfs.h> 39 #include <ufs/lfs/lfs_accessors.h> 40 41 #include <err.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <ctype.h> 46 #include <unistd.h> 47 #include <errno.h> 48 49 #include <signal.h> 50 51 #include "bufcache.h" 52 #include "vnode.h" 53 #include "lfs_user.h" 54 #include "segwrite.h" 55 56 #include "fsutil.h" 57 #include "fsck.h" 58 #include "extern.h" 59 #include "exitvalues.h" 60 61 long diskreads, totalreads; /* Disk cache statistics */ 62 63 extern off_t locked_queue_bytes; 64 65 int 66 ftypeok(union lfs_dinode * dp) 67 { 68 switch (lfs_dino_getmode(fs, dp) & LFS_IFMT) { 69 70 case LFS_IFDIR: 71 case LFS_IFREG: 72 case LFS_IFBLK: 73 case LFS_IFCHR: 74 case LFS_IFLNK: 75 case LFS_IFSOCK: 76 case LFS_IFIFO: 77 return (1); 78 79 default: 80 if (debug) 81 pwarn("bad file type 0%o\n", lfs_dino_getmode(fs, dp)); 82 return (0); 83 } 84 } 85 86 int 87 reply(const char *question) 88 { 89 int persevere; 90 char c; 91 92 if (preen) 93 err(EXIT_FAILURE, "INTERNAL ERROR: GOT TO reply()"); 94 persevere = !strcmp(question, "CONTINUE"); 95 pwarn("\n"); 96 if (!persevere && nflag) { 97 printf("%s? no\n\n", question); 98 return (0); 99 } 100 if (yflag || (persevere && nflag)) { 101 printf("%s? yes\n\n", question); 102 return (1); 103 } 104 do { 105 printf("%s? [yn] ", question); 106 (void) fflush(stdout); 107 c = getc(stdin); 108 while (c != '\n' && getc(stdin) != '\n') 109 if (feof(stdin)) 110 return (0); 111 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 112 printf("\n"); 113 if (c == 'y' || c == 'Y') 114 return (1); 115 return (0); 116 } 117 118 static void 119 write_superblocks(void) 120 { 121 if (debug) 122 pwarn("writing superblocks with lfs_idaddr = 0x%jx\n", 123 (uintmax_t)lfs_sb_getidaddr(fs)); 124 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 125 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 126 fsmodified = 1; 127 } 128 129 void 130 ckfini(int markclean) 131 { 132 if (locked_queue_bytes > 0) { 133 if (preen || reply("WRITE CHANGES TO DISK") == 1) { 134 if (preen == 0) 135 pwarn("WRITING CHANGES TO DISK\n"); 136 lfs_segwrite(fs, SEGM_CKP); 137 fsdirty = 0; 138 fsmodified = 1; 139 } 140 } 141 142 if (!nflag && (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) == 0) { 143 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN); 144 fsmodified = 1; 145 } 146 147 if (fsmodified && (preen || reply("UPDATE SUPERBLOCKS"))) { 148 sbdirty(); 149 write_superblocks(); 150 } 151 if (markclean && fsmodified) { 152 /* 153 * Mark the file system as clean, and sync the superblock. 154 */ 155 if (preen) 156 pwarn("MARKING FILE SYSTEM CLEAN\n"); 157 else if (!reply("MARK FILE SYSTEM CLEAN")) 158 markclean = 0; 159 if (markclean) { 160 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN); 161 sbdirty(); 162 write_superblocks(); 163 if (!preen) 164 printf( 165 "\n***** FILE SYSTEM MARKED CLEAN *****\n"); 166 } 167 } 168 169 if (debug) 170 bufstats(); 171 (void) close(fsreadfd); 172 } 173 /* 174 * Free a previously allocated block 175 */ 176 void 177 freeblk(daddr_t blkno, long frags) 178 { 179 struct inodesc idesc; 180 181 idesc.id_blkno = blkno; 182 idesc.id_numfrags = frags; 183 (void) pass4check(&idesc); 184 } 185 /* 186 * Find a pathname 187 */ 188 void 189 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino) 190 { 191 int len; 192 char *cp; 193 struct inodesc idesc; 194 static int busy = 0; 195 196 if (curdir == ino && ino == ULFS_ROOTINO) { 197 (void) strlcpy(namebuf, "/", namebuflen); 198 return; 199 } 200 if (busy || 201 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) { 202 (void) strlcpy(namebuf, "?", namebuflen); 203 return; 204 } 205 busy = 1; 206 memset(&idesc, 0, sizeof(struct inodesc)); 207 idesc.id_type = DATA; 208 idesc.id_fix = IGNORE; 209 cp = &namebuf[MAXPATHLEN - 1]; 210 *cp = '\0'; 211 if (curdir != ino) { 212 idesc.id_parent = curdir; 213 goto namelookup; 214 } 215 while (ino != ULFS_ROOTINO) { 216 idesc.id_number = ino; 217 idesc.id_func = findino; 218 idesc.id_name = ".."; 219 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0) 220 break; 221 namelookup: 222 idesc.id_number = idesc.id_parent; 223 idesc.id_parent = ino; 224 idesc.id_func = findname; 225 idesc.id_name = namebuf; 226 if (ginode(idesc.id_number) == NULL) 227 break; 228 if ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0) 229 break; 230 len = strlen(namebuf); 231 cp -= len; 232 memcpy(cp, namebuf, (size_t) len); 233 *--cp = '/'; 234 if (cp < &namebuf[LFS_MAXNAMLEN]) 235 break; 236 ino = idesc.id_number; 237 } 238 busy = 0; 239 if (ino != ULFS_ROOTINO) 240 *--cp = '?'; 241 memcpy(namebuf, cp, (size_t) (&namebuf[MAXPATHLEN] - cp)); 242 } 243 244 /* 245 * determine whether an inode should be fixed. 246 */ 247 int 248 dofix(struct inodesc * idesc, const char *msg) 249 { 250 251 switch (idesc->id_fix) { 252 253 case DONTKNOW: 254 if (idesc->id_type == DATA) 255 direrror(idesc->id_number, msg); 256 else 257 pwarn("%s", msg); 258 if (preen) { 259 printf(" (SALVAGED)\n"); 260 idesc->id_fix = FIX; 261 return (ALTERED); 262 } 263 if (reply("SALVAGE") == 0) { 264 idesc->id_fix = NOFIX; 265 return (0); 266 } 267 idesc->id_fix = FIX; 268 return (ALTERED); 269 270 case FIX: 271 return (ALTERED); 272 273 case NOFIX: 274 case IGNORE: 275 return (0); 276 277 default: 278 err(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix); 279 } 280 /* NOTREACHED */ 281 } 282