386BSD 0.1 development
[unix-history] / usr / src / sbin / fsck / dir.c
CommitLineData
8bce6cdf
WJ
1/*
2 * Copyright (c) 1980, 1986 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)dir.c 5.19 (Berkeley) 7/26/91";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <ufs/dinode.h>
40#include <ufs/fs.h>
41#define KERNEL
42#include <ufs/dir.h>
43#undef KERNEL
44#include <stdlib.h>
45#include <string.h>
46#include "fsck.h"
47
48char *lfname = "lost+found";
49int lfmode = 01777;
50struct dirtemplate emptydir = { 0, DIRBLKSIZ };
51struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
52
53struct direct *fsck_readdir();
54struct bufarea *getdirblk();
55
56/*
57 * Propagate connected state through the tree.
58 */
59propagate()
60{
61 register struct inoinfo **inpp, *inp;
62 struct inoinfo **inpend;
63 long change;
64
65 inpend = &inpsort[inplast];
66 do {
67 change = 0;
68 for (inpp = inpsort; inpp < inpend; inpp++) {
69 inp = *inpp;
70 if (inp->i_parent == 0)
71 continue;
72 if (statemap[inp->i_parent] == DFOUND &&
73 statemap[inp->i_number] == DSTATE) {
74 statemap[inp->i_number] = DFOUND;
75 change++;
76 }
77 }
78 } while (change > 0);
79}
80
81/*
82 * Scan each entry in a directory block.
83 */
84dirscan(idesc)
85 register struct inodesc *idesc;
86{
87 register struct direct *dp;
88 register struct bufarea *bp;
89 int dsize, n;
90 long blksiz;
91 char dbuf[DIRBLKSIZ];
92
93 if (idesc->id_type != DATA)
94 errexit("wrong type to dirscan %d\n", idesc->id_type);
95 if (idesc->id_entryno == 0 &&
96 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
97 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
98 blksiz = idesc->id_numfrags * sblock.fs_fsize;
99 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
100 idesc->id_filesize -= blksiz;
101 return (SKIP);
102 }
103 idesc->id_loc = 0;
104 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
105 dsize = dp->d_reclen;
106 bcopy((char *)dp, dbuf, (size_t)dsize);
107 idesc->id_dirp = (struct direct *)dbuf;
108 if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
109 bp = getdirblk(idesc->id_blkno, blksiz);
110 bcopy(dbuf, bp->b_un.b_buf + idesc->id_loc - dsize,
111 (size_t)dsize);
112 dirty(bp);
113 sbdirty();
114 }
115 if (n & STOP)
116 return (n);
117 }
118 return (idesc->id_filesize > 0 ? KEEPON : STOP);
119}
120
121/*
122 * get next entry in a directory.
123 */
124struct direct *
125fsck_readdir(idesc)
126 register struct inodesc *idesc;
127{
128 register struct direct *dp, *ndp;
129 register struct bufarea *bp;
130 long size, blksiz, fix;
131
132 blksiz = idesc->id_numfrags * sblock.fs_fsize;
133 bp = getdirblk(idesc->id_blkno, blksiz);
134 if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
135 idesc->id_loc < blksiz) {
136 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
137 if (dircheck(idesc, dp))
138 goto dpok;
139 idesc->id_loc += DIRBLKSIZ;
140 idesc->id_filesize -= DIRBLKSIZ;
141 fix = dofix(idesc, "DIRECTORY CORRUPTED");
142 bp = getdirblk(idesc->id_blkno, blksiz);
143 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
144 dp->d_reclen = DIRBLKSIZ;
145 dp->d_ino = 0;
146 dp->d_namlen = 0;
147 dp->d_name[0] = '\0';
148 if (fix)
149 dirty(bp);
150 return (dp);
151 }
152dpok:
153 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
154 return NULL;
155 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
156 idesc->id_loc += dp->d_reclen;
157 idesc->id_filesize -= dp->d_reclen;
158 if ((idesc->id_loc % DIRBLKSIZ) == 0)
159 return (dp);
160 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
161 if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
162 dircheck(idesc, ndp) == 0) {
163 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
164 idesc->id_loc += size;
165 idesc->id_filesize -= size;
166 fix = dofix(idesc, "DIRECTORY CORRUPTED");
167 bp = getdirblk(idesc->id_blkno, blksiz);
168 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
169 dp->d_reclen += size;
170 if (fix)
171 dirty(bp);
172 }
173 return (dp);
174}
175
176/*
177 * Verify that a directory entry is valid.
178 * This is a superset of the checks made in the kernel.
179 */
180dircheck(idesc, dp)
181 struct inodesc *idesc;
182 register struct direct *dp;
183{
184 register int size;
185 register char *cp;
186 int spaceleft;
187
188 size = DIRSIZ(dp);
189 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
190 if (dp->d_ino < maxino &&
191 dp->d_reclen != 0 &&
192 dp->d_reclen <= spaceleft &&
193 (dp->d_reclen & 0x3) == 0 &&
194 dp->d_reclen >= size &&
195 idesc->id_filesize >= size &&
196 dp->d_namlen <= MAXNAMLEN) {
197 if (dp->d_ino == 0)
198 return (1);
199 for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
200 if (*cp == 0 || (*cp++ == '/'))
201 return (0);
202 if (*cp == 0)
203 return (1);
204 }
205 return (0);
206}
207
208direrror(ino, errmesg)
209 ino_t ino;
210 char *errmesg;
211{
212
213 fileerror(ino, ino, errmesg);
214}
215
216fileerror(cwd, ino, errmesg)
217 ino_t cwd, ino;
218 char *errmesg;
219{
220 register struct dinode *dp;
221 char pathbuf[MAXPATHLEN + 1];
222
223 pwarn("%s ", errmesg);
224 pinode(ino);
225 printf("\n");
226 getpathname(pathbuf, cwd, ino);
227 if (ino < ROOTINO || ino > maxino) {
228 pfatal("NAME=%s\n", pathbuf);
229 return;
230 }
231 dp = ginode(ino);
232 if (ftypeok(dp))
233 pfatal("%s=%s\n",
234 (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
235 else
236 pfatal("NAME=%s\n", pathbuf);
237}
238
239adjust(idesc, lcnt)
240 register struct inodesc *idesc;
241 short lcnt;
242{
243 register struct dinode *dp;
244
245 dp = ginode(idesc->id_number);
246 if (dp->di_nlink == lcnt) {
247 if (linkup(idesc->id_number, (ino_t)0) == 0)
248 clri(idesc, "UNREF", 0);
249 } else {
250 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
251 ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
252 pinode(idesc->id_number);
253 printf(" COUNT %d SHOULD BE %d",
254 dp->di_nlink, dp->di_nlink - lcnt);
255 if (preen) {
256 if (lcnt < 0) {
257 printf("\n");
258 pfatal("LINK COUNT INCREASING");
259 }
260 printf(" (ADJUSTED)\n");
261 }
262 if (preen || reply("ADJUST") == 1) {
263 dp->di_nlink -= lcnt;
264 inodirty();
265 }
266 }
267}
268
269mkentry(idesc)
270 struct inodesc *idesc;
271{
272 register struct direct *dirp = idesc->id_dirp;
273 struct direct newent;
274 int newlen, oldlen;
275
276 newent.d_namlen = strlen(idesc->id_name);
277 newlen = DIRSIZ(&newent);
278 if (dirp->d_ino != 0)
279 oldlen = DIRSIZ(dirp);
280 else
281 oldlen = 0;
282 if (dirp->d_reclen - oldlen < newlen)
283 return (KEEPON);
284 newent.d_reclen = dirp->d_reclen - oldlen;
285 dirp->d_reclen = oldlen;
286 dirp = (struct direct *)(((char *)dirp) + oldlen);
287 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
288 dirp->d_reclen = newent.d_reclen;
289 dirp->d_namlen = newent.d_namlen;
290 bcopy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
291 return (ALTERED|STOP);
292}
293
294chgino(idesc)
295 struct inodesc *idesc;
296{
297 register struct direct *dirp = idesc->id_dirp;
298
299 if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
300 return (KEEPON);
301 dirp->d_ino = idesc->id_parent;
302 return (ALTERED|STOP);
303}
304
305linkup(orphan, parentdir)
306 ino_t orphan;
307 ino_t parentdir;
308{
309 register struct dinode *dp;
310 int lostdir;
311 ino_t oldlfdir;
312 struct inodesc idesc;
313 char tempname[BUFSIZ];
314 extern int pass4check();
315
316 bzero((char *)&idesc, sizeof(struct inodesc));
317 dp = ginode(orphan);
318 lostdir = (dp->di_mode & IFMT) == IFDIR;
319 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
320 pinode(orphan);
321 if (preen && dp->di_size == 0)
322 return (0);
323 if (preen)
324 printf(" (RECONNECTED)\n");
325 else
326 if (reply("RECONNECT") == 0)
327 return (0);
328 if (lfdir == 0) {
329 dp = ginode(ROOTINO);
330 idesc.id_name = lfname;
331 idesc.id_type = DATA;
332 idesc.id_func = findino;
333 idesc.id_number = ROOTINO;
334 if ((ckinode(dp, &idesc) & FOUND) != 0) {
335 lfdir = idesc.id_parent;
336 } else {
337 pwarn("NO lost+found DIRECTORY");
338 if (preen || reply("CREATE")) {
339 lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
340 if (lfdir != 0) {
341 if (makeentry(ROOTINO, lfdir, lfname) != 0) {
342 if (preen)
343 printf(" (CREATED)\n");
344 } else {
345 freedir(lfdir, ROOTINO);
346 lfdir = 0;
347 if (preen)
348 printf("\n");
349 }
350 }
351 }
352 }
353 if (lfdir == 0) {
354 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
355 printf("\n\n");
356 return (0);
357 }
358 }
359 dp = ginode(lfdir);
360 if ((dp->di_mode & IFMT) != IFDIR) {
361 pfatal("lost+found IS NOT A DIRECTORY");
362 if (reply("REALLOCATE") == 0)
363 return (0);
364 oldlfdir = lfdir;
365 if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
366 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
367 return (0);
368 }
369 if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
370 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
371 return (0);
372 }
373 inodirty();
374 idesc.id_type = ADDR;
375 idesc.id_func = pass4check;
376 idesc.id_number = oldlfdir;
377 adjust(&idesc, lncntp[oldlfdir] + 1);
378 lncntp[oldlfdir] = 0;
379 dp = ginode(lfdir);
380 }
381 if (statemap[lfdir] != DFOUND) {
382 pfatal("SORRY. NO lost+found DIRECTORY\n\n");
383 return (0);
384 }
385 (void)lftempname(tempname, orphan);
386 if (makeentry(lfdir, orphan, tempname) == 0) {
387 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
388 printf("\n\n");
389 return (0);
390 }
391 lncntp[orphan]--;
392 if (lostdir) {
393 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
394 parentdir != (ino_t)-1)
395 (void)makeentry(orphan, lfdir, "..");
396 dp = ginode(lfdir);
397 dp->di_nlink++;
398 inodirty();
399 lncntp[lfdir]++;
400 pwarn("DIR I=%lu CONNECTED. ", orphan);
401 if (parentdir != (ino_t)-1)
402 printf("PARENT WAS I=%lu\n", parentdir);
403 if (preen == 0)
404 printf("\n");
405 }
406 return (1);
407}
408
409/*
410 * fix an entry in a directory.
411 */
412changeino(dir, name, newnum)
413 ino_t dir;
414 char *name;
415 ino_t newnum;
416{
417 struct inodesc idesc;
418
419 bzero((char *)&idesc, sizeof(struct inodesc));
420 idesc.id_type = DATA;
421 idesc.id_func = chgino;
422 idesc.id_number = dir;
423 idesc.id_fix = DONTKNOW;
424 idesc.id_name = name;
425 idesc.id_parent = newnum; /* new value for name */
426 return (ckinode(ginode(dir), &idesc));
427}
428
429/*
430 * make an entry in a directory
431 */
432makeentry(parent, ino, name)
433 ino_t parent, ino;
434 char *name;
435{
436 struct dinode *dp;
437 struct inodesc idesc;
438 char pathbuf[MAXPATHLEN + 1];
439
440 if (parent < ROOTINO || parent >= maxino ||
441 ino < ROOTINO || ino >= maxino)
442 return (0);
443 bzero((char *)&idesc, sizeof(struct inodesc));
444 idesc.id_type = DATA;
445 idesc.id_func = mkentry;
446 idesc.id_number = parent;
447 idesc.id_parent = ino; /* this is the inode to enter */
448 idesc.id_fix = DONTKNOW;
449 idesc.id_name = name;
450 dp = ginode(parent);
451 if (dp->di_size % DIRBLKSIZ) {
452 dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
453 inodirty();
454 }
455 if ((ckinode(dp, &idesc) & ALTERED) != 0)
456 return (1);
457 getpathname(pathbuf, parent, parent);
458 dp = ginode(parent);
459 if (expanddir(dp, pathbuf) == 0)
460 return (0);
461 return (ckinode(dp, &idesc) & ALTERED);
462}
463
464/*
465 * Attempt to expand the size of a directory
466 */
467expanddir(dp, name)
468 register struct dinode *dp;
469 char *name;
470{
471 daddr_t lastbn, newblk;
472 register struct bufarea *bp;
473 char *cp, firstblk[DIRBLKSIZ];
474
475 lastbn = lblkno(&sblock, dp->di_size);
476 if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
477 return (0);
478 if ((newblk = allocblk(sblock.fs_frag)) == 0)
479 return (0);
480 dp->di_db[lastbn + 1] = dp->di_db[lastbn];
481 dp->di_db[lastbn] = newblk;
482 dp->di_size += sblock.fs_bsize;
483 dp->di_blocks += btodb(sblock.fs_bsize);
484 bp = getdirblk(dp->di_db[lastbn + 1],
485 (long)dblksize(&sblock, dp, lastbn + 1));
486 if (bp->b_errs)
487 goto bad;
488 bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
489 bp = getdirblk(newblk, sblock.fs_bsize);
490 if (bp->b_errs)
491 goto bad;
492 bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
493 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
494 cp < &bp->b_un.b_buf[sblock.fs_bsize];
495 cp += DIRBLKSIZ)
496 bcopy((char *)&emptydir, cp, sizeof emptydir);
497 dirty(bp);
498 bp = getdirblk(dp->di_db[lastbn + 1],
499 (long)dblksize(&sblock, dp, lastbn + 1));
500 if (bp->b_errs)
501 goto bad;
502 bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir);
503 pwarn("NO SPACE LEFT IN %s", name);
504 if (preen)
505 printf(" (EXPANDED)\n");
506 else if (reply("EXPAND") == 0)
507 goto bad;
508 dirty(bp);
509 inodirty();
510 return (1);
511bad:
512 dp->di_db[lastbn] = dp->di_db[lastbn + 1];
513 dp->di_db[lastbn + 1] = 0;
514 dp->di_size -= sblock.fs_bsize;
515 dp->di_blocks -= btodb(sblock.fs_bsize);
516 freeblk(newblk, sblock.fs_frag);
517 return (0);
518}
519
520/*
521 * allocate a new directory
522 */
523allocdir(parent, request, mode)
524 ino_t parent, request;
525 int mode;
526{
527 ino_t ino;
528 char *cp;
529 struct dinode *dp;
530 register struct bufarea *bp;
531
532 ino = allocino(request, IFDIR|mode);
533 dirhead.dot_ino = ino;
534 dirhead.dotdot_ino = parent;
535 dp = ginode(ino);
536 bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
537 if (bp->b_errs) {
538 freeino(ino);
539 return (0);
540 }
541 bcopy((char *)&dirhead, bp->b_un.b_buf, sizeof dirhead);
542 for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
543 cp < &bp->b_un.b_buf[sblock.fs_fsize];
544 cp += DIRBLKSIZ)
545 bcopy((char *)&emptydir, cp, sizeof emptydir);
546 dirty(bp);
547 dp->di_nlink = 2;
548 inodirty();
549 if (ino == ROOTINO) {
550 lncntp[ino] = dp->di_nlink;
551 cacheino(dp, ino);
552 return(ino);
553 }
554 if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
555 freeino(ino);
556 return (0);
557 }
558 cacheino(dp, ino);
559 statemap[ino] = statemap[parent];
560 if (statemap[ino] == DSTATE) {
561 lncntp[ino] = dp->di_nlink;
562 lncntp[parent]++;
563 }
564 dp = ginode(parent);
565 dp->di_nlink++;
566 inodirty();
567 return (ino);
568}
569
570/*
571 * free a directory inode
572 */
573freedir(ino, parent)
574 ino_t ino, parent;
575{
576 struct dinode *dp;
577
578 if (ino != parent) {
579 dp = ginode(parent);
580 dp->di_nlink--;
581 inodirty();
582 }
583 freeino(ino);
584}
585
586/*
587 * generate a temporary name for the lost+found directory.
588 */
589lftempname(bufp, ino)
590 char *bufp;
591 ino_t ino;
592{
593 register ino_t in;
594 register char *cp;
595 int namlen;
596
597 cp = bufp + 2;
598 for (in = maxino; in > 0; in /= 10)
599 cp++;
600 *--cp = 0;
601 namlen = cp - bufp;
602 in = ino;
603 while (cp > bufp) {
604 *--cp = (in % 10) + '0';
605 in /= 10;
606 }
607 *cp = '#';
608 return (namlen);
609}
610
611/*
612 * Get a directory block.
613 * Insure that it is held until another is requested.
614 */
615struct bufarea *
616getdirblk(blkno, size)
617 daddr_t blkno;
618 long size;
619{
620
621 if (pdirbp != 0)
622 pdirbp->b_flags &= ~B_INUSE;
623 pdirbp = getdatablk(blkno, size);
624 return (pdirbp);
625}