utilities.c revision 1.12 1 /* $NetBSD: utilities.c,v 1.12 2003/03/31 19:57:00 perseant 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/mount.h>
39
40 #include <ufs/ufs/inode.h>
41 #include <ufs/ufs/dir.h>
42 #define buf ubuf
43 #define vnode uvnode
44 #include <ufs/lfs/lfs.h>
45
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <unistd.h>
52
53 #include <signal.h>
54
55 #include "bufcache.h"
56 #include "vnode.h"
57 #include "lfs.h"
58 #include "segwrite.h"
59
60 #include "fsutil.h"
61 #include "fsck.h"
62 #include "extern.h"
63
64 long diskreads, totalreads; /* Disk cache statistics */
65
66 extern int returntosingle;
67 extern off_t locked_queue_bytes;
68
69 int
70 ftypeok(struct dinode * dp)
71 {
72 switch (dp->di_mode & IFMT) {
73
74 case IFDIR:
75 case IFREG:
76 case IFBLK:
77 case IFCHR:
78 case IFLNK:
79 case IFSOCK:
80 case IFIFO:
81 return (1);
82
83 default:
84 if (debug)
85 pwarn("bad file type 0%o\n", dp->di_mode);
86 return (0);
87 }
88 }
89
90 int
91 reply(char *question)
92 {
93 int persevere;
94 char c;
95
96 if (preen)
97 err(1, "INTERNAL ERROR: GOT TO reply()");
98 persevere = !strcmp(question, "CONTINUE");
99 printf("\n");
100 if (!persevere && nflag) {
101 printf("%s? no\n\n", question);
102 return (0);
103 }
104 if (yflag || (persevere && nflag)) {
105 printf("%s? yes\n\n", question);
106 return (1);
107 }
108 do {
109 printf("%s? [yn] ", question);
110 (void) fflush(stdout);
111 c = getc(stdin);
112 while (c != '\n' && getc(stdin) != '\n')
113 if (feof(stdin))
114 return (0);
115 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
116 printf("\n");
117 if (c == 'y' || c == 'Y')
118 return (1);
119 return (0);
120 }
121
122 static void
123 write_superblocks(void)
124 {
125 lfs_writesuper(fs, fs->lfs_sboffs[0]);
126 lfs_writesuper(fs, fs->lfs_sboffs[1]);
127 fsmodified = 1;
128 }
129
130 void
131 ckfini(int markclean)
132 {
133 if (locked_queue_bytes > 0) {
134 if (preen || reply("WRITE CHANGES TO DISK") == 1) {
135 if (preen == 0)
136 pwarn("WRITING CHANGES TO DISK\n");
137 lfs_segwrite(fs, SEGM_CKP);
138 fsdirty = 0;
139 fsmodified = 1;
140 }
141 }
142
143 if ((fs->lfs_flags & LFS_PF_CLEAN) == 0)
144 fsmodified = 1;
145 fs->lfs_pflags |= LFS_PF_CLEAN;
146
147 if (fsmodified && (preen || reply("UPDATE STANDARD SUPERBLOCK"))) {
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 fs->lfs_pflags |= 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, ino_t curdir, ino_t ino)
190 {
191 int len;
192 register char *cp;
193 struct inodesc idesc;
194 static int busy = 0;
195
196 if (curdir == ino && ino == ROOTINO) {
197 (void) strcpy(namebuf, "/");
198 return;
199 }
200 if (busy ||
201 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
202 (void) strcpy(namebuf, "?");
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 != 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 ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0)
227 break;
228 len = strlen(namebuf);
229 cp -= len;
230 memcpy(cp, namebuf, (size_t) len);
231 *--cp = '/';
232 if (cp < &namebuf[MAXNAMLEN])
233 break;
234 ino = idesc.id_number;
235 }
236 busy = 0;
237 if (ino != ROOTINO)
238 *--cp = '?';
239 memcpy(namebuf, cp, (size_t) (&namebuf[MAXPATHLEN] - cp));
240 }
241
242 void
243 catch(int n)
244 {
245 ckfini(0);
246 exit(12);
247 }
248 /*
249 * When preening, allow a single quit to signal
250 * a special exit after filesystem checks complete
251 * so that reboot sequence may be interrupted.
252 */
253 void
254 catchquit(int n)
255 {
256 printf("returning to single-user after filesystem check\n");
257 returntosingle = 1;
258 (void) signal(SIGQUIT, SIG_DFL);
259 }
260 /*
261 * Ignore a single quit signal; wait and flush just in case.
262 * Used by child processes in preen.
263 */
264 void
265 voidquit(int n)
266 {
267
268 sleep(1);
269 (void) signal(SIGQUIT, SIG_IGN);
270 (void) signal(SIGQUIT, SIG_DFL);
271 }
272 /*
273 * determine whether an inode should be fixed.
274 */
275 int
276 dofix(struct inodesc * idesc, char *msg)
277 {
278
279 switch (idesc->id_fix) {
280
281 case DONTKNOW:
282 if (idesc->id_type == DATA)
283 direrror(idesc->id_number, msg);
284 else
285 pwarn("%s", msg);
286 if (preen) {
287 printf(" (SALVAGED)\n");
288 idesc->id_fix = FIX;
289 return (ALTERED);
290 }
291 if (reply("SALVAGE") == 0) {
292 idesc->id_fix = NOFIX;
293 return (0);
294 }
295 idesc->id_fix = FIX;
296 return (ALTERED);
297
298 case FIX:
299 return (ALTERED);
300
301 case NOFIX:
302 case IGNORE:
303 return (0);
304
305 default:
306 err(8, "UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
307 }
308 /* NOTREACHED */
309 }
310