fd7d51962d72aec5322078fe53cc21881c545381
[openafs.git] / src / vfsck / pass4.c
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 #include <roken.h>
22
23 #include <ctype.h>
24
25 #define VICE
26 #ifdef  AFS_OSF_ENV
27 #include <sys/vnode.h>
28 #include <sys/mount.h>
29 #include <ufs/inode.h>
30 #include <ufs/fs.h>
31 #define _BSD
32 #define _KERNEL
33 #include <ufs/dir.h>
34 #undef  _KERNEL
35 #undef  _BSD
36 #else /* AFS_OSF_ENV */
37 #ifdef AFS_VFSINCL_ENV
38 #include <sys/vnode.h>
39 #ifdef    AFS_SUN5_ENV
40 #include <sys/fs/ufs_inode.h>
41 #include <sys/fs/ufs_fs.h>
42 #define _KERNEL
43 #include <sys/fs/ufs_fsdir.h>
44 #undef _KERNEL
45 #include <sys/fs/ufs_mount.h>
46 #else
47 #include <ufs/inode.h>
48 #include <ufs/fs.h>
49 #endif
50 #else /* AFS_VFSINCL_ENV */
51 #include <sys/inode.h>
52 #ifdef  AFS_HPUX_ENV
53 #define LONGFILENAMES   1
54 #include <sys/sysmacros.h>
55 #include <sys/ino.h>
56 #endif
57 #include <sys/fs.h>
58 #endif /* AFS_VFSINCL_ENV */
59 #endif /* AFS_OSF_ENV */
60 #include <afs/osi_inode.h>
61
62 #include "fsck.h"
63
64 int pass4check();
65
66 pass4()
67 {
68     ino_t inumber;
69     struct zlncnt *zlnp;
70     struct inodesc idesc;
71     int n;
72 #if defined(ACLS) && defined(AFS_HPUX_ENV)
73     struct dinode *dp;
74 #endif /* ACLS */
75
76
77     memset(&idesc, 0, sizeof(struct inodesc));
78     idesc.id_type = ADDR;
79     idesc.id_func = pass4check;
80     for (inumber = ROOTINO; inumber <= lastino; inumber++) {
81         idesc.id_number = inumber;
82 #if defined(ACLS) && defined(AFS_HPUX_ENV)
83         switch (statemap[inumber] & STATE) {
84 #else /* no ACLS */
85         switch (statemap[inumber]) {
86 #endif /* ACLS */
87
88         case FSTATE:
89         case DFOUND:
90             n = lncntp[inumber];
91             if (n)
92                 adjust(&idesc, (short)n);
93             else {
94                 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
95                     if (zlnp->zlncnt == inumber) {
96                         zlnp->zlncnt = zlnhead->zlncnt;
97                         zlnp = zlnhead;
98                         zlnhead = zlnhead->next;
99                         free((char *)zlnp);
100                         clri(&idesc, "UNREF", 1);
101                         break;
102                     }
103             }
104             break;
105
106 #ifdef VICE
107         case VSTATE:
108             nViceFiles++;
109             break;
110 #endif /* VICE */
111
112         case DSTATE:
113             clri(&idesc, "UNREF", 1);
114             break;
115
116         case DCLEAR:
117         case FCLEAR:
118             clri(&idesc, "BAD/DUP", 1);
119             break;
120
121         case USTATE:
122             break;
123
124 #if defined(ACLS) && defined(AFS_HPUX_ENV)
125             /*
126              * UNreferenced continuation inode
127              */
128         case CSTATE:
129             clri(&idesc, "UNREF", 2);
130             break;
131
132             /*
133              * referenced continuation inode
134              */
135         case CRSTATE:
136             if ((dp = ginode(inumber)) == NULL)
137                 break;
138             if (dp->di_nlink != 1)
139                 if (!qflag) {
140                     pwarn("BAD LINK COUNT IN CONTINUATION INODE ");
141                     pwarn("I=%u (%ld should be %ld)", inumber, dp->di_nlink,
142                           1);
143                     if (preen)
144                         printf(" (CORRECTED)\n");
145                     else {
146                         if (reply("CORRECT") == 0)
147                             continue;
148                     }
149                     dp->di_nlink = 1;
150                     inodirty();
151                 }
152             break;
153 #endif /* ACLS */
154
155         default:
156             errexit("BAD STATE %d FOR INODE I=%d", statemap[inumber],
157                     inumber);
158         }
159     }
160 }
161
162 pass4check(idesc)
163      struct inodesc *idesc;
164 {
165     struct dups *dlp;
166     int nfrags, res = KEEPON;
167     daddr_t blkno = idesc->id_blkno;
168
169     for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
170         if (chkrange(blkno, 1)) {
171             res = SKIP;
172         } else if (testbmap(blkno)) {
173             for (dlp = duplist; dlp; dlp = dlp->next) {
174                 if (dlp->dup != blkno)
175                     continue;
176                 dlp->dup = duplist->dup;
177                 dlp = duplist;
178                 duplist = duplist->next;
179                 free((char *)dlp);
180                 break;
181             }
182             if (dlp == 0) {
183                 clrbmap(blkno);
184                 n_blks--;
185             }
186         }
187     }
188     return (res);
189 }