Home | History | Annotate | Line # | Download | only in fsck_lfs
dir.c revision 1.21.8.1
      1  1.21.8.1      matt /* $NetBSD: dir.c,v 1.21.8.1 2007/11/06 23:12:34 matt 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.11       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1  perseant  *    may be used to endorse or promote products derived from this software
     17       1.1  perseant  *    without specific prior written permission.
     18       1.1  perseant  *
     19       1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1  perseant  * SUCH DAMAGE.
     30       1.1  perseant  */
     31       1.1  perseant 
     32       1.8  perseant #include <sys/types.h>
     33       1.1  perseant #include <sys/param.h>
     34       1.1  perseant #include <sys/time.h>
     35       1.8  perseant #include <sys/buf.h>
     36       1.8  perseant #include <sys/mount.h>
     37       1.8  perseant 
     38       1.8  perseant #include <ufs/ufs/inode.h>
     39       1.1  perseant #include <ufs/ufs/dir.h>
     40       1.8  perseant #include <ufs/ufs/ufsmount.h>
     41       1.1  perseant #include <ufs/lfs/lfs.h>
     42       1.1  perseant 
     43       1.8  perseant #include <err.h>
     44       1.1  perseant #include <stdio.h>
     45       1.1  perseant #include <stdlib.h>
     46       1.1  perseant #include <string.h>
     47       1.1  perseant 
     48       1.8  perseant #include "bufcache.h"
     49       1.8  perseant #include "vnode.h"
     50      1.19  christos #include "lfs_user.h"
     51       1.8  perseant 
     52       1.1  perseant #include "fsck.h"
     53       1.1  perseant #include "fsutil.h"
     54       1.1  perseant #include "extern.h"
     55       1.1  perseant 
     56      1.16  christos const char *lfname = "lost+found";
     57       1.8  perseant int lfmode = 01700;
     58      1.21  christos struct dirtemplate emptydir = {
     59      1.21  christos 	.dot_ino = 0,
     60      1.21  christos 	.dot_reclen = DIRBLKSIZ,
     61      1.21  christos };
     62       1.3  perseant struct dirtemplate dirhead = {
     63      1.21  christos 	.dot_ino = 0,
     64      1.21  christos 	.dot_reclen = 12,
     65      1.21  christos 	.dot_type = DT_DIR,
     66      1.21  christos 	.dot_namlen = 1,
     67      1.21  christos 	.dot_name = ".",
     68      1.21  christos 	.dotdot_ino = 0,
     69      1.21  christos 	.dotdot_reclen = DIRBLKSIZ - 12,
     70      1.21  christos 	.dotdot_type = DT_DIR,
     71      1.21  christos 	.dotdot_namlen = 2,
     72      1.21  christos 	.dotdot_name = ".."
     73       1.1  perseant };
     74       1.3  perseant struct odirtemplate odirhead = {
     75      1.21  christos 	.dot_ino = 0,
     76      1.21  christos 	.dot_reclen = 12,
     77      1.21  christos 	.dot_namlen = 1,
     78      1.21  christos 	.dot_name = ".",
     79      1.21  christos 	.dotdot_ino = 0,
     80      1.21  christos 	.dotdot_reclen = DIRBLKSIZ - 12,
     81      1.21  christos 	.dotdot_namlen = 2,
     82      1.21  christos 	.dotdot_name = ".."
     83       1.1  perseant };
     84       1.1  perseant 
     85       1.9      fvdl static int expanddir(struct uvnode *, struct ufs1_dinode *, char *);
     86       1.8  perseant static void freedir(ino_t, ino_t);
     87       1.8  perseant static struct direct *fsck_readdir(struct uvnode *, struct inodesc *);
     88       1.8  perseant static int lftempname(char *, ino_t);
     89       1.8  perseant static int mkentry(struct inodesc *);
     90       1.8  perseant static int chgino(struct inodesc *);
     91       1.1  perseant 
     92       1.1  perseant /*
     93       1.1  perseant  * Propagate connected state through the tree.
     94       1.1  perseant  */
     95       1.1  perseant void
     96      1.14   xtraeme propagate(void)
     97       1.1  perseant {
     98       1.8  perseant 	struct inoinfo **inpp, *inp, *pinp;
     99       1.1  perseant 	struct inoinfo **inpend;
    100       1.1  perseant 
    101       1.1  perseant 	/*
    102       1.1  perseant 	 * Create a list of children for each directory.
    103       1.1  perseant 	 */
    104       1.1  perseant 	inpend = &inpsort[inplast];
    105       1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    106       1.1  perseant 		inp = *inpp;
    107       1.1  perseant 		if (inp->i_parent == 0 ||
    108       1.1  perseant 		    inp->i_number == ROOTINO)
    109       1.1  perseant 			continue;
    110       1.1  perseant 		pinp = getinoinfo(inp->i_parent);
    111       1.1  perseant 		inp->i_parentp = pinp;
    112       1.1  perseant 		inp->i_sibling = pinp->i_child;
    113       1.1  perseant 		pinp->i_child = inp;
    114       1.1  perseant 	}
    115       1.1  perseant 	inp = getinoinfo(ROOTINO);
    116       1.1  perseant 	while (inp) {
    117       1.1  perseant 		statemap[inp->i_number] = DFOUND;
    118       1.1  perseant 		if (inp->i_child &&
    119       1.1  perseant 		    statemap[inp->i_child->i_number] == DSTATE)
    120       1.1  perseant 			inp = inp->i_child;
    121       1.1  perseant 		else if (inp->i_sibling)
    122       1.1  perseant 			inp = inp->i_sibling;
    123       1.1  perseant 		else
    124       1.1  perseant 			inp = inp->i_parentp;
    125       1.1  perseant 	}
    126       1.1  perseant }
    127       1.1  perseant 
    128       1.1  perseant /*
    129       1.1  perseant  * Scan each entry in a directory block.
    130       1.1  perseant  */
    131       1.1  perseant int
    132       1.8  perseant dirscan(struct inodesc *idesc)
    133       1.1  perseant {
    134       1.8  perseant 	struct direct *dp;
    135       1.8  perseant 	struct ubuf *bp;
    136       1.8  perseant 	int dsize, n;
    137       1.8  perseant 	long blksiz;
    138       1.8  perseant 	char dbuf[DIRBLKSIZ];
    139       1.8  perseant 	struct uvnode *vp;
    140       1.1  perseant 
    141       1.1  perseant 	if (idesc->id_type != DATA)
    142       1.1  perseant 		errexit("wrong type to dirscan %d\n", idesc->id_type);
    143       1.1  perseant 	if (idesc->id_entryno == 0 &&
    144       1.1  perseant 	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
    145       1.1  perseant 		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
    146       1.8  perseant 	blksiz = idesc->id_numfrags * fs->lfs_fsize;
    147       1.8  perseant 	if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags))) {
    148       1.1  perseant 		idesc->id_filesize -= blksiz;
    149       1.1  perseant 		return (SKIP);
    150       1.1  perseant 	}
    151       1.1  perseant 	idesc->id_loc = 0;
    152       1.8  perseant 
    153       1.8  perseant 	vp = vget(fs, idesc->id_number);
    154       1.8  perseant 	for (dp = fsck_readdir(vp, idesc); dp != NULL;
    155       1.8  perseant 	    dp = fsck_readdir(vp, idesc)) {
    156       1.1  perseant 		dsize = dp->d_reclen;
    157       1.8  perseant 		memcpy(dbuf, dp, (size_t) dsize);
    158       1.8  perseant 		idesc->id_dirp = (struct direct *) dbuf;
    159       1.8  perseant 		if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
    160       1.8  perseant 			bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    161       1.8  perseant 			memcpy(bp->b_data + idesc->id_loc - dsize, dbuf,
    162       1.8  perseant 			    (size_t) dsize);
    163       1.8  perseant 			VOP_BWRITE(bp);
    164       1.1  perseant 			sbdirty();
    165       1.1  perseant 		}
    166       1.3  perseant 		if (n & STOP)
    167       1.1  perseant 			return (n);
    168       1.1  perseant 	}
    169       1.1  perseant 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
    170       1.1  perseant }
    171       1.1  perseant 
    172       1.1  perseant /*
    173       1.1  perseant  * get next entry in a directory.
    174       1.1  perseant  */
    175       1.1  perseant static struct direct *
    176       1.8  perseant fsck_readdir(struct uvnode *vp, struct inodesc *idesc)
    177       1.1  perseant {
    178       1.8  perseant 	struct direct *dp, *ndp;
    179       1.8  perseant 	struct ubuf *bp;
    180       1.8  perseant 	long size, blksiz, fix, dploc;
    181       1.1  perseant 
    182       1.8  perseant 	blksiz = idesc->id_numfrags * fs->lfs_fsize;
    183       1.8  perseant 	bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    184       1.1  perseant 	if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
    185       1.1  perseant 	    idesc->id_loc < blksiz) {
    186       1.8  perseant 		dp = (struct direct *) (bp->b_data + idesc->id_loc);
    187       1.1  perseant 		if (dircheck(idesc, dp))
    188       1.1  perseant 			goto dpok;
    189  1.21.8.1      matt 		brelse(bp, 0);
    190       1.1  perseant 		if (idesc->id_fix == IGNORE)
    191       1.1  perseant 			return (0);
    192       1.1  perseant 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
    193      1.12      yamt 		bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    194       1.8  perseant 		dp = (struct direct *) (bp->b_data + idesc->id_loc);
    195       1.1  perseant 		dp->d_reclen = DIRBLKSIZ;
    196       1.1  perseant 		dp->d_ino = 0;
    197       1.1  perseant 		dp->d_type = 0;
    198       1.1  perseant 		dp->d_namlen = 0;
    199       1.1  perseant 		dp->d_name[0] = '\0';
    200       1.1  perseant 		if (fix)
    201       1.8  perseant 			VOP_BWRITE(bp);
    202       1.8  perseant 		else
    203  1.21.8.1      matt 			brelse(bp, 0);
    204       1.1  perseant 		idesc->id_loc += DIRBLKSIZ;
    205       1.1  perseant 		idesc->id_filesize -= DIRBLKSIZ;
    206       1.1  perseant 		return (dp);
    207       1.1  perseant 	}
    208       1.1  perseant dpok:
    209       1.8  perseant 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) {
    210  1.21.8.1      matt 		brelse(bp, 0);
    211       1.1  perseant 		return NULL;
    212       1.8  perseant 	}
    213       1.1  perseant 	dploc = idesc->id_loc;
    214       1.8  perseant 	dp = (struct direct *) (bp->b_data + dploc);
    215       1.1  perseant 	idesc->id_loc += dp->d_reclen;
    216       1.1  perseant 	idesc->id_filesize -= dp->d_reclen;
    217       1.8  perseant 	if ((idesc->id_loc % DIRBLKSIZ) == 0) {
    218  1.21.8.1      matt 		brelse(bp, 0);
    219       1.8  perseant 		return dp;
    220       1.8  perseant 	}
    221       1.8  perseant 	ndp = (struct direct *) (bp->b_data + idesc->id_loc);
    222       1.1  perseant 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
    223       1.1  perseant 	    dircheck(idesc, ndp) == 0) {
    224  1.21.8.1      matt 		brelse(bp, 0);
    225       1.1  perseant 		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
    226       1.1  perseant 		idesc->id_loc += size;
    227       1.1  perseant 		idesc->id_filesize -= size;
    228       1.1  perseant 		if (idesc->id_fix == IGNORE)
    229       1.8  perseant 			return 0;
    230       1.1  perseant 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
    231      1.12      yamt 		bread(vp, idesc->id_lblkno, blksiz, NOCRED, &bp);
    232       1.8  perseant 		dp = (struct direct *) (bp->b_data + dploc);
    233       1.1  perseant 		dp->d_reclen += size;
    234       1.1  perseant 		if (fix)
    235       1.8  perseant 			VOP_BWRITE(bp);
    236       1.8  perseant 		else
    237  1.21.8.1      matt 			brelse(bp, 0);
    238       1.8  perseant 	} else
    239  1.21.8.1      matt 		brelse(bp, 0);
    240       1.8  perseant 
    241       1.1  perseant 	return (dp);
    242       1.1  perseant }
    243       1.1  perseant 
    244       1.1  perseant /*
    245       1.1  perseant  * Verify that a directory entry is valid.
    246       1.1  perseant  * This is a superset of the checks made in the kernel.
    247       1.1  perseant  */
    248       1.1  perseant int
    249       1.8  perseant dircheck(struct inodesc *idesc, struct direct *dp)
    250       1.1  perseant {
    251       1.8  perseant 	int size;
    252       1.8  perseant 	char *cp;
    253       1.8  perseant 	u_char namlen, type;
    254       1.8  perseant 	int spaceleft;
    255       1.1  perseant 
    256       1.1  perseant 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
    257       1.1  perseant 	if (dp->d_ino >= maxino ||
    258       1.1  perseant 	    dp->d_reclen == 0 ||
    259       1.1  perseant 	    dp->d_reclen > spaceleft ||
    260       1.3  perseant 	    (dp->d_reclen & 0x3) != 0) {
    261       1.3  perseant 		pwarn("ino too large, reclen=0, reclen>space, or reclen&3!=0\n");
    262       1.3  perseant 		pwarn("dp->d_ino = 0x%x\tdp->d_reclen = 0x%x\n",
    263       1.8  perseant 		    dp->d_ino, dp->d_reclen);
    264      1.18  christos 		pwarn("maxino = %llu\tspaceleft = 0x%x\n",
    265      1.18  christos 		    (unsigned long long)maxino, spaceleft);
    266       1.3  perseant 		return (0);
    267       1.3  perseant 	}
    268       1.1  perseant 	if (dp->d_ino == 0)
    269       1.1  perseant 		return (1);
    270       1.8  perseant 	size = DIRSIZ(0, dp, 0);
    271       1.8  perseant 	namlen = dp->d_namlen;
    272       1.8  perseant 	type = dp->d_type;
    273       1.1  perseant 	if (dp->d_reclen < size ||
    274       1.1  perseant 	    idesc->id_filesize < size ||
    275       1.8  perseant 	/* namlen > MAXNAMLEN || */
    276       1.3  perseant 	    type > 15) {
    277       1.3  perseant 		printf("reclen<size, filesize<size, namlen too large, or type>15\n");
    278       1.1  perseant 		return (0);
    279       1.3  perseant 	}
    280       1.1  perseant 	for (cp = dp->d_name, size = 0; size < namlen; size++)
    281       1.1  perseant 		if (*cp == '\0' || (*cp++ == '/')) {
    282       1.3  perseant 			printf("name contains NUL or /\n");
    283       1.1  perseant 			return (0);
    284       1.3  perseant 		}
    285       1.1  perseant 	if (*cp != '\0') {
    286       1.3  perseant 		printf("name size misstated\n");
    287       1.1  perseant 		return (0);
    288       1.3  perseant 	}
    289       1.1  perseant 	return (1);
    290       1.1  perseant }
    291       1.1  perseant 
    292       1.1  perseant void
    293      1.17  christos direrror(ino_t ino, const char *errmesg)
    294       1.1  perseant {
    295       1.1  perseant 
    296       1.1  perseant 	fileerror(ino, ino, errmesg);
    297       1.1  perseant }
    298       1.1  perseant 
    299       1.1  perseant void
    300      1.17  christos fileerror(ino_t cwd, ino_t ino, const char *errmesg)
    301       1.1  perseant {
    302       1.8  perseant 	char pathbuf[MAXPATHLEN + 1];
    303       1.8  perseant 	struct uvnode *vp;
    304       1.1  perseant 
    305       1.1  perseant 	pwarn("%s ", errmesg);
    306       1.1  perseant 	pinode(ino);
    307       1.1  perseant 	printf("\n");
    308      1.20  perseant 	pwarn("PARENT=%lld\n", (long long)cwd);
    309      1.10    itojun 	getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
    310       1.4  perseant 	if (ino < ROOTINO || ino >= maxino) {
    311       1.1  perseant 		pfatal("NAME=%s\n", pathbuf);
    312       1.1  perseant 		return;
    313       1.1  perseant 	}
    314       1.8  perseant 	vp = vget(fs, ino);
    315       1.8  perseant 	if (vp == NULL)
    316       1.3  perseant 		pfatal("INO is NULL\n");
    317       1.3  perseant 	else {
    318       1.8  perseant 		if (ftypeok(VTOD(vp)))
    319       1.3  perseant 			pfatal("%s=%s\n",
    320      1.13  jdolecek 			    (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ?
    321       1.8  perseant 			    "DIR" : "FILE", pathbuf);
    322       1.3  perseant 		else
    323       1.3  perseant 			pfatal("NAME=%s\n", pathbuf);
    324       1.3  perseant 	}
    325       1.1  perseant }
    326       1.1  perseant 
    327       1.1  perseant void
    328       1.8  perseant adjust(struct inodesc *idesc, short lcnt)
    329       1.1  perseant {
    330       1.8  perseant 	struct uvnode *vp;
    331       1.9      fvdl 	struct ufs1_dinode *dp;
    332       1.1  perseant 
    333       1.8  perseant 	vp = vget(fs, idesc->id_number);
    334       1.8  perseant 	dp = VTOD(vp);
    335       1.1  perseant 	if (dp->di_nlink == lcnt) {
    336       1.8  perseant 		if (linkup(idesc->id_number, (ino_t) 0) == 0)
    337       1.1  perseant 			clri(idesc, "UNREF", 0);
    338       1.1  perseant 	} else {
    339       1.1  perseant 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
    340       1.8  perseant 		    ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
    341       1.1  perseant 		pinode(idesc->id_number);
    342       1.1  perseant 		printf(" COUNT %d SHOULD BE %d",
    343       1.8  perseant 		    dp->di_nlink, dp->di_nlink - lcnt);
    344       1.1  perseant 		if (preen) {
    345       1.1  perseant 			if (lcnt < 0) {
    346       1.1  perseant 				printf("\n");
    347       1.1  perseant 				pfatal("LINK COUNT INCREASING");
    348       1.1  perseant 			}
    349       1.1  perseant 			printf(" (ADJUSTED)\n");
    350       1.1  perseant 		}
    351       1.1  perseant 		if (preen || reply("ADJUST") == 1) {
    352       1.1  perseant 			dp->di_nlink -= lcnt;
    353       1.8  perseant 			inodirty(VTOI(vp));
    354       1.1  perseant 		}
    355       1.1  perseant 	}
    356       1.1  perseant }
    357       1.1  perseant 
    358       1.1  perseant static int
    359       1.8  perseant mkentry(struct inodesc *idesc)
    360       1.1  perseant {
    361       1.8  perseant 	struct direct *dirp = idesc->id_dirp;
    362       1.8  perseant 	struct direct newent;
    363       1.8  perseant 	int newlen, oldlen;
    364       1.1  perseant 
    365       1.1  perseant 	newent.d_namlen = strlen(idesc->id_name);
    366       1.1  perseant 	newlen = DIRSIZ(0, &newent, 0);
    367       1.1  perseant 	if (dirp->d_ino != 0)
    368       1.1  perseant 		oldlen = DIRSIZ(0, dirp, 0);
    369       1.1  perseant 	else
    370       1.1  perseant 		oldlen = 0;
    371       1.1  perseant 	if (dirp->d_reclen - oldlen < newlen)
    372       1.1  perseant 		return (KEEPON);
    373       1.1  perseant 	newent.d_reclen = dirp->d_reclen - oldlen;
    374       1.1  perseant 	dirp->d_reclen = oldlen;
    375       1.8  perseant 	dirp = (struct direct *) (((char *) dirp) + oldlen);
    376       1.1  perseant 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
    377       1.1  perseant 	dirp->d_reclen = newent.d_reclen;
    378       1.8  perseant 	dirp->d_type = typemap[idesc->id_parent];
    379       1.1  perseant 	dirp->d_namlen = newent.d_namlen;
    380       1.8  perseant 	memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
    381       1.3  perseant 	return (ALTERED | STOP);
    382       1.1  perseant }
    383       1.1  perseant 
    384       1.1  perseant static int
    385       1.8  perseant chgino(struct inodesc *idesc)
    386       1.1  perseant {
    387       1.8  perseant 	struct direct *dirp = idesc->id_dirp;
    388       1.1  perseant 
    389       1.8  perseant 	if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
    390       1.1  perseant 		return (KEEPON);
    391       1.1  perseant 	dirp->d_ino = idesc->id_parent;
    392       1.8  perseant 	dirp->d_type = typemap[idesc->id_parent];
    393       1.3  perseant 	return (ALTERED | STOP);
    394       1.1  perseant }
    395       1.1  perseant 
    396       1.1  perseant int
    397       1.3  perseant linkup(ino_t orphan, ino_t parentdir)
    398       1.1  perseant {
    399       1.9      fvdl 	struct ufs1_dinode *dp;
    400       1.8  perseant 	int lostdir;
    401       1.8  perseant 	ino_t oldlfdir;
    402       1.8  perseant 	struct inodesc idesc;
    403       1.8  perseant 	char tempname[BUFSIZ];
    404       1.8  perseant 	struct uvnode *vp;
    405       1.1  perseant 
    406       1.1  perseant 	memset(&idesc, 0, sizeof(struct inodesc));
    407       1.8  perseant 	vp = vget(fs, orphan);
    408       1.8  perseant 	dp = VTOD(vp);
    409       1.1  perseant 	lostdir = (dp->di_mode & IFMT) == IFDIR;
    410       1.1  perseant 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
    411       1.1  perseant 	pinode(orphan);
    412       1.1  perseant 	if (preen && dp->di_size == 0)
    413       1.1  perseant 		return (0);
    414       1.1  perseant 	if (preen)
    415       1.1  perseant 		printf(" (RECONNECTED)\n");
    416       1.3  perseant 	else if (reply("RECONNECT") == 0)
    417       1.3  perseant 		return (0);
    418       1.1  perseant 	if (lfdir == 0) {
    419       1.1  perseant 		dp = ginode(ROOTINO);
    420       1.1  perseant 		idesc.id_name = lfname;
    421       1.1  perseant 		idesc.id_type = DATA;
    422       1.1  perseant 		idesc.id_func = findino;
    423       1.1  perseant 		idesc.id_number = ROOTINO;
    424       1.1  perseant 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
    425       1.1  perseant 			lfdir = idesc.id_parent;
    426       1.1  perseant 		} else {
    427       1.1  perseant 			pwarn("NO lost+found DIRECTORY");
    428       1.1  perseant 			if (preen || reply("CREATE")) {
    429       1.8  perseant 				lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
    430       1.1  perseant 				if (lfdir != 0) {
    431       1.1  perseant 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
    432       1.1  perseant 						if (preen)
    433       1.1  perseant 							printf(" (CREATED)\n");
    434       1.1  perseant 					} else {
    435       1.1  perseant 						freedir(lfdir, ROOTINO);
    436       1.1  perseant 						lfdir = 0;
    437       1.1  perseant 						if (preen)
    438       1.1  perseant 							printf("\n");
    439       1.1  perseant 					}
    440       1.1  perseant 				}
    441       1.1  perseant 			}
    442       1.1  perseant 		}
    443       1.1  perseant 		if (lfdir == 0) {
    444       1.1  perseant 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
    445       1.1  perseant 			printf("\n\n");
    446       1.1  perseant 			return (0);
    447       1.1  perseant 		}
    448       1.1  perseant 	}
    449       1.8  perseant 	vp = vget(fs, lfdir);
    450       1.8  perseant 	dp = VTOD(vp);
    451       1.1  perseant 	if ((dp->di_mode & IFMT) != IFDIR) {
    452       1.1  perseant 		pfatal("lost+found IS NOT A DIRECTORY");
    453       1.1  perseant 		if (reply("REALLOCATE") == 0)
    454       1.1  perseant 			return (0);
    455       1.1  perseant 		oldlfdir = lfdir;
    456       1.8  perseant 		if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
    457       1.1  perseant 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    458       1.1  perseant 			return (0);
    459       1.1  perseant 		}
    460       1.1  perseant 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
    461       1.1  perseant 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
    462       1.1  perseant 			return (0);
    463       1.1  perseant 		}
    464       1.8  perseant 		inodirty(VTOI(vp));
    465       1.1  perseant 		idesc.id_type = ADDR;
    466       1.1  perseant 		idesc.id_func = pass4check;
    467       1.1  perseant 		idesc.id_number = oldlfdir;
    468       1.1  perseant 		adjust(&idesc, lncntp[oldlfdir] + 1);
    469       1.1  perseant 		lncntp[oldlfdir] = 0;
    470       1.8  perseant 		vp = vget(fs, lfdir);
    471       1.8  perseant 		dp = VTOD(vp);
    472       1.1  perseant 	}
    473       1.1  perseant 	if (statemap[lfdir] != DFOUND) {
    474       1.1  perseant 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
    475       1.1  perseant 		return (0);
    476       1.1  perseant 	}
    477       1.8  perseant 	(void) lftempname(tempname, orphan);
    478       1.1  perseant 	if (makeentry(lfdir, orphan, tempname) == 0) {
    479       1.1  perseant 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
    480       1.1  perseant 		printf("\n\n");
    481       1.1  perseant 		return (0);
    482       1.1  perseant 	}
    483       1.1  perseant 	lncntp[orphan]--;
    484       1.1  perseant 	if (lostdir) {
    485       1.1  perseant 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
    486       1.8  perseant 		    parentdir != (ino_t) - 1)
    487       1.8  perseant 			(void) makeentry(orphan, lfdir, "..");
    488       1.8  perseant 		vp = vget(fs, lfdir);
    489       1.9      fvdl 		VTOI(vp)->i_ffs1_nlink++;
    490       1.8  perseant 		inodirty(VTOI(vp));
    491       1.1  perseant 		lncntp[lfdir]++;
    492      1.18  christos 		pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
    493       1.8  perseant 		if (parentdir != (ino_t) - 1)
    494      1.18  christos 			printf("PARENT WAS I=%llu\n",
    495      1.18  christos 			    (unsigned long long)parentdir);
    496       1.1  perseant 		if (preen == 0)
    497       1.1  perseant 			printf("\n");
    498       1.1  perseant 	}
    499       1.1  perseant 	return (1);
    500       1.1  perseant }
    501       1.1  perseant 
    502       1.1  perseant /*
    503       1.1  perseant  * fix an entry in a directory.
    504       1.1  perseant  */
    505       1.1  perseant int
    506      1.16  christos changeino(ino_t dir, const char *name, ino_t newnum)
    507       1.1  perseant {
    508       1.8  perseant 	struct inodesc idesc;
    509       1.1  perseant 
    510       1.1  perseant 	memset(&idesc, 0, sizeof(struct inodesc));
    511       1.1  perseant 	idesc.id_type = DATA;
    512       1.1  perseant 	idesc.id_func = chgino;
    513       1.1  perseant 	idesc.id_number = dir;
    514       1.1  perseant 	idesc.id_fix = DONTKNOW;
    515       1.1  perseant 	idesc.id_name = name;
    516       1.1  perseant 	idesc.id_parent = newnum;	/* new value for name */
    517       1.8  perseant 
    518       1.1  perseant 	return (ckinode(ginode(dir), &idesc));
    519       1.1  perseant }
    520       1.1  perseant 
    521       1.1  perseant /*
    522       1.1  perseant  * make an entry in a directory
    523       1.1  perseant  */
    524       1.1  perseant int
    525      1.16  christos makeentry(ino_t parent, ino_t ino, const char *name)
    526       1.3  perseant {
    527       1.9      fvdl 	struct ufs1_dinode *dp;
    528       1.8  perseant 	struct inodesc idesc;
    529       1.8  perseant 	char pathbuf[MAXPATHLEN + 1];
    530       1.8  perseant 	struct uvnode *vp;
    531       1.3  perseant 
    532       1.1  perseant 	if (parent < ROOTINO || parent >= maxino ||
    533       1.1  perseant 	    ino < ROOTINO || ino >= maxino)
    534       1.1  perseant 		return (0);
    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 = mkentry;
    538       1.1  perseant 	idesc.id_number = parent;
    539       1.1  perseant 	idesc.id_parent = ino;	/* this is the inode to enter */
    540       1.1  perseant 	idesc.id_fix = DONTKNOW;
    541       1.1  perseant 	idesc.id_name = name;
    542       1.8  perseant 	vp = vget(fs, parent);
    543       1.8  perseant 	dp = VTOD(vp);
    544       1.1  perseant 	if (dp->di_size % DIRBLKSIZ) {
    545       1.1  perseant 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
    546       1.8  perseant 		inodirty(VTOI(vp));
    547       1.1  perseant 	}
    548       1.1  perseant 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
    549       1.1  perseant 		return (1);
    550      1.10    itojun 	getpathname(pathbuf, sizeof(pathbuf), parent, parent);
    551       1.8  perseant 	vp = vget(fs, parent);
    552       1.8  perseant 	dp = VTOD(vp);
    553       1.8  perseant 	if (expanddir(vp, dp, pathbuf) == 0)
    554       1.1  perseant 		return (0);
    555       1.1  perseant 	return (ckinode(dp, &idesc) & ALTERED);
    556       1.1  perseant }
    557       1.1  perseant 
    558       1.1  perseant /*
    559       1.1  perseant  * Attempt to expand the size of a directory
    560       1.1  perseant  */
    561       1.1  perseant static int
    562       1.9      fvdl expanddir(struct uvnode *vp, struct ufs1_dinode *dp, char *name)
    563       1.1  perseant {
    564      1.15  perseant 	daddr_t lastbn;
    565       1.8  perseant 	struct ubuf *bp;
    566       1.8  perseant 	char *cp, firstblk[DIRBLKSIZ];
    567       1.1  perseant 
    568       1.8  perseant 	lastbn = lblkno(fs, dp->di_size);
    569       1.1  perseant 	if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
    570       1.1  perseant 		return (0);
    571       1.1  perseant 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
    572       1.8  perseant 	dp->di_db[lastbn] = 0;
    573       1.8  perseant 	bp = getblk(vp, lastbn, fs->lfs_bsize);
    574       1.8  perseant 	VOP_BWRITE(bp);
    575       1.8  perseant 	dp->di_size += fs->lfs_bsize;
    576       1.8  perseant 	dp->di_blocks += btofsb(fs, fs->lfs_bsize);
    577       1.8  perseant 	bread(vp, dp->di_db[lastbn + 1],
    578       1.8  perseant 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    579       1.8  perseant 	if (bp->b_flags & B_ERROR)
    580       1.1  perseant 		goto bad;
    581       1.8  perseant 	memcpy(firstblk, bp->b_data, DIRBLKSIZ);
    582      1.15  perseant 	bread(vp, lastbn, fs->lfs_bsize, NOCRED, &bp);
    583       1.8  perseant 	if (bp->b_flags & B_ERROR)
    584       1.1  perseant 		goto bad;
    585       1.8  perseant 	memcpy(bp->b_data, firstblk, DIRBLKSIZ);
    586       1.8  perseant 	for (cp = &bp->b_data[DIRBLKSIZ];
    587       1.8  perseant 	    cp < &bp->b_data[fs->lfs_bsize];
    588       1.8  perseant 	    cp += DIRBLKSIZ)
    589       1.1  perseant 		memcpy(cp, &emptydir, sizeof emptydir);
    590       1.8  perseant 	VOP_BWRITE(bp);
    591       1.8  perseant 	bread(vp, dp->di_db[lastbn + 1],
    592       1.8  perseant 	    (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
    593       1.8  perseant 	if (bp->b_flags & B_ERROR)
    594       1.1  perseant 		goto bad;
    595       1.8  perseant 	memcpy(bp->b_data, &emptydir, sizeof emptydir);
    596       1.1  perseant 	pwarn("NO SPACE LEFT IN %s", name);
    597       1.1  perseant 	if (preen)
    598       1.1  perseant 		printf(" (EXPANDED)\n");
    599       1.1  perseant 	else if (reply("EXPAND") == 0)
    600       1.1  perseant 		goto bad;
    601       1.8  perseant 	VOP_BWRITE(bp);
    602       1.8  perseant 	inodirty(VTOI(vp));
    603       1.1  perseant 	return (1);
    604       1.1  perseant bad:
    605       1.1  perseant 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
    606       1.1  perseant 	dp->di_db[lastbn + 1] = 0;
    607       1.8  perseant 	dp->di_size -= fs->lfs_bsize;
    608       1.8  perseant 	dp->di_blocks -= btofsb(fs, fs->lfs_bsize);
    609       1.1  perseant 	return (0);
    610       1.1  perseant }
    611       1.1  perseant 
    612       1.1  perseant /*
    613       1.1  perseant  * allocate a new directory
    614       1.1  perseant  */
    615       1.1  perseant int
    616       1.3  perseant allocdir(ino_t parent, ino_t request, int mode)
    617       1.3  perseant {
    618       1.8  perseant 	ino_t ino;
    619       1.8  perseant 	char *cp;
    620       1.9      fvdl 	struct ufs1_dinode *dp;
    621       1.8  perseant 	struct ubuf *bp;
    622       1.1  perseant 	struct dirtemplate *dirp;
    623       1.8  perseant 	struct uvnode *vp;
    624       1.1  perseant 
    625       1.3  perseant 	ino = allocino(request, IFDIR | mode);
    626       1.8  perseant 	dirp = &dirhead;
    627       1.1  perseant 	dirp->dot_ino = ino;
    628       1.1  perseant 	dirp->dotdot_ino = parent;
    629       1.8  perseant 	vp = vget(fs, ino);
    630       1.8  perseant 	dp = VTOD(vp);
    631       1.8  perseant 	bread(vp, dp->di_db[0], fs->lfs_fsize, NOCRED, &bp);
    632       1.8  perseant 	if (bp->b_flags & B_ERROR) {
    633  1.21.8.1      matt 		brelse(bp, 0);
    634       1.1  perseant 		freeino(ino);
    635       1.1  perseant 		return (0);
    636       1.1  perseant 	}
    637       1.8  perseant 	memcpy(bp->b_data, dirp, sizeof(struct dirtemplate));
    638       1.8  perseant 	for (cp = &bp->b_data[DIRBLKSIZ];
    639       1.8  perseant 	    cp < &bp->b_data[fs->lfs_fsize];
    640       1.8  perseant 	    cp += DIRBLKSIZ)
    641       1.1  perseant 		memcpy(cp, &emptydir, sizeof emptydir);
    642       1.8  perseant 	VOP_BWRITE(bp);
    643       1.1  perseant 	dp->di_nlink = 2;
    644       1.8  perseant 	inodirty(VTOI(vp));
    645       1.1  perseant 	if (ino == ROOTINO) {
    646       1.1  perseant 		lncntp[ino] = dp->di_nlink;
    647       1.1  perseant 		cacheino(dp, ino);
    648       1.3  perseant 		return (ino);
    649       1.1  perseant 	}
    650       1.1  perseant 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
    651       1.1  perseant 		freeino(ino);
    652       1.1  perseant 		return (0);
    653       1.1  perseant 	}
    654       1.1  perseant 	cacheino(dp, ino);
    655       1.1  perseant 	statemap[ino] = statemap[parent];
    656       1.1  perseant 	if (statemap[ino] == DSTATE) {
    657       1.1  perseant 		lncntp[ino] = dp->di_nlink;
    658       1.1  perseant 		lncntp[parent]++;
    659       1.1  perseant 	}
    660       1.8  perseant 	vp = vget(fs, parent);
    661       1.8  perseant 	dp = VTOD(vp);
    662       1.1  perseant 	dp->di_nlink++;
    663       1.8  perseant 	inodirty(VTOI(vp));
    664       1.1  perseant 	return (ino);
    665       1.1  perseant }
    666       1.1  perseant 
    667       1.1  perseant /*
    668       1.1  perseant  * free a directory inode
    669       1.1  perseant  */
    670       1.1  perseant static void
    671       1.3  perseant freedir(ino_t ino, ino_t parent)
    672       1.1  perseant {
    673       1.8  perseant 	struct uvnode *vp;
    674       1.1  perseant 
    675       1.1  perseant 	if (ino != parent) {
    676       1.8  perseant 		vp = vget(fs, parent);
    677       1.9      fvdl 		VTOI(vp)->i_ffs1_nlink--;
    678       1.8  perseant 		inodirty(VTOI(vp));
    679       1.1  perseant 	}
    680       1.1  perseant 	freeino(ino);
    681       1.1  perseant }
    682       1.1  perseant 
    683       1.1  perseant /*
    684       1.1  perseant  * generate a temporary name for the lost+found directory.
    685       1.1  perseant  */
    686       1.1  perseant static int
    687       1.3  perseant lftempname(char *bufp, ino_t ino)
    688       1.3  perseant {
    689       1.8  perseant 	ino_t in;
    690       1.8  perseant 	char *cp;
    691       1.8  perseant 	int namlen;
    692       1.1  perseant 
    693       1.1  perseant 	cp = bufp + 2;
    694       1.1  perseant 	for (in = maxino; in > 0; in /= 10)
    695       1.1  perseant 		cp++;
    696       1.1  perseant 	*--cp = 0;
    697       1.1  perseant 	namlen = cp - bufp;
    698       1.1  perseant 	in = ino;
    699       1.1  perseant 	while (cp > bufp) {
    700       1.1  perseant 		*--cp = (in % 10) + '0';
    701       1.1  perseant 		in /= 10;
    702       1.1  perseant 	}
    703       1.1  perseant 	*cp = '#';
    704       1.1  perseant 	return (namlen);
    705       1.1  perseant }
    706