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