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