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