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