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