dir.c revision 1.3 1 1.3 perseant /* $NetBSD: dir.c,v 1.3 2000/05/23 01:48:52 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.1 perseant #include <ufs/ufs/dinode.h>
39 1.1 perseant #include <ufs/ufs/dir.h>
40 1.3 perseant #include <sys/mount.h> /* XXX */
41 1.1 perseant #include <ufs/lfs/lfs.h>
42 1.1 perseant
43 1.1 perseant #include <stdio.h>
44 1.1 perseant #include <stdlib.h>
45 1.1 perseant #include <string.h>
46 1.1 perseant
47 1.1 perseant #include "fsck.h"
48 1.1 perseant #include "fsutil.h"
49 1.1 perseant #include "extern.h"
50 1.1 perseant
51 1.3 perseant char *lfname = "lost+found";
52 1.3 perseant int lfmode = 01700;
53 1.3 perseant struct dirtemplate emptydir = {0, DIRBLKSIZ};
54 1.3 perseant struct dirtemplate dirhead = {
55 1.1 perseant 0, 12, DT_DIR, 1, ".",
56 1.1 perseant 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
57 1.1 perseant };
58 1.3 perseant struct odirtemplate odirhead = {
59 1.1 perseant 0, 12, 1, ".",
60 1.1 perseant 0, DIRBLKSIZ - 12, 2, ".."
61 1.1 perseant };
62 1.1 perseant
63 1.3 perseant static int expanddir(struct dinode *, char *);
64 1.3 perseant static void freedir(ino_t, ino_t);
65 1.3 perseant static struct direct *fsck_readdir(struct inodesc *);
66 1.3 perseant static struct bufarea *getdirblk(daddr_t, long);
67 1.3 perseant static int lftempname(char *, ino_t);
68 1.3 perseant static int mkentry(struct inodesc *);
69 1.3 perseant static int chgino(struct inodesc *);
70 1.1 perseant
71 1.1 perseant /*
72 1.1 perseant * Propagate connected state through the tree.
73 1.1 perseant */
74 1.1 perseant void
75 1.1 perseant propagate()
76 1.1 perseant {
77 1.1 perseant register struct inoinfo **inpp, *inp, *pinp;
78 1.1 perseant struct inoinfo **inpend;
79 1.1 perseant
80 1.1 perseant /*
81 1.1 perseant * Create a list of children for each directory.
82 1.1 perseant */
83 1.1 perseant inpend = &inpsort[inplast];
84 1.1 perseant for (inpp = inpsort; inpp < inpend; inpp++) {
85 1.1 perseant inp = *inpp;
86 1.1 perseant if (inp->i_parent == 0 ||
87 1.1 perseant inp->i_number == ROOTINO)
88 1.1 perseant continue;
89 1.1 perseant pinp = getinoinfo(inp->i_parent);
90 1.1 perseant inp->i_parentp = pinp;
91 1.1 perseant inp->i_sibling = pinp->i_child;
92 1.1 perseant pinp->i_child = inp;
93 1.1 perseant }
94 1.1 perseant inp = getinoinfo(ROOTINO);
95 1.1 perseant while (inp) {
96 1.1 perseant statemap[inp->i_number] = DFOUND;
97 1.1 perseant if (inp->i_child &&
98 1.1 perseant statemap[inp->i_child->i_number] == DSTATE)
99 1.1 perseant inp = inp->i_child;
100 1.1 perseant else if (inp->i_sibling)
101 1.1 perseant inp = inp->i_sibling;
102 1.1 perseant else
103 1.1 perseant inp = inp->i_parentp;
104 1.1 perseant }
105 1.1 perseant }
106 1.1 perseant
107 1.1 perseant /*
108 1.1 perseant * Scan each entry in a directory block.
109 1.1 perseant */
110 1.1 perseant int
111 1.3 perseant dirscan(struct inodesc * idesc)
112 1.1 perseant {
113 1.1 perseant register struct direct *dp;
114 1.1 perseant register struct bufarea *bp;
115 1.3 perseant int dsize, n;
116 1.3 perseant long blksiz;
117 1.3 perseant char dbuf[DIRBLKSIZ];
118 1.1 perseant
119 1.1 perseant if (idesc->id_type != DATA)
120 1.1 perseant errexit("wrong type to dirscan %d\n", idesc->id_type);
121 1.1 perseant if (idesc->id_entryno == 0 &&
122 1.1 perseant (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
123 1.1 perseant idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
124 1.1 perseant blksiz = idesc->id_numfrags * sblock.lfs_fsize;
125 1.1 perseant if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
126 1.1 perseant idesc->id_filesize -= blksiz;
127 1.1 perseant return (SKIP);
128 1.1 perseant }
129 1.1 perseant idesc->id_loc = 0;
130 1.1 perseant for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
131 1.1 perseant dsize = dp->d_reclen;
132 1.1 perseant memcpy(dbuf, dp, (size_t)dsize);
133 1.1 perseant # if (BYTE_ORDER == LITTLE_ENDIAN)
134 1.1 perseant if (!newinofmt) {
135 1.3 perseant struct direct *tdp = (struct direct *)dbuf;
136 1.3 perseant u_char tmp;
137 1.1 perseant
138 1.1 perseant tmp = tdp->d_namlen;
139 1.1 perseant tdp->d_namlen = tdp->d_type;
140 1.1 perseant tdp->d_type = tmp;
141 1.1 perseant }
142 1.1 perseant # endif
143 1.1 perseant idesc->id_dirp = (struct direct *)dbuf;
144 1.1 perseant if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
145 1.1 perseant # if (BYTE_ORDER == LITTLE_ENDIAN)
146 1.1 perseant if (!newinofmt && !doinglevel2) {
147 1.3 perseant struct direct *tdp;
148 1.3 perseant u_char tmp;
149 1.1 perseant
150 1.1 perseant tdp = (struct direct *)dbuf;
151 1.1 perseant tmp = tdp->d_namlen;
152 1.1 perseant tdp->d_namlen = tdp->d_type;
153 1.1 perseant tdp->d_type = tmp;
154 1.1 perseant }
155 1.1 perseant # endif
156 1.1 perseant bp = getdirblk(idesc->id_blkno, blksiz);
157 1.1 perseant memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
158 1.3 perseant (size_t)dsize);
159 1.1 perseant dirty(bp);
160 1.1 perseant sbdirty();
161 1.1 perseant }
162 1.3 perseant if (n & STOP)
163 1.1 perseant return (n);
164 1.1 perseant }
165 1.1 perseant return (idesc->id_filesize > 0 ? KEEPON : STOP);
166 1.1 perseant }
167 1.1 perseant
168 1.1 perseant /*
169 1.1 perseant * get next entry in a directory.
170 1.1 perseant */
171 1.1 perseant static struct direct *
172 1.3 perseant fsck_readdir(struct inodesc * idesc)
173 1.1 perseant {
174 1.1 perseant register struct direct *dp, *ndp;
175 1.1 perseant register struct bufarea *bp;
176 1.3 perseant long size, blksiz, fix, dploc;
177 1.1 perseant
178 1.1 perseant blksiz = idesc->id_numfrags * sblock.lfs_fsize;
179 1.1 perseant bp = getdirblk(idesc->id_blkno, blksiz);
180 1.1 perseant if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
181 1.1 perseant idesc->id_loc < blksiz) {
182 1.1 perseant dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
183 1.1 perseant if (dircheck(idesc, dp))
184 1.1 perseant goto dpok;
185 1.1 perseant if (idesc->id_fix == IGNORE)
186 1.1 perseant return (0);
187 1.1 perseant fix = dofix(idesc, "DIRECTORY CORRUPTED");
188 1.1 perseant bp = getdirblk(idesc->id_blkno, blksiz);
189 1.1 perseant dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
190 1.1 perseant dp->d_reclen = DIRBLKSIZ;
191 1.1 perseant dp->d_ino = 0;
192 1.1 perseant dp->d_type = 0;
193 1.1 perseant dp->d_namlen = 0;
194 1.1 perseant dp->d_name[0] = '\0';
195 1.1 perseant if (fix)
196 1.1 perseant dirty(bp);
197 1.1 perseant idesc->id_loc += DIRBLKSIZ;
198 1.1 perseant idesc->id_filesize -= DIRBLKSIZ;
199 1.1 perseant return (dp);
200 1.1 perseant }
201 1.1 perseant dpok:
202 1.1 perseant if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
203 1.1 perseant return NULL;
204 1.1 perseant dploc = idesc->id_loc;
205 1.1 perseant dp = (struct direct *)(bp->b_un.b_buf + dploc);
206 1.1 perseant idesc->id_loc += dp->d_reclen;
207 1.1 perseant idesc->id_filesize -= dp->d_reclen;
208 1.1 perseant if ((idesc->id_loc % DIRBLKSIZ) == 0)
209 1.1 perseant return (dp);
210 1.1 perseant ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
211 1.1 perseant if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
212 1.1 perseant dircheck(idesc, ndp) == 0) {
213 1.1 perseant size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
214 1.1 perseant idesc->id_loc += size;
215 1.1 perseant idesc->id_filesize -= size;
216 1.1 perseant if (idesc->id_fix == IGNORE)
217 1.1 perseant return (0);
218 1.1 perseant fix = dofix(idesc, "DIRECTORY CORRUPTED");
219 1.1 perseant bp = getdirblk(idesc->id_blkno, blksiz);
220 1.1 perseant dp = (struct direct *)(bp->b_un.b_buf + dploc);
221 1.1 perseant dp->d_reclen += size;
222 1.1 perseant if (fix)
223 1.1 perseant dirty(bp);
224 1.1 perseant }
225 1.1 perseant return (dp);
226 1.1 perseant }
227 1.1 perseant
228 1.1 perseant /*
229 1.1 perseant * Verify that a directory entry is valid.
230 1.1 perseant * This is a superset of the checks made in the kernel.
231 1.1 perseant */
232 1.1 perseant int
233 1.3 perseant dircheck(struct inodesc * idesc, struct direct * dp)
234 1.1 perseant {
235 1.3 perseant register int size;
236 1.3 perseant register char *cp;
237 1.3 perseant u_char namlen, type;
238 1.3 perseant int spaceleft;
239 1.1 perseant
240 1.1 perseant spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
241 1.1 perseant if (dp->d_ino >= maxino ||
242 1.1 perseant dp->d_reclen == 0 ||
243 1.1 perseant dp->d_reclen > spaceleft ||
244 1.3 perseant (dp->d_reclen & 0x3) != 0) {
245 1.3 perseant pwarn("ino too large, reclen=0, reclen>space, or reclen&3!=0\n");
246 1.3 perseant pwarn("dp->d_ino = 0x%x\tdp->d_reclen = 0x%x\n",
247 1.3 perseant dp->d_ino, dp->d_reclen);
248 1.3 perseant pwarn("maxino = 0x%x\tspaceleft = 0x%x\n", maxino, spaceleft);
249 1.3 perseant return (0);
250 1.3 perseant }
251 1.1 perseant if (dp->d_ino == 0)
252 1.1 perseant return (1);
253 1.1 perseant size = DIRSIZ(!newinofmt, dp, 0);
254 1.1 perseant # if (BYTE_ORDER == LITTLE_ENDIAN)
255 1.1 perseant if (!newinofmt) {
256 1.1 perseant type = dp->d_namlen;
257 1.1 perseant namlen = dp->d_type;
258 1.1 perseant } else {
259 1.1 perseant namlen = dp->d_namlen;
260 1.1 perseant type = dp->d_type;
261 1.1 perseant }
262 1.1 perseant # else
263 1.1 perseant namlen = dp->d_namlen;
264 1.1 perseant type = dp->d_type;
265 1.1 perseant # endif
266 1.1 perseant if (dp->d_reclen < size ||
267 1.1 perseant idesc->id_filesize < size ||
268 1.1 perseant namlen > MAXNAMLEN ||
269 1.3 perseant type > 15) {
270 1.3 perseant printf("reclen<size, filesize<size, namlen too large, or type>15\n");
271 1.1 perseant return (0);
272 1.3 perseant }
273 1.1 perseant for (cp = dp->d_name, size = 0; size < namlen; size++)
274 1.1 perseant if (*cp == '\0' || (*cp++ == '/')) {
275 1.3 perseant printf("name contains NUL or /\n");
276 1.1 perseant return (0);
277 1.3 perseant }
278 1.1 perseant if (*cp != '\0') {
279 1.3 perseant printf("name size misstated\n");
280 1.1 perseant return (0);
281 1.3 perseant }
282 1.1 perseant return (1);
283 1.1 perseant }
284 1.1 perseant
285 1.1 perseant void
286 1.3 perseant direrror(ino_t ino, char *errmesg)
287 1.1 perseant {
288 1.1 perseant
289 1.1 perseant fileerror(ino, ino, errmesg);
290 1.1 perseant }
291 1.1 perseant
292 1.1 perseant void
293 1.3 perseant fileerror(ino_t cwd, ino_t ino, char *errmesg)
294 1.1 perseant {
295 1.1 perseant register struct dinode *dp;
296 1.3 perseant char pathbuf[MAXPATHLEN + 1];
297 1.1 perseant
298 1.1 perseant pwarn("%s ", errmesg);
299 1.1 perseant pinode(ino);
300 1.1 perseant printf("\n");
301 1.1 perseant getpathname(pathbuf, cwd, ino);
302 1.1 perseant if (ino < ROOTINO || ino > maxino) {
303 1.1 perseant pfatal("NAME=%s\n", pathbuf);
304 1.1 perseant return;
305 1.1 perseant }
306 1.1 perseant dp = ginode(ino);
307 1.3 perseant if (dp == NULL)
308 1.3 perseant pfatal("INO is NULL\n");
309 1.3 perseant else {
310 1.3 perseant if (ftypeok(dp))
311 1.3 perseant pfatal("%s=%s\n",
312 1.3 perseant (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
313 1.3 perseant else
314 1.3 perseant pfatal("NAME=%s\n", pathbuf);
315 1.3 perseant }
316 1.1 perseant }
317 1.1 perseant
318 1.1 perseant void
319 1.3 perseant adjust(struct inodesc * idesc, short lcnt)
320 1.1 perseant {
321 1.1 perseant register struct dinode *dp;
322 1.1 perseant
323 1.1 perseant dp = ginode(idesc->id_number);
324 1.1 perseant if (dp->di_nlink == lcnt) {
325 1.1 perseant if (linkup(idesc->id_number, (ino_t)0) == 0)
326 1.1 perseant clri(idesc, "UNREF", 0);
327 1.1 perseant } else {
328 1.1 perseant pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
329 1.3 perseant ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
330 1.1 perseant pinode(idesc->id_number);
331 1.1 perseant printf(" COUNT %d SHOULD BE %d",
332 1.3 perseant dp->di_nlink, dp->di_nlink - lcnt);
333 1.1 perseant if (preen) {
334 1.1 perseant if (lcnt < 0) {
335 1.1 perseant printf("\n");
336 1.1 perseant pfatal("LINK COUNT INCREASING");
337 1.1 perseant }
338 1.1 perseant printf(" (ADJUSTED)\n");
339 1.1 perseant }
340 1.1 perseant if (preen || reply("ADJUST") == 1) {
341 1.1 perseant dp->di_nlink -= lcnt;
342 1.1 perseant inodirty();
343 1.1 perseant }
344 1.1 perseant }
345 1.1 perseant }
346 1.1 perseant
347 1.1 perseant static int
348 1.3 perseant mkentry(struct inodesc * idesc)
349 1.1 perseant {
350 1.1 perseant register struct direct *dirp = idesc->id_dirp;
351 1.3 perseant struct direct newent;
352 1.3 perseant int newlen, oldlen;
353 1.1 perseant
354 1.1 perseant newent.d_namlen = strlen(idesc->id_name);
355 1.1 perseant newlen = DIRSIZ(0, &newent, 0);
356 1.1 perseant if (dirp->d_ino != 0)
357 1.1 perseant oldlen = DIRSIZ(0, dirp, 0);
358 1.1 perseant else
359 1.1 perseant oldlen = 0;
360 1.1 perseant if (dirp->d_reclen - oldlen < newlen)
361 1.1 perseant return (KEEPON);
362 1.1 perseant newent.d_reclen = dirp->d_reclen - oldlen;
363 1.1 perseant dirp->d_reclen = oldlen;
364 1.1 perseant dirp = (struct direct *)(((char *)dirp) + oldlen);
365 1.1 perseant dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
366 1.1 perseant dirp->d_reclen = newent.d_reclen;
367 1.1 perseant if (newinofmt)
368 1.1 perseant dirp->d_type = typemap[idesc->id_parent];
369 1.1 perseant else
370 1.1 perseant dirp->d_type = 0;
371 1.1 perseant dirp->d_namlen = newent.d_namlen;
372 1.1 perseant memcpy(dirp->d_name, idesc->id_name, (size_t)dirp->d_namlen + 1);
373 1.1 perseant # if (BYTE_ORDER == LITTLE_ENDIAN)
374 1.1 perseant /*
375 1.3 perseant * If the entry was split, dirscan()will only reverse the byte
376 1.3 perseant * order of the original entry, and not the new one, before
377 1.3 perseant * writing it back out. So, we reverse the byte order here if
378 1.3 perseant * necessary.
379 1.3 perseant */
380 1.1 perseant if (oldlen != 0 && !newinofmt && !doinglevel2) {
381 1.3 perseant u_char tmp;
382 1.1 perseant
383 1.1 perseant tmp = dirp->d_namlen;
384 1.1 perseant dirp->d_namlen = dirp->d_type;
385 1.1 perseant dirp->d_type = tmp;
386 1.1 perseant }
387 1.1 perseant # endif
388 1.3 perseant return (ALTERED | STOP);
389 1.1 perseant }
390 1.1 perseant
391 1.1 perseant static int
392 1.3 perseant chgino(struct inodesc * idesc)
393 1.1 perseant {
394 1.1 perseant register struct direct *dirp = idesc->id_dirp;
395 1.1 perseant
396 1.1 perseant if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
397 1.1 perseant return (KEEPON);
398 1.1 perseant dirp->d_ino = idesc->id_parent;
399 1.1 perseant if (newinofmt)
400 1.1 perseant dirp->d_type = typemap[idesc->id_parent];
401 1.1 perseant else
402 1.1 perseant dirp->d_type = 0;
403 1.3 perseant return (ALTERED | STOP);
404 1.1 perseant }
405 1.1 perseant
406 1.1 perseant int
407 1.3 perseant linkup(ino_t orphan, ino_t parentdir)
408 1.1 perseant {
409 1.1 perseant register struct dinode *dp;
410 1.3 perseant int lostdir;
411 1.3 perseant ino_t oldlfdir;
412 1.3 perseant struct inodesc idesc;
413 1.3 perseant char tempname[BUFSIZ];
414 1.1 perseant
415 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
416 1.1 perseant dp = ginode(orphan);
417 1.1 perseant lostdir = (dp->di_mode & IFMT) == IFDIR;
418 1.1 perseant pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
419 1.1 perseant pinode(orphan);
420 1.1 perseant if (preen && dp->di_size == 0)
421 1.1 perseant return (0);
422 1.1 perseant if (preen)
423 1.1 perseant printf(" (RECONNECTED)\n");
424 1.3 perseant else if (reply("RECONNECT") == 0)
425 1.3 perseant return (0);
426 1.1 perseant if (lfdir == 0) {
427 1.1 perseant dp = ginode(ROOTINO);
428 1.1 perseant idesc.id_name = lfname;
429 1.1 perseant idesc.id_type = DATA;
430 1.1 perseant idesc.id_func = findino;
431 1.1 perseant idesc.id_number = ROOTINO;
432 1.1 perseant if ((ckinode(dp, &idesc) & FOUND) != 0) {
433 1.1 perseant lfdir = idesc.id_parent;
434 1.1 perseant } else {
435 1.1 perseant pwarn("NO lost+found DIRECTORY");
436 1.1 perseant if (preen || reply("CREATE")) {
437 1.1 perseant lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
438 1.1 perseant if (lfdir != 0) {
439 1.1 perseant if (makeentry(ROOTINO, lfdir, lfname) != 0) {
440 1.1 perseant if (preen)
441 1.1 perseant printf(" (CREATED)\n");
442 1.1 perseant } else {
443 1.1 perseant freedir(lfdir, ROOTINO);
444 1.1 perseant lfdir = 0;
445 1.1 perseant if (preen)
446 1.1 perseant printf("\n");
447 1.1 perseant }
448 1.1 perseant }
449 1.1 perseant }
450 1.1 perseant }
451 1.1 perseant if (lfdir == 0) {
452 1.1 perseant pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
453 1.1 perseant printf("\n\n");
454 1.1 perseant return (0);
455 1.1 perseant }
456 1.1 perseant }
457 1.1 perseant dp = ginode(lfdir);
458 1.1 perseant if ((dp->di_mode & IFMT) != IFDIR) {
459 1.1 perseant pfatal("lost+found IS NOT A DIRECTORY");
460 1.1 perseant if (reply("REALLOCATE") == 0)
461 1.1 perseant return (0);
462 1.1 perseant oldlfdir = lfdir;
463 1.1 perseant if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
464 1.1 perseant pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
465 1.1 perseant return (0);
466 1.1 perseant }
467 1.1 perseant if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
468 1.1 perseant pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
469 1.1 perseant return (0);
470 1.1 perseant }
471 1.1 perseant inodirty();
472 1.1 perseant idesc.id_type = ADDR;
473 1.1 perseant idesc.id_func = pass4check;
474 1.1 perseant idesc.id_number = oldlfdir;
475 1.1 perseant adjust(&idesc, lncntp[oldlfdir] + 1);
476 1.1 perseant lncntp[oldlfdir] = 0;
477 1.1 perseant dp = ginode(lfdir);
478 1.1 perseant }
479 1.1 perseant if (statemap[lfdir] != DFOUND) {
480 1.1 perseant pfatal("SORRY. NO lost+found DIRECTORY\n\n");
481 1.1 perseant return (0);
482 1.1 perseant }
483 1.1 perseant (void)lftempname(tempname, orphan);
484 1.1 perseant if (makeentry(lfdir, orphan, tempname) == 0) {
485 1.1 perseant pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
486 1.1 perseant printf("\n\n");
487 1.1 perseant return (0);
488 1.1 perseant }
489 1.1 perseant lncntp[orphan]--;
490 1.1 perseant if (lostdir) {
491 1.1 perseant if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
492 1.1 perseant parentdir != (ino_t)-1)
493 1.1 perseant (void)makeentry(orphan, lfdir, "..");
494 1.1 perseant dp = ginode(lfdir);
495 1.1 perseant dp->di_nlink++;
496 1.1 perseant inodirty();
497 1.1 perseant lncntp[lfdir]++;
498 1.1 perseant pwarn("DIR I=%u CONNECTED. ", orphan);
499 1.1 perseant if (parentdir != (ino_t)-1)
500 1.1 perseant printf("PARENT WAS I=%u\n", parentdir);
501 1.1 perseant if (preen == 0)
502 1.1 perseant printf("\n");
503 1.1 perseant }
504 1.1 perseant return (1);
505 1.1 perseant }
506 1.1 perseant
507 1.1 perseant /*
508 1.1 perseant * fix an entry in a directory.
509 1.1 perseant */
510 1.1 perseant int
511 1.3 perseant changeino(ino_t dir, char *name, ino_t newnum)
512 1.1 perseant {
513 1.3 perseant struct inodesc idesc;
514 1.1 perseant
515 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
516 1.1 perseant idesc.id_type = DATA;
517 1.1 perseant idesc.id_func = chgino;
518 1.1 perseant idesc.id_number = dir;
519 1.1 perseant idesc.id_fix = DONTKNOW;
520 1.1 perseant idesc.id_name = name;
521 1.1 perseant idesc.id_parent = newnum; /* new value for name */
522 1.1 perseant return (ckinode(ginode(dir), &idesc));
523 1.1 perseant }
524 1.1 perseant
525 1.1 perseant /*
526 1.1 perseant * make an entry in a directory
527 1.1 perseant */
528 1.1 perseant int
529 1.3 perseant makeentry(ino_t parent, ino_t ino, char *name)
530 1.3 perseant {
531 1.3 perseant struct dinode *dp;
532 1.3 perseant struct inodesc idesc;
533 1.3 perseant char pathbuf[MAXPATHLEN + 1];
534 1.3 perseant
535 1.1 perseant if (parent < ROOTINO || parent >= maxino ||
536 1.1 perseant ino < ROOTINO || ino >= maxino)
537 1.1 perseant return (0);
538 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
539 1.1 perseant idesc.id_type = DATA;
540 1.1 perseant idesc.id_func = mkentry;
541 1.1 perseant idesc.id_number = parent;
542 1.1 perseant idesc.id_parent = ino; /* this is the inode to enter */
543 1.1 perseant idesc.id_fix = DONTKNOW;
544 1.1 perseant idesc.id_name = name;
545 1.1 perseant dp = ginode(parent);
546 1.1 perseant if (dp->di_size % DIRBLKSIZ) {
547 1.1 perseant dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
548 1.1 perseant inodirty();
549 1.1 perseant }
550 1.1 perseant if ((ckinode(dp, &idesc) & ALTERED) != 0)
551 1.1 perseant return (1);
552 1.1 perseant getpathname(pathbuf, parent, parent);
553 1.1 perseant dp = ginode(parent);
554 1.1 perseant if (expanddir(dp, pathbuf) == 0)
555 1.1 perseant return (0);
556 1.1 perseant return (ckinode(dp, &idesc) & ALTERED);
557 1.1 perseant }
558 1.1 perseant
559 1.1 perseant /*
560 1.1 perseant * Attempt to expand the size of a directory
561 1.1 perseant */
562 1.1 perseant static int
563 1.3 perseant expanddir(struct dinode * dp, char *name)
564 1.1 perseant {
565 1.3 perseant daddr_t lastbn, newblk;
566 1.1 perseant register struct bufarea *bp;
567 1.3 perseant char *cp, firstblk[DIRBLKSIZ];
568 1.1 perseant
569 1.1 perseant lastbn = lblkno(&sblock, dp->di_size);
570 1.1 perseant if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
571 1.1 perseant return (0);
572 1.1 perseant if ((newblk = allocblk(sblock.lfs_frag)) == 0)
573 1.1 perseant return (0);
574 1.1 perseant dp->di_db[lastbn + 1] = dp->di_db[lastbn];
575 1.1 perseant dp->di_db[lastbn] = newblk;
576 1.1 perseant dp->di_size += sblock.lfs_bsize;
577 1.1 perseant dp->di_blocks += btodb(sblock.lfs_bsize);
578 1.1 perseant bp = getdirblk(dp->di_db[lastbn + 1],
579 1.3 perseant (long)dblksize(&sblock, dp, lastbn + 1));
580 1.1 perseant if (bp->b_errs)
581 1.1 perseant goto bad;
582 1.1 perseant memcpy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
583 1.1 perseant bp = getdirblk(newblk, sblock.lfs_bsize);
584 1.1 perseant if (bp->b_errs)
585 1.1 perseant goto bad;
586 1.1 perseant memcpy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
587 1.1 perseant for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
588 1.1 perseant cp < &bp->b_un.b_buf[sblock.lfs_bsize];
589 1.1 perseant cp += DIRBLKSIZ)
590 1.1 perseant memcpy(cp, &emptydir, sizeof emptydir);
591 1.1 perseant dirty(bp);
592 1.1 perseant bp = getdirblk(dp->di_db[lastbn + 1],
593 1.3 perseant (long)dblksize(&sblock, dp, lastbn + 1));
594 1.1 perseant if (bp->b_errs)
595 1.1 perseant goto bad;
596 1.1 perseant memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
597 1.1 perseant pwarn("NO SPACE LEFT IN %s", name);
598 1.1 perseant if (preen)
599 1.1 perseant printf(" (EXPANDED)\n");
600 1.1 perseant else if (reply("EXPAND") == 0)
601 1.1 perseant goto bad;
602 1.1 perseant dirty(bp);
603 1.1 perseant inodirty();
604 1.1 perseant return (1);
605 1.1 perseant bad:
606 1.1 perseant dp->di_db[lastbn] = dp->di_db[lastbn + 1];
607 1.1 perseant dp->di_db[lastbn + 1] = 0;
608 1.1 perseant dp->di_size -= sblock.lfs_bsize;
609 1.1 perseant dp->di_blocks -= btodb(sblock.lfs_bsize);
610 1.1 perseant freeblk(newblk, sblock.lfs_frag);
611 1.1 perseant return (0);
612 1.1 perseant }
613 1.1 perseant
614 1.1 perseant /*
615 1.1 perseant * allocate a new directory
616 1.1 perseant */
617 1.1 perseant int
618 1.3 perseant allocdir(ino_t parent, ino_t request, int mode)
619 1.3 perseant {
620 1.3 perseant ino_t ino;
621 1.3 perseant char *cp;
622 1.3 perseant struct dinode *dp;
623 1.1 perseant register struct bufarea *bp;
624 1.1 perseant struct dirtemplate *dirp;
625 1.1 perseant
626 1.3 perseant ino = allocino(request, IFDIR | mode);
627 1.1 perseant if (newinofmt)
628 1.1 perseant dirp = &dirhead;
629 1.1 perseant else
630 1.3 perseant dirp = (struct dirtemplate *) & odirhead;
631 1.1 perseant dirp->dot_ino = ino;
632 1.1 perseant dirp->dotdot_ino = parent;
633 1.1 perseant dp = ginode(ino);
634 1.1 perseant bp = getdirblk(dp->di_db[0], sblock.lfs_fsize);
635 1.1 perseant if (bp->b_errs) {
636 1.1 perseant freeino(ino);
637 1.1 perseant return (0);
638 1.1 perseant }
639 1.1 perseant memcpy(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
640 1.1 perseant for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
641 1.1 perseant cp < &bp->b_un.b_buf[sblock.lfs_fsize];
642 1.1 perseant cp += DIRBLKSIZ)
643 1.1 perseant memcpy(cp, &emptydir, sizeof emptydir);
644 1.1 perseant dirty(bp);
645 1.1 perseant dp->di_nlink = 2;
646 1.1 perseant inodirty();
647 1.1 perseant if (ino == ROOTINO) {
648 1.1 perseant lncntp[ino] = dp->di_nlink;
649 1.1 perseant cacheino(dp, ino);
650 1.3 perseant return (ino);
651 1.1 perseant }
652 1.1 perseant if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
653 1.1 perseant freeino(ino);
654 1.1 perseant return (0);
655 1.1 perseant }
656 1.1 perseant cacheino(dp, ino);
657 1.1 perseant statemap[ino] = statemap[parent];
658 1.1 perseant if (statemap[ino] == DSTATE) {
659 1.1 perseant lncntp[ino] = dp->di_nlink;
660 1.1 perseant lncntp[parent]++;
661 1.1 perseant }
662 1.1 perseant dp = ginode(parent);
663 1.1 perseant dp->di_nlink++;
664 1.1 perseant inodirty();
665 1.1 perseant return (ino);
666 1.1 perseant }
667 1.1 perseant
668 1.1 perseant /*
669 1.1 perseant * free a directory inode
670 1.1 perseant */
671 1.1 perseant static void
672 1.3 perseant freedir(ino_t ino, ino_t parent)
673 1.1 perseant {
674 1.3 perseant struct dinode *dp;
675 1.1 perseant
676 1.1 perseant if (ino != parent) {
677 1.1 perseant dp = ginode(parent);
678 1.1 perseant dp->di_nlink--;
679 1.1 perseant inodirty();
680 1.1 perseant }
681 1.1 perseant freeino(ino);
682 1.1 perseant }
683 1.1 perseant
684 1.1 perseant /*
685 1.1 perseant * generate a temporary name for the lost+found directory.
686 1.1 perseant */
687 1.1 perseant static int
688 1.3 perseant lftempname(char *bufp, ino_t ino)
689 1.3 perseant {
690 1.3 perseant register ino_t in;
691 1.3 perseant register char *cp;
692 1.3 perseant int namlen;
693 1.1 perseant
694 1.1 perseant cp = bufp + 2;
695 1.1 perseant for (in = maxino; in > 0; in /= 10)
696 1.1 perseant cp++;
697 1.1 perseant *--cp = 0;
698 1.1 perseant namlen = cp - bufp;
699 1.1 perseant in = ino;
700 1.1 perseant while (cp > bufp) {
701 1.1 perseant *--cp = (in % 10) + '0';
702 1.1 perseant in /= 10;
703 1.1 perseant }
704 1.1 perseant *cp = '#';
705 1.1 perseant return (namlen);
706 1.1 perseant }
707 1.1 perseant
708 1.1 perseant /*
709 1.1 perseant * Get a directory block.
710 1.1 perseant * Ensure that it is held until another is requested.
711 1.1 perseant */
712 1.1 perseant static struct bufarea *
713 1.3 perseant getdirblk(daddr_t blkno, long size)
714 1.1 perseant {
715 1.1 perseant
716 1.1 perseant if (pdirbp != 0)
717 1.1 perseant pdirbp->b_flags &= ~B_INUSE;
718 1.1 perseant /* pdirbp = getdatablk(blkno, size); */
719 1.3 perseant pdirbp = getddblk(blkno, size);
720 1.1 perseant return (pdirbp);
721 1.1 perseant }
722