xref: /openbsd-src/sbin/fsck_ext2fs/dir.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: dir.c,v 1.19 2014/07/13 16:08:53 pelikan Exp $	*/
2 /*	$NetBSD: dir.c,v 1.5 2000/01/28 16:01:46 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1980, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <ufs/ufs/dir.h>
37 #include <ufs/ext2fs/ext2fs_dinode.h>
38 #include <ufs/ext2fs/ext2fs_dir.h>
39 #include <ufs/ext2fs/ext2fs.h>
40 
41 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "fsck.h"
48 #include "fsutil.h"
49 #include "extern.h"
50 
51 char	*lfname = "lost+found";
52 int	lfmode = 01777;
53 struct	ext2fs_dirtemplate emptydir = { 0, DIRBLKSIZ };
54 struct	ext2fs_dirtemplate dirhead = {
55 	0, 12, 1, EXT2_FT_DIR, ".",
56 	0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
57 };
58 #undef DIRBLKSIZ
59 
60 static int expanddir(struct ext2fs_dinode *, char *);
61 static void freedir(ino_t, ino_t);
62 static struct ext2fs_direct *fsck_readdir(struct inodesc *);
63 static struct bufarea *getdirblk(daddr32_t, long);
64 static int lftempname(char *, ino_t);
65 static int mkentry(struct inodesc *);
66 static int chgino(struct  inodesc *);
67 
68 /*
69  * Propagate connected state through the tree.
70  */
71 void
72 propagate(void)
73 {
74 	struct inoinfo **inpp, *inp, *pinp;
75 	struct inoinfo **inpend;
76 
77 	/*
78 	 * Create a list of children for each directory.
79 	 */
80 	inpend = &inpsort[inplast];
81 	for (inpp = inpsort; inpp < inpend; inpp++) {
82 		inp = *inpp;
83 		if (inp->i_parent == 0 ||
84 		    inp->i_number == EXT2_ROOTINO)
85 			continue;
86 		pinp = getinoinfo(inp->i_parent);
87 		inp->i_parentp = pinp;
88 		inp->i_sibling = pinp->i_child;
89 		pinp->i_child = inp;
90 	}
91 	inp = getinoinfo(EXT2_ROOTINO);
92 	while (inp) {
93 		statemap[inp->i_number] = DFOUND;
94 		if (inp->i_child &&
95 		    statemap[inp->i_child->i_number] == DSTATE)
96 			inp = inp->i_child;
97 		else if (inp->i_sibling)
98 			inp = inp->i_sibling;
99 		else
100 			inp = inp->i_parentp;
101 	}
102 }
103 
104 /*
105  * Scan each entry in a directory block.
106  */
107 int
108 dirscan(struct inodesc *idesc)
109 {
110 	struct ext2fs_direct *dp;
111 	struct bufarea *bp;
112 	int dsize, n;
113 	long blksiz;
114 	char *dbuf = NULL;
115 
116 	if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL) {
117 		fprintf(stderr, "out of memory");
118 		exit(8);
119 	}
120 
121 	if (idesc->id_type != DATA)
122 		errexit("wrong type to dirscan %d\n", idesc->id_type);
123 	if (idesc->id_entryno == 0 &&
124 	    (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0)
125 		idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize);
126 	blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
127 	if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
128 		idesc->id_filesize -= blksiz;
129 		free(dbuf);
130 		return (SKIP);
131 	}
132 	idesc->id_loc = 0;
133 	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
134 		dsize = letoh16(dp->e2d_reclen);
135 		memcpy(dbuf, dp, (size_t)dsize);
136 		idesc->id_dirp = (struct ext2fs_direct *)dbuf;
137 		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
138 			bp = getdirblk(idesc->id_blkno, blksiz);
139 			memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
140 			    (size_t)dsize);
141 			dirty(bp);
142 			sbdirty();
143 		}
144 		if (n & STOP) {
145 			free(dbuf);
146 			return (n);
147 		}
148 	}
149 	free(dbuf);
150 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
151 }
152 
153 /*
154  * get next entry in a directory.
155  */
156 static struct ext2fs_direct *
157 fsck_readdir(struct inodesc *idesc)
158 {
159 	struct ext2fs_direct *dp, *ndp;
160 	struct bufarea *bp;
161 	long size, blksiz, fix, dploc;
162 
163 	blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
164 	bp = getdirblk(idesc->id_blkno, blksiz);
165 	if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 &&
166 	    idesc->id_loc < blksiz) {
167 		dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
168 		if (dircheck(idesc, dp))
169 			goto dpok;
170 		if (idesc->id_fix == IGNORE)
171 			return (0);
172 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
173 		bp = getdirblk(idesc->id_blkno, blksiz);
174 		dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
175 		dp->e2d_reclen = htole16(sblock.e2fs_bsize);
176 		dp->e2d_ino = 0;
177 		dp->e2d_namlen = 0;
178 		dp->e2d_type = 0;
179 		dp->e2d_name[0] = '\0';
180 		if (fix)
181 			dirty(bp);
182 		idesc->id_loc += sblock.e2fs_bsize;
183 		idesc->id_filesize -= sblock.e2fs_bsize;
184 		return (dp);
185 	}
186 dpok:
187 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
188 		return NULL;
189 	dploc = idesc->id_loc;
190 	dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
191 	idesc->id_loc += letoh16(dp->e2d_reclen);
192 	idesc->id_filesize -= letoh16(dp->e2d_reclen);
193 	if ((idesc->id_loc % sblock.e2fs_bsize) == 0)
194 		return (dp);
195 	ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
196 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
197 	    dircheck(idesc, ndp) == 0) {
198 		size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
199 		idesc->id_loc += size;
200 		idesc->id_filesize -= size;
201 		if (idesc->id_fix == IGNORE)
202 			return (0);
203 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
204 		bp = getdirblk(idesc->id_blkno, blksiz);
205 		dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
206 		dp->e2d_reclen = htole16(letoh16(dp->e2d_reclen) + size);
207 		if (fix)
208 			dirty(bp);
209 	}
210 	return (dp);
211 }
212 
213 /*
214  * Verify that a directory entry is valid.
215  * This is a superset of the checks made in the kernel.
216  */
217 int
218 dircheck(struct inodesc *idesc, struct ext2fs_direct *dp)
219 {
220 	int size;
221 	char *cp;
222 	int spaceleft;
223 	u_int16_t reclen = letoh16(dp->e2d_reclen);
224 
225 	spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
226 	if (letoh32(dp->e2d_ino) > maxino ||
227 	    reclen == 0 ||
228 	    reclen > spaceleft ||
229 	    (reclen & 0x3) != 0)
230 		return (0);
231 	if (dp->e2d_ino == 0)
232 		return (1);
233 	if (sblock.e2fs.e2fs_rev < E2FS_REV1 ||
234 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0)
235 		if (dp->e2d_type != 0)
236 			return (1);
237 	size = EXT2FS_DIRSIZ(dp->e2d_namlen);
238 	if (reclen < size ||
239 	    idesc->id_filesize < size ||
240 	    dp->e2d_namlen > EXT2FS_MAXNAMLEN)
241 		return (0);
242 	for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++)
243 		if (*cp == '\0' || (*cp++ == '/'))
244 			return (0);
245 	return (1);
246 }
247 
248 void
249 direrror(ino_t ino, char *errmesg)
250 {
251 
252 	fileerror(ino, ino, errmesg);
253 }
254 
255 void
256 fileerror(ino_t cwd, ino_t ino, char *errmesg)
257 {
258 	struct ext2fs_dinode *dp;
259 	char pathbuf[MAXPATHLEN + 1];
260 
261 	pwarn("%s ", errmesg);
262 	pinode(ino);
263 	printf("\n");
264 	getpathname(pathbuf, sizeof pathbuf, cwd, ino);
265 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) {
266 		pfatal("NAME=%s\n", pathbuf);
267 		return;
268 	}
269 	dp = ginode(ino);
270 	if (ftypeok(dp))
271 		pfatal("%s=%s\n",
272 		    (letoh16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
273 	else
274 		pfatal("NAME=%s\n", pathbuf);
275 }
276 
277 void
278 adjust(struct inodesc *idesc, short lcnt)
279 {
280 	struct ext2fs_dinode *dp;
281 
282 	dp = ginode(idesc->id_number);
283 	if (letoh16(dp->e2di_nlink) == lcnt) {
284 		if (linkup(idesc->id_number, (ino_t)0) == 0)
285 			clri(idesc, "UNREF", 0);
286 	} else {
287 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
288 			((letoh16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
289 		pinode(idesc->id_number);
290 		printf(" COUNT %d SHOULD BE %d",
291 			letoh16(dp->e2di_nlink), letoh16(dp->e2di_nlink) - lcnt);
292 		if (preen) {
293 			if (lcnt < 0) {
294 				printf("\n");
295 				pfatal("LINK COUNT INCREASING");
296 			}
297 			printf(" (ADJUSTED)\n");
298 		}
299 		if (preen || reply("ADJUST") == 1) {
300 			dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) - lcnt);
301 			inodirty();
302 		}
303 	}
304 }
305 
306 static int
307 mkentry(struct inodesc *idesc)
308 {
309 	struct ext2fs_direct *dirp = idesc->id_dirp;
310 	struct ext2fs_direct newent;
311 	int newlen, oldlen;
312 
313 	newent.e2d_type = EXT2_FT_UNKNOWN;
314 	newent.e2d_namlen = strlen(idesc->id_name);
315 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
316 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
317 		newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
318 	newlen = EXT2FS_DIRSIZ(newent.e2d_namlen);
319 	if (dirp->e2d_ino != 0)
320 		oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen);
321 	else
322 		oldlen = 0;
323 	if (letoh16(dirp->e2d_reclen) - oldlen < newlen)
324 		return (KEEPON);
325 	newent.e2d_reclen = htole16(letoh16(dirp->e2d_reclen) - oldlen);
326 	dirp->e2d_reclen = htole16(oldlen);
327 	dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen);
328 	dirp->e2d_ino = htole32(idesc->id_parent); /* ino to be entered is in id_parent */
329 	dirp->e2d_reclen = newent.e2d_reclen;
330 	dirp->e2d_namlen = newent.e2d_namlen;
331 	dirp->e2d_type = newent.e2d_type;
332 	memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen));
333 	return (ALTERED|STOP);
334 }
335 
336 static int
337 chgino(struct inodesc *idesc)
338 {
339 	struct ext2fs_direct *dirp = idesc->id_dirp;
340 	u_int16_t namlen = dirp->e2d_namlen;
341 
342 	if (strlen(idesc->id_name) != namlen ||
343 		strncmp(dirp->e2d_name, idesc->id_name, (int)namlen))
344 		return (KEEPON);
345 	dirp->e2d_ino = htole32(idesc->id_parent);
346 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
347 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
348 		dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
349 	else
350 		dirp->e2d_type = 0;
351 	return (ALTERED|STOP);
352 }
353 
354 int
355 linkup(ino_t orphan, ino_t parentdir)
356 {
357 	struct ext2fs_dinode *dp;
358 	int lostdir;
359 	ino_t oldlfdir;
360 	struct inodesc idesc;
361 	char tempname[BUFSIZ];
362 
363 	memset(&idesc, 0, sizeof(struct inodesc));
364 	dp = ginode(orphan);
365 	lostdir = (letoh16(dp->e2di_mode) & IFMT) == IFDIR;
366 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
367 	pinode(orphan);
368 	if (preen && inosize(dp) == 0)
369 		return (0);
370 	if (preen)
371 		printf(" (RECONNECTED)\n");
372 	else
373 		if (reply("RECONNECT") == 0)
374 			return (0);
375 	if (lfdir == 0) {
376 		dp = ginode(EXT2_ROOTINO);
377 		idesc.id_name = lfname;
378 		idesc.id_type = DATA;
379 		idesc.id_func = findino;
380 		idesc.id_number = EXT2_ROOTINO;
381 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
382 			lfdir = idesc.id_parent;
383 		} else {
384 			pwarn("NO lost+found DIRECTORY");
385 			if (preen || reply("CREATE")) {
386 				lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode);
387 				if (lfdir != 0) {
388 					if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) {
389 						if (preen)
390 							printf(" (CREATED)\n");
391 					} else {
392 						freedir(lfdir, EXT2_ROOTINO);
393 						lfdir = 0;
394 						if (preen)
395 							printf("\n");
396 					}
397 				}
398 			}
399 		}
400 		if (lfdir == 0) {
401 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
402 			printf("\n\n");
403 			return (0);
404 		}
405 	}
406 	dp = ginode(lfdir);
407 	if ((letoh16(dp->e2di_mode) & IFMT) != IFDIR) {
408 		pfatal("lost+found IS NOT A DIRECTORY");
409 		if (reply("REALLOCATE") == 0)
410 			return (0);
411 		oldlfdir = lfdir;
412 		if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) {
413 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
414 			return (0);
415 		}
416 		if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
417 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
418 			return (0);
419 		}
420 		inodirty();
421 		idesc.id_type = ADDR;
422 		idesc.id_func = pass4check;
423 		idesc.id_number = oldlfdir;
424 		adjust(&idesc, lncntp[oldlfdir] + 1);
425 		lncntp[oldlfdir] = 0;
426 		dp = ginode(lfdir);
427 	}
428 	if (statemap[lfdir] != DFOUND) {
429 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
430 		return (0);
431 	}
432 	(void)lftempname(tempname, orphan);
433 	if (makeentry(lfdir, orphan, tempname) == 0) {
434 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
435 		printf("\n\n");
436 		return (0);
437 	}
438 	lncntp[orphan]--;
439 	if (lostdir) {
440 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
441 		    parentdir != (ino_t)-1)
442 			(void)makeentry(orphan, lfdir, "..");
443 		dp = ginode(lfdir);
444 		dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) +1);
445 		inodirty();
446 		lncntp[lfdir]++;
447 		pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
448 		if (parentdir != (ino_t)-1)
449 			printf("PARENT WAS I=%llu\n",
450 			    (unsigned long long)parentdir);
451 		if (preen == 0)
452 			printf("\n");
453 	}
454 	return (1);
455 }
456 
457 /*
458  * fix an entry in a directory.
459  */
460 int
461 changeino(ino_t dir, char *name, ino_t newnum)
462 {
463 	struct inodesc idesc;
464 
465 	memset(&idesc, 0, sizeof(struct inodesc));
466 	idesc.id_type = DATA;
467 	idesc.id_func = chgino;
468 	idesc.id_number = dir;
469 	idesc.id_fix = DONTKNOW;
470 	idesc.id_name = name;
471 	idesc.id_parent = newnum;	/* new value for name */
472 	return (ckinode(ginode(dir), &idesc));
473 }
474 
475 /*
476  * make an entry in a directory
477  */
478 int
479 makeentry(ino_t parent, ino_t ino, char *name)
480 {
481 	struct ext2fs_dinode *dp;
482 	struct inodesc idesc;
483 	char pathbuf[MAXPATHLEN + 1];
484 
485 	if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO)
486 		|| parent >= maxino ||
487 	    (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino)
488 		return (0);
489 	memset(&idesc, 0, sizeof(struct inodesc));
490 	idesc.id_type = DATA;
491 	idesc.id_func = mkentry;
492 	idesc.id_number = parent;
493 	idesc.id_parent = ino;	/* this is the inode to enter */
494 	idesc.id_fix = DONTKNOW;
495 	idesc.id_name = name;
496 	dp = ginode(parent);
497 	if (inosize(dp) % sblock.e2fs_bsize) {
498 		inossize(dp, roundup(inosize(dp), sblock.e2fs_bsize));
499 		inodirty();
500 	}
501 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
502 		return (1);
503 	getpathname(pathbuf, sizeof pathbuf, parent, parent);
504 	dp = ginode(parent);
505 	if (expanddir(dp, pathbuf) == 0)
506 		return (0);
507 	return (ckinode(dp, &idesc) & ALTERED);
508 }
509 
510 /*
511  * Attempt to expand the size of a directory
512  */
513 static int
514 expanddir(struct ext2fs_dinode *dp, char *name)
515 {
516 	daddr32_t lastbn, newblk;
517 	struct bufarea *bp;
518 	char *firstblk;
519 
520 	lastbn = lblkno(&sblock, inosize(dp));
521 	if (lastbn >= NDADDR - 1 || letoh32(dp->e2di_blocks[lastbn]) == 0 ||
522 		inosize(dp) == 0)
523 		return (0);
524 	if ((newblk = allocblk()) == 0)
525 		return (0);
526 	dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn];
527 	dp->e2di_blocks[lastbn] = htole32(newblk);
528 	inossize(dp, inosize(dp) + sblock.e2fs_bsize);
529 	dp->e2di_nblock = htole32(letoh32(dp->e2di_nblock) + 1);
530 	bp = getdirblk(letoh32(dp->e2di_blocks[lastbn + 1]),
531 		sblock.e2fs_bsize);
532 	if (bp->b_errs)
533 		goto bad;
534 	if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL) {
535 		fprintf(stderr, "out of memory\n");
536 		exit(8);
537 	}
538 	memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize);
539 	bp = getdirblk(newblk, sblock.e2fs_bsize);
540 	if (bp->b_errs) {
541 		free(firstblk);
542 		goto bad;
543 	}
544 	memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize);
545 	free(firstblk);
546 	dirty(bp);
547 	bp = getdirblk(letoh32(dp->e2di_blocks[lastbn + 1]),
548 		sblock.e2fs_bsize);
549 	if (bp->b_errs)
550 		goto bad;
551 	emptydir.dot_reclen = htole16(sblock.e2fs_bsize);
552 	memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
553 	pwarn("NO SPACE LEFT IN %s", name);
554 	if (preen)
555 		printf(" (EXPANDED)\n");
556 	else if (reply("EXPAND") == 0)
557 		goto bad;
558 	dirty(bp);
559 	inodirty();
560 	return (1);
561 bad:
562 	dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1];
563 	dp->e2di_blocks[lastbn + 1] = 0;
564 	dp->e2di_size = htole32(letoh32(dp->e2di_size) - sblock.e2fs_bsize);
565 	inossize(dp, inosize(dp) - sblock.e2fs_bsize);
566 	dp->e2di_nblock = htole32(letoh32(dp->e2di_nblock) - 1);
567 	freeblk(newblk);
568 	return (0);
569 }
570 
571 /*
572  * allocate a new directory
573  */
574 int
575 allocdir(ino_t parent, ino_t request, int mode)
576 {
577 	ino_t ino;
578 	struct ext2fs_dinode *dp;
579 	struct bufarea *bp;
580 	struct ext2fs_dirtemplate *dirp;
581 
582 	ino = allocino(request, IFDIR|mode);
583 	dirhead.dot_reclen = htole16(12); /* XXX */
584 	dirhead.dotdot_reclen = htole16(sblock.e2fs_bsize - 12); /* XXX */
585 	dirhead.dot_namlen = 1;
586 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
587 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
588 		dirhead.dot_type = EXT2_FT_DIR;
589 	else
590 		dirhead.dot_type = 0;
591 	dirhead.dotdot_namlen = 2;
592 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
593 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
594 		dirhead.dotdot_type = EXT2_FT_DIR;
595 	else
596 		dirhead.dotdot_type = 0;
597 	dirp = &dirhead;
598 	dirp->dot_ino = htole32(ino);
599 	dirp->dotdot_ino = htole32(parent);
600 	dp = ginode(ino);
601 	bp = getdirblk(letoh32(dp->e2di_blocks[0]), sblock.e2fs_bsize);
602 	if (bp->b_errs) {
603 		freeino(ino);
604 		return (0);
605 	}
606 	memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate));
607 	dirty(bp);
608 	dp->e2di_nlink = htole16(2);
609 	inodirty();
610 	if (ino == EXT2_ROOTINO) {
611 		lncntp[ino] = letoh16(dp->e2di_nlink);
612 		cacheino(dp, ino);
613 		return(ino);
614 	}
615 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
616 		freeino(ino);
617 		return (0);
618 	}
619 	cacheino(dp, ino);
620 	statemap[ino] = statemap[parent];
621 	if (statemap[ino] == DSTATE) {
622 		lncntp[ino] = letoh16(dp->e2di_nlink);
623 		lncntp[parent]++;
624 	}
625 	dp = ginode(parent);
626 	dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) + 1);
627 	inodirty();
628 	return (ino);
629 }
630 
631 /*
632  * free a directory inode
633  */
634 static void
635 freedir(ino_t ino, ino_t parent)
636 {
637 	struct ext2fs_dinode *dp;
638 
639 	if (ino != parent) {
640 		dp = ginode(parent);
641 		dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) - 1);
642 		inodirty();
643 	}
644 	freeino(ino);
645 }
646 
647 /*
648  * generate a temporary name for the lost+found directory.
649  */
650 static int
651 lftempname(char *bufp, ino_t ino)
652 {
653 	ino_t in;
654 	char *cp;
655 	int namlen;
656 
657 	cp = bufp + 2;
658 	for (in = maxino; in > 0; in /= 10)
659 		cp++;
660 	*--cp = 0;
661 	namlen = cp - bufp;
662 	in = ino;
663 	while (cp > bufp) {
664 		*--cp = (in % 10) + '0';
665 		in /= 10;
666 	}
667 	*cp = '#';
668 	return (namlen);
669 }
670 
671 /*
672  * Get a directory block.
673  * Insure that it is held until another is requested.
674  */
675 static struct bufarea *
676 getdirblk(daddr32_t blkno, long size)
677 {
678 
679 	if (pdirbp != 0)
680 		pdirbp->b_flags &= ~B_INUSE;
681 	pdirbp = getdatablk(blkno, size);
682 	return (pdirbp);
683 }
684