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