utilities.c revision 1.19 1 /* $NetBSD: utilities.c,v 1.19 2005/06/27 02:48:28 christos 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 #include <ufs/ufs/inode.h>
37 #include <ufs/ufs/dir.h>
38 #define buf ubuf
39 #define vnode uvnode
40 #include <ufs/lfs/lfs.h>
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <unistd.h>
48
49 #include <signal.h>
50
51 #include "bufcache.h"
52 #include "vnode.h"
53 #include "lfs.h"
54 #include "segwrite.h"
55
56 #include "fsutil.h"
57 #include "fsck.h"
58 #include "extern.h"
59
60 long diskreads, totalreads; /* Disk cache statistics */
61
62 extern int returntosingle;
63 extern off_t locked_queue_bytes;
64
65 int
66 ftypeok(struct ufs1_dinode * dp)
67 {
68 switch (dp->di_mode & IFMT) {
69
70 case IFDIR:
71 case IFREG:
72 case IFBLK:
73 case IFCHR:
74 case IFLNK:
75 case IFSOCK:
76 case IFIFO:
77 return (1);
78
79 default:
80 if (debug)
81 pwarn("bad file type 0%o\n", dp->di_mode);
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(1, "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 lfs_writesuper(fs, fs->lfs_sboffs[0]);
122 lfs_writesuper(fs, fs->lfs_sboffs[1]);
123 fsmodified = 1;
124 }
125
126 void
127 ckfini(int markclean)
128 {
129 if (locked_queue_bytes > 0) {
130 if (preen || reply("WRITE CHANGES TO DISK") == 1) {
131 if (preen == 0)
132 pwarn("WRITING CHANGES TO DISK\n");
133 lfs_segwrite(fs, SEGM_CKP);
134 fsdirty = 0;
135 fsmodified = 1;
136 }
137 }
138
139 if ((fs->lfs_pflags & LFS_PF_CLEAN) == 0) {
140 fsmodified = 1;
141 }
142 fs->lfs_pflags |= LFS_PF_CLEAN;
143
144 if (fsmodified && (preen || reply("UPDATE STANDARD SUPERBLOCK"))) {
145 sbdirty();
146 write_superblocks();
147 }
148 if (markclean && fsmodified) {
149 /*
150 * Mark the file system as clean, and sync the superblock.
151 */
152 if (preen)
153 pwarn("MARKING FILE SYSTEM CLEAN\n");
154 else if (!reply("MARK FILE SYSTEM CLEAN"))
155 markclean = 0;
156 if (markclean) {
157 fs->lfs_pflags |= LFS_PF_CLEAN;
158 sbdirty();
159 write_superblocks();
160 if (!preen)
161 printf(
162 "\n***** FILE SYSTEM MARKED CLEAN *****\n");
163 }
164 }
165
166 if (debug)
167 bufstats();
168 (void) close(fsreadfd);
169 }
170 /*
171 * Free a previously allocated block
172 */
173 void
174 freeblk(daddr_t blkno, long frags)
175 {
176 struct inodesc idesc;
177
178 idesc.id_blkno = blkno;
179 idesc.id_numfrags = frags;
180 (void) pass4check(&idesc);
181 }
182 /*
183 * Find a pathname
184 */
185 void
186 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
187 {
188 int len;
189 char *cp;
190 struct inodesc idesc;
191 static int busy = 0;
192
193 if (curdir == ino && ino == ROOTINO) {
194 (void) strlcpy(namebuf, "/", namebuflen);
195 return;
196 }
197 if (busy ||
198 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
199 (void) strlcpy(namebuf, "?", namebuflen);
200 return;
201 }
202 busy = 1;
203 memset(&idesc, 0, sizeof(struct inodesc));
204 idesc.id_type = DATA;
205 idesc.id_fix = IGNORE;
206 cp = &namebuf[MAXPATHLEN - 1];
207 *cp = '\0';
208 if (curdir != ino) {
209 idesc.id_parent = curdir;
210 goto namelookup;
211 }
212 while (ino != ROOTINO) {
213 idesc.id_number = ino;
214 idesc.id_func = findino;
215 idesc.id_name = "..";
216 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
217 break;
218 namelookup:
219 idesc.id_number = idesc.id_parent;
220 idesc.id_parent = ino;
221 idesc.id_func = findname;
222 idesc.id_name = namebuf;
223 if ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0)
224 break;
225 len = strlen(namebuf);
226 cp -= len;
227 memcpy(cp, namebuf, (size_t) len);
228 *--cp = '/';
229 if (cp < &namebuf[MAXNAMLEN])
230 break;
231 ino = idesc.id_number;
232 }
233 busy = 0;
234 if (ino != ROOTINO)
235 *--cp = '?';
236 memcpy(namebuf, cp, (size_t) (&namebuf[MAXPATHLEN] - cp));
237 }
238
239 void
240 catch(int n)
241 {
242 ckfini(0);
243 exit(12);
244 }
245 /*
246 * When preening, allow a single quit to signal
247 * a special exit after filesystem checks complete
248 * so that reboot sequence may be interrupted.
249 */
250 void
251 catchquit(int n)
252 {
253 printf("returning to single-user after filesystem check\n");
254 returntosingle = 1;
255 (void) signal(SIGQUIT, SIG_DFL);
256 }
257 /*
258 * Ignore a single quit signal; wait and flush just in case.
259 * Used by child processes in preen.
260 */
261 void
262 voidquit(int n)
263 {
264
265 sleep(1);
266 (void) signal(SIGQUIT, SIG_IGN);
267 (void) signal(SIGQUIT, SIG_DFL);
268 }
269 /*
270 * determine whether an inode should be fixed.
271 */
272 int
273 dofix(struct inodesc * idesc, const char *msg)
274 {
275
276 switch (idesc->id_fix) {
277
278 case DONTKNOW:
279 if (idesc->id_type == DATA)
280 direrror(idesc->id_number, msg);
281 else
282 pwarn("%s", msg);
283 if (preen) {
284 printf(" (SALVAGED)\n");
285 idesc->id_fix = FIX;
286 return (ALTERED);
287 }
288 if (reply("SALVAGE") == 0) {
289 idesc->id_fix = NOFIX;
290 return (0);
291 }
292 idesc->id_fix = FIX;
293 return (ALTERED);
294
295 case FIX:
296 return (ALTERED);
297
298 case NOFIX:
299 case IGNORE:
300 return (0);
301
302 default:
303 err(8, "UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
304 }
305 /* NOTREACHED */
306 }
307