Commit | Line | Data |
---|---|---|
76797561 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
250baf55 | 7 | #ifndef lint |
2c4b8d40 | 8 | static char sccsid[] = "@(#)dir.c 5.2 (Berkeley) %G%"; |
76797561 | 9 | #endif not lint |
250baf55 KM |
10 | |
11 | #include <sys/param.h> | |
12 | #include <sys/inode.h> | |
13 | #include <sys/fs.h> | |
14 | #define KERNEL | |
15 | #include <sys/dir.h> | |
16 | #undef KERNEL | |
17 | #include "fsck.h" | |
18 | ||
19 | #define MINDIRSIZE (sizeof (struct dirtemplate)) | |
20 | ||
21 | char *endpathname = &pathname[BUFSIZ - 2]; | |
22 | char *lfname = "lost+found"; | |
3e2a23ca | 23 | struct dirtemplate emptydir = { 0, DIRBLKSIZ }; |
ebd05fde | 24 | struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." }; |
250baf55 KM |
25 | |
26 | DIRECT *fsck_readdir(); | |
27 | ||
28 | descend(parentino, inumber) | |
29 | struct inodesc *parentino; | |
30 | ino_t inumber; | |
31 | { | |
32 | register DINODE *dp; | |
33 | struct inodesc curino; | |
34 | ||
35 | bzero((char *)&curino, sizeof(struct inodesc)); | |
993a756c KM |
36 | if (statemap[inumber] != DSTATE) |
37 | errexit("BAD INODE %d TO DESCEND", statemap[inumber]); | |
38 | statemap[inumber] = DFOUND; | |
39c18287 | 39 | dp = ginode(inumber); |
250baf55 KM |
40 | if (dp->di_size == 0) { |
41 | direrr(inumber, "ZERO LENGTH DIRECTORY"); | |
42 | if (reply("REMOVE") == 1) | |
993a756c | 43 | statemap[inumber] = DCLEAR; |
250baf55 KM |
44 | return; |
45 | } | |
46 | if (dp->di_size < MINDIRSIZE) { | |
47 | direrr(inumber, "DIRECTORY TOO SHORT"); | |
48 | dp->di_size = MINDIRSIZE; | |
49 | if (reply("FIX") == 1) | |
50 | inodirty(); | |
51 | } | |
f4f8f56a KM |
52 | if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) { |
53 | pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", | |
54 | pathname, dp->di_size, DIRBLKSIZ); | |
55 | dp->di_size = roundup(dp->di_size, DIRBLKSIZ); | |
56 | if (preen) | |
57 | printf(" (ADJUSTED)\n"); | |
58 | if (preen || reply("ADJUST") == 1) | |
59 | inodirty(); | |
60 | } | |
250baf55 KM |
61 | curino.id_type = DATA; |
62 | curino.id_func = parentino->id_func; | |
63 | curino.id_parent = parentino->id_number; | |
64 | curino.id_number = inumber; | |
250baf55 KM |
65 | (void)ckinode(dp, &curino); |
66 | } | |
67 | ||
68 | dirscan(idesc) | |
69 | register struct inodesc *idesc; | |
70 | { | |
71 | register DIRECT *dp; | |
72 | int dsize, n; | |
73 | long blksiz; | |
74 | char dbuf[DIRBLKSIZ]; | |
75 | ||
76 | if (idesc->id_type != DATA) | |
77 | errexit("wrong type to dirscan %d\n", idesc->id_type); | |
042f816f KM |
78 | if (idesc->id_entryno == 0 && |
79 | (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) | |
80 | idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); | |
250baf55 KM |
81 | blksiz = idesc->id_numfrags * sblock.fs_fsize; |
82 | if (outrange(idesc->id_blkno, idesc->id_numfrags)) { | |
83 | idesc->id_filesize -= blksiz; | |
84 | return (SKIP); | |
85 | } | |
86 | idesc->id_loc = 0; | |
87 | for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { | |
88 | dsize = dp->d_reclen; | |
89 | bcopy((char *)dp, dbuf, dsize); | |
90 | idesc->id_dirp = (DIRECT *)dbuf; | |
91 | if ((n = (*idesc->id_func)(idesc)) & ALTERED) { | |
f4f8f56a KM |
92 | getblk(&fileblk, idesc->id_blkno, blksiz); |
93 | if (fileblk.b_errs != NULL) { | |
94 | n &= ~ALTERED; | |
95 | } else { | |
250baf55 KM |
96 | bcopy(dbuf, (char *)dp, dsize); |
97 | dirty(&fileblk); | |
98 | sbdirty(); | |
f4f8f56a | 99 | } |
250baf55 KM |
100 | } |
101 | if (n & STOP) | |
102 | return (n); | |
103 | } | |
104 | return (idesc->id_filesize > 0 ? KEEPON : STOP); | |
105 | } | |
106 | ||
107 | /* | |
108 | * get next entry in a directory. | |
109 | */ | |
110 | DIRECT * | |
111 | fsck_readdir(idesc) | |
112 | register struct inodesc *idesc; | |
113 | { | |
114 | register DIRECT *dp, *ndp; | |
115 | long size, blksiz; | |
116 | ||
117 | blksiz = idesc->id_numfrags * sblock.fs_fsize; | |
f4f8f56a KM |
118 | getblk(&fileblk, idesc->id_blkno, blksiz); |
119 | if (fileblk.b_errs != NULL) { | |
250baf55 KM |
120 | idesc->id_filesize -= blksiz - idesc->id_loc; |
121 | return NULL; | |
122 | } | |
123 | if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && | |
124 | idesc->id_loc < blksiz) { | |
125 | dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); | |
126 | if (dircheck(idesc, dp)) | |
127 | goto dpok; | |
128 | idesc->id_loc += DIRBLKSIZ; | |
129 | idesc->id_filesize -= DIRBLKSIZ; | |
130 | dp->d_reclen = DIRBLKSIZ; | |
131 | dp->d_ino = 0; | |
132 | dp->d_namlen = 0; | |
133 | dp->d_name[0] = '\0'; | |
7718c0e6 | 134 | if (dofix(idesc, "DIRECTORY CORRUPTED")) |
250baf55 KM |
135 | dirty(&fileblk); |
136 | return (dp); | |
137 | } | |
138 | dpok: | |
139 | if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) | |
140 | return NULL; | |
141 | dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); | |
142 | idesc->id_loc += dp->d_reclen; | |
143 | idesc->id_filesize -= dp->d_reclen; | |
144 | if ((idesc->id_loc % DIRBLKSIZ) == 0) | |
145 | return (dp); | |
146 | ndp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); | |
147 | if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && | |
148 | dircheck(idesc, ndp) == 0) { | |
149 | size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); | |
150 | dp->d_reclen += size; | |
151 | idesc->id_loc += size; | |
152 | idesc->id_filesize -= size; | |
7718c0e6 | 153 | if (dofix(idesc, "DIRECTORY CORRUPTED")) |
250baf55 KM |
154 | dirty(&fileblk); |
155 | } | |
156 | return (dp); | |
157 | } | |
158 | ||
159 | /* | |
160 | * Verify that a directory entry is valid. | |
161 | * This is a superset of the checks made in the kernel. | |
162 | */ | |
163 | dircheck(idesc, dp) | |
164 | struct inodesc *idesc; | |
165 | register DIRECT *dp; | |
166 | { | |
167 | register int size; | |
168 | register char *cp; | |
169 | int spaceleft; | |
170 | ||
171 | size = DIRSIZ(dp); | |
172 | spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); | |
173 | if (dp->d_ino < imax && | |
174 | dp->d_reclen != 0 && | |
175 | dp->d_reclen <= spaceleft && | |
176 | (dp->d_reclen & 0x3) == 0 && | |
177 | dp->d_reclen >= size && | |
178 | idesc->id_filesize >= size && | |
179 | dp->d_namlen <= MAXNAMLEN) { | |
180 | if (dp->d_ino == 0) | |
181 | return (1); | |
182 | for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++) | |
183 | if (*cp == 0 || (*cp++ & 0200)) | |
184 | return (0); | |
185 | if (*cp == 0) | |
186 | return (1); | |
187 | } | |
188 | return (0); | |
189 | } | |
190 | ||
191 | direrr(ino, s) | |
192 | ino_t ino; | |
193 | char *s; | |
194 | { | |
195 | register DINODE *dp; | |
196 | ||
197 | pwarn("%s ", s); | |
198 | pinode(ino); | |
199 | printf("\n"); | |
39c18287 KM |
200 | if (ino < ROOTINO || ino > imax) { |
201 | pfatal("NAME=%s\n", pathname); | |
202 | return; | |
203 | } | |
204 | dp = ginode(ino); | |
205 | if (ftypeok(dp)) | |
7718c0e6 | 206 | pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname); |
250baf55 KM |
207 | else |
208 | pfatal("NAME=%s\n", pathname); | |
209 | } | |
210 | ||
211 | adjust(idesc, lcnt) | |
212 | register struct inodesc *idesc; | |
213 | short lcnt; | |
214 | { | |
215 | register DINODE *dp; | |
216 | ||
39c18287 | 217 | dp = ginode(idesc->id_number); |
250baf55 KM |
218 | if (dp->di_nlink == lcnt) { |
219 | if (linkup(idesc->id_number, (ino_t)0) == 0) | |
220 | clri(idesc, "UNREF", 0); | |
993a756c | 221 | } else { |
7718c0e6 KM |
222 | pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : |
223 | (DIRCT(dp) ? "DIR" : "FILE")); | |
250baf55 KM |
224 | pinode(idesc->id_number); |
225 | printf(" COUNT %d SHOULD BE %d", | |
226 | dp->di_nlink, dp->di_nlink-lcnt); | |
227 | if (preen) { | |
228 | if (lcnt < 0) { | |
229 | printf("\n"); | |
7718c0e6 | 230 | pfatal("LINK COUNT INCREASING"); |
250baf55 KM |
231 | } |
232 | printf(" (ADJUSTED)\n"); | |
233 | } | |
234 | if (preen || reply("ADJUST") == 1) { | |
235 | dp->di_nlink -= lcnt; | |
236 | inodirty(); | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | mkentry(idesc) | |
242 | struct inodesc *idesc; | |
243 | { | |
244 | register DIRECT *dirp = idesc->id_dirp; | |
245 | DIRECT newent; | |
246 | int newlen, oldlen; | |
247 | ||
248 | newent.d_namlen = 11; | |
249 | newlen = DIRSIZ(&newent); | |
250 | if (dirp->d_ino != 0) | |
251 | oldlen = DIRSIZ(dirp); | |
252 | else | |
253 | oldlen = 0; | |
254 | if (dirp->d_reclen - oldlen < newlen) | |
255 | return (KEEPON); | |
256 | newent.d_reclen = dirp->d_reclen - oldlen; | |
257 | dirp->d_reclen = oldlen; | |
258 | dirp = (struct direct *)(((char *)dirp) + oldlen); | |
259 | dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ | |
260 | dirp->d_reclen = newent.d_reclen; | |
ebd05fde KM |
261 | dirp->d_namlen = strlen(idesc->id_name); |
262 | bcopy(idesc->id_name, dirp->d_name, dirp->d_namlen + 1); | |
250baf55 KM |
263 | return (ALTERED|STOP); |
264 | } | |
265 | ||
59e3daa2 | 266 | chgino(idesc) |
250baf55 KM |
267 | struct inodesc *idesc; |
268 | { | |
269 | register DIRECT *dirp = idesc->id_dirp; | |
270 | ||
59e3daa2 KM |
271 | if (bcmp(dirp->d_name, idesc->id_name, dirp->d_namlen + 1)) |
272 | return (KEEPON); | |
273 | dirp->d_ino = idesc->id_parent;; | |
274 | return (ALTERED|STOP); | |
250baf55 KM |
275 | } |
276 | ||
277 | linkup(orphan, pdir) | |
278 | ino_t orphan; | |
279 | ino_t pdir; | |
280 | { | |
281 | register DINODE *dp; | |
282 | int lostdir, len; | |
ebd05fde | 283 | ino_t oldlfdir; |
250baf55 | 284 | struct inodesc idesc; |
ebd05fde | 285 | char tempname[BUFSIZ]; |
59e3daa2 | 286 | extern int pass4check(); |
250baf55 KM |
287 | |
288 | bzero((char *)&idesc, sizeof(struct inodesc)); | |
39c18287 | 289 | dp = ginode(orphan); |
7718c0e6 | 290 | lostdir = DIRCT(dp); |
250baf55 KM |
291 | pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); |
292 | pinode(orphan); | |
293 | if (preen && dp->di_size == 0) | |
294 | return (0); | |
295 | if (preen) | |
296 | printf(" (RECONNECTED)\n"); | |
297 | else | |
298 | if (reply("RECONNECT") == 0) | |
299 | return (0); | |
300 | pathp = pathname; | |
301 | *pathp++ = '/'; | |
302 | *pathp = '\0'; | |
303 | if (lfdir == 0) { | |
39c18287 | 304 | dp = ginode(ROOTINO); |
7718c0e6 | 305 | idesc.id_name = lfname; |
250baf55 KM |
306 | idesc.id_type = DATA; |
307 | idesc.id_func = findino; | |
308 | idesc.id_number = ROOTINO; | |
250baf55 | 309 | (void)ckinode(dp, &idesc); |
ebd05fde KM |
310 | if (idesc.id_parent >= ROOTINO && idesc.id_parent < imax) { |
311 | lfdir = idesc.id_parent; | |
312 | } else { | |
313 | pwarn("NO lost+found DIRECTORY"); | |
314 | if (preen || reply("CREATE")) { | |
ca1a30be KM |
315 | lfdir = allocdir(ROOTINO, 0); |
316 | if (lfdir != 0) { | |
317 | if (makeentry(ROOTINO, lfdir, lfname) != 0) { | |
ebd05fde KM |
318 | if (preen) |
319 | printf(" (CREATED)\n"); | |
320 | } else { | |
ca1a30be KM |
321 | freedir(lfdir, ROOTINO); |
322 | lfdir = 0; | |
ebd05fde KM |
323 | if (preen) |
324 | printf("\n"); | |
325 | } | |
326 | } | |
327 | } | |
328 | } | |
39c18287 | 329 | if (lfdir == 0) { |
ebd05fde | 330 | pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); |
250baf55 KM |
331 | printf("\n\n"); |
332 | return (0); | |
333 | } | |
334 | } | |
39c18287 | 335 | dp = ginode(lfdir); |
59e3daa2 KM |
336 | if (!DIRCT(dp)) { |
337 | pfatal("lost+found IS NOT A DIRECTORY"); | |
338 | if (reply("REALLOCATE") == 0) | |
339 | return (0); | |
340 | oldlfdir = lfdir; | |
341 | if ((lfdir = allocdir(ROOTINO, 0)) == 0) { | |
342 | pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); | |
343 | return (0); | |
344 | } | |
345 | idesc.id_type = DATA; | |
346 | idesc.id_func = chgino; | |
347 | idesc.id_number = ROOTINO; | |
348 | idesc.id_parent = lfdir; /* new inumber for lost+found */ | |
349 | idesc.id_name = lfname; | |
350 | if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) { | |
351 | pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); | |
352 | return (0); | |
353 | } | |
354 | inodirty(); | |
355 | idesc.id_type = ADDR; | |
356 | idesc.id_func = pass4check; | |
357 | idesc.id_number = oldlfdir; | |
358 | adjust(&idesc, lncntp[oldlfdir] + 1); | |
359 | lncntp[oldlfdir] = 0; | |
360 | dp = ginode(lfdir); | |
361 | } | |
362 | if (statemap[lfdir] != DFOUND) { | |
363 | pfatal("SORRY. NO lost+found DIRECTORY\n\n"); | |
250baf55 KM |
364 | return (0); |
365 | } | |
250baf55 KM |
366 | len = strlen(lfname); |
367 | bcopy(lfname, pathp, len + 1); | |
368 | pathp += len; | |
ca1a30be KM |
369 | len = lftempname(tempname, orphan); |
370 | if (makeentry(lfdir, orphan, tempname) == 0) { | |
250baf55 KM |
371 | pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); |
372 | printf("\n\n"); | |
373 | return (0); | |
374 | } | |
375 | lncntp[orphan]--; | |
376 | *pathp++ = '/'; | |
2c4b8d40 | 377 | bcopy(tempname, pathp, len + 1); |
ebd05fde | 378 | pathp += len; |
250baf55 KM |
379 | if (lostdir) { |
380 | dp = ginode(orphan); | |
381 | idesc.id_type = DATA; | |
59e3daa2 | 382 | idesc.id_func = chgino; |
250baf55 | 383 | idesc.id_number = orphan; |
250baf55 | 384 | idesc.id_fix = DONTKNOW; |
59e3daa2 KM |
385 | idesc.id_name = ".."; |
386 | idesc.id_parent = lfdir; /* new value for ".." */ | |
250baf55 | 387 | (void)ckinode(dp, &idesc); |
39c18287 KM |
388 | dp = ginode(lfdir); |
389 | dp->di_nlink++; | |
390 | inodirty(); | |
391 | lncntp[lfdir]++; | |
250baf55 KM |
392 | pwarn("DIR I=%u CONNECTED. ", orphan); |
393 | printf("PARENT WAS I=%u\n", pdir); | |
394 | if (preen == 0) | |
395 | printf("\n"); | |
396 | } | |
397 | return (1); | |
398 | } | |
399 | ||
3e2a23ca KM |
400 | /* |
401 | * make an entry in a directory | |
402 | */ | |
ca1a30be KM |
403 | makeentry(parent, ino, name) |
404 | ino_t parent, ino; | |
405 | char *name; | |
3e2a23ca | 406 | { |
ca1a30be KM |
407 | DINODE *dp; |
408 | struct inodesc idesc; | |
3e2a23ca | 409 | |
ca1a30be KM |
410 | if (parent < ROOTINO || parent >= imax || ino < ROOTINO || ino >= imax) |
411 | return (0); | |
412 | bzero(&idesc, sizeof(struct inodesc)); | |
413 | idesc.id_type = DATA; | |
414 | idesc.id_func = mkentry; | |
415 | idesc.id_number = parent; | |
416 | idesc.id_parent = ino; /* this is the inode to enter */ | |
417 | idesc.id_fix = DONTKNOW; | |
418 | idesc.id_name = name; | |
419 | dp = ginode(parent); | |
042f816f KM |
420 | if (dp->di_size % DIRBLKSIZ) { |
421 | dp->di_size = roundup(dp->di_size, DIRBLKSIZ); | |
422 | inodirty(); | |
423 | } | |
ca1a30be | 424 | if ((ckinode(dp, &idesc) & ALTERED) != 0) |
3e2a23ca KM |
425 | return (1); |
426 | if (expanddir(dp) == 0) | |
427 | return (0); | |
ca1a30be | 428 | return (ckinode(dp, &idesc) & ALTERED); |
3e2a23ca KM |
429 | } |
430 | ||
431 | /* | |
432 | * Attempt to expand the size of a directory | |
433 | */ | |
434 | expanddir(dp) | |
435 | register DINODE *dp; | |
436 | { | |
437 | daddr_t lastbn, newblk; | |
438 | char *cp, firstblk[DIRBLKSIZ]; | |
439 | ||
440 | lastbn = lblkno(&sblock, dp->di_size); | |
441 | if (lastbn >= NDADDR - 1) | |
442 | return (0); | |
443 | if ((newblk = allocblk(sblock.fs_frag)) == 0) | |
444 | return (0); | |
445 | dp->di_db[lastbn + 1] = dp->di_db[lastbn]; | |
446 | dp->di_db[lastbn] = newblk; | |
447 | dp->di_size += sblock.fs_bsize; | |
448 | dp->di_blocks += btodb(sblock.fs_bsize); | |
f4f8f56a KM |
449 | getblk(&fileblk, dp->di_db[lastbn + 1], |
450 | dblksize(&sblock, dp, lastbn + 1)); | |
451 | if (fileblk.b_errs != NULL) | |
3e2a23ca KM |
452 | goto bad; |
453 | bcopy(dirblk.b_buf, firstblk, DIRBLKSIZ); | |
f4f8f56a KM |
454 | getblk(&fileblk, newblk, sblock.fs_bsize); |
455 | if (fileblk.b_errs != NULL) | |
3e2a23ca KM |
456 | goto bad; |
457 | bcopy(firstblk, dirblk.b_buf, DIRBLKSIZ); | |
458 | for (cp = &dirblk.b_buf[DIRBLKSIZ]; | |
459 | cp < &dirblk.b_buf[sblock.fs_bsize]; | |
460 | cp += DIRBLKSIZ) | |
461 | bcopy((char *)&emptydir, cp, sizeof emptydir); | |
462 | dirty(&fileblk); | |
f4f8f56a KM |
463 | getblk(&fileblk, dp->di_db[lastbn + 1], |
464 | dblksize(&sblock, dp, lastbn + 1)); | |
465 | if (fileblk.b_errs != NULL) | |
3e2a23ca KM |
466 | goto bad; |
467 | bcopy((char *)&emptydir, dirblk.b_buf, sizeof emptydir); | |
468 | pwarn("NO SPACE LEFT IN %s", pathname); | |
469 | if (preen) | |
470 | printf(" (EXPANDED)\n"); | |
471 | else if (reply("EXPAND") == 0) | |
472 | goto bad; | |
473 | dirty(&fileblk); | |
474 | inodirty(); | |
475 | return (1); | |
476 | bad: | |
477 | dp->di_db[lastbn] = dp->di_db[lastbn + 1]; | |
478 | dp->di_db[lastbn + 1] = 0; | |
479 | dp->di_size -= sblock.fs_bsize; | |
480 | dp->di_blocks -= btodb(sblock.fs_bsize); | |
481 | freeblk(newblk, sblock.fs_frag); | |
482 | return (0); | |
483 | } | |
484 | ||
ebd05fde KM |
485 | /* |
486 | * allocate a new directory | |
487 | */ | |
488 | allocdir(parent, request) | |
489 | ino_t parent, request; | |
490 | { | |
491 | ino_t ino; | |
492 | char *cp; | |
493 | DINODE *dp; | |
494 | ||
495 | ino = allocino(request, IFDIR|0755); | |
496 | dirhead.dot_ino = ino; | |
497 | dirhead.dotdot_ino = parent; | |
498 | dp = ginode(ino); | |
f4f8f56a KM |
499 | getblk(&fileblk, dp->di_db[0], sblock.fs_fsize); |
500 | if (fileblk.b_errs != NULL) { | |
ebd05fde KM |
501 | freeino(ino); |
502 | return (0); | |
503 | } | |
504 | bcopy((char *)&dirhead, dirblk.b_buf, sizeof dirhead); | |
505 | for (cp = &dirblk.b_buf[DIRBLKSIZ]; | |
506 | cp < &dirblk.b_buf[sblock.fs_fsize]; | |
507 | cp += DIRBLKSIZ) | |
508 | bcopy((char *)&emptydir, cp, sizeof emptydir); | |
509 | dirty(&fileblk); | |
510 | dp->di_nlink = 2; | |
511 | inodirty(); | |
512 | if (ino == ROOTINO) { | |
513 | lncntp[ino] = dp->di_nlink; | |
514 | return(ino); | |
515 | } | |
516 | if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { | |
517 | freeino(ino); | |
518 | return (0); | |
519 | } | |
520 | statemap[ino] = statemap[parent]; | |
521 | if (statemap[ino] == DSTATE) { | |
522 | lncntp[ino] = dp->di_nlink; | |
523 | lncntp[parent]++; | |
524 | } | |
525 | dp = ginode(parent); | |
526 | dp->di_nlink++; | |
527 | inodirty(); | |
528 | return (ino); | |
529 | } | |
530 | ||
531 | /* | |
532 | * free a directory inode | |
533 | */ | |
534 | freedir(ino, parent) | |
535 | ino_t ino, parent; | |
536 | { | |
537 | DINODE *dp; | |
538 | ||
539 | if (ino != parent) { | |
540 | dp = ginode(parent); | |
541 | dp->di_nlink--; | |
542 | inodirty(); | |
543 | } | |
544 | freeino(ino); | |
545 | } | |
546 | ||
250baf55 KM |
547 | /* |
548 | * generate a temporary name for the lost+found directory. | |
549 | */ | |
550 | lftempname(bufp, ino) | |
551 | char *bufp; | |
552 | ino_t ino; | |
553 | { | |
554 | register ino_t in; | |
555 | register char *cp; | |
556 | int namlen; | |
557 | ||
558 | cp = bufp + 2; | |
559 | for (in = imax; in > 0; in /= 10) | |
560 | cp++; | |
561 | *--cp = 0; | |
562 | namlen = cp - bufp; | |
563 | in = ino; | |
564 | while (cp > bufp) { | |
565 | *--cp = (in % 10) + '0'; | |
566 | in /= 10; | |
567 | } | |
568 | *cp = '#'; | |
569 | return (namlen); | |
570 | } |