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