1*b9fc9a72Sderaadt /* $OpenBSD: dir.c,v 1.20 2015/01/16 06:39:57 deraadt Exp $ */
20190393fSart /* $NetBSD: dir.c,v 1.5 2000/01/28 16:01:46 bouyer Exp $ */
38c424e8eSdownsj
48c424e8eSdownsj /*
55ff4e0c8Sdownsj * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj * The Regents of the University of California. All rights reserved.
88c424e8eSdownsj *
98c424e8eSdownsj * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj * modification, are permitted provided that the following conditions
118c424e8eSdownsj * are met:
128c424e8eSdownsj * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj * notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj * notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj * documentation and/or other materials provided with the distribution.
171ef0d710Smillert * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj * may be used to endorse or promote products derived from this software
198c424e8eSdownsj * without specific prior written permission.
208c424e8eSdownsj *
218c424e8eSdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj * SUCH DAMAGE.
328c424e8eSdownsj */
338c424e8eSdownsj
34*b9fc9a72Sderaadt #include <sys/param.h> /* DEV_BSIZE roundup */
358c424e8eSdownsj #include <sys/time.h>
360190393fSart #include <ufs/ufs/dir.h>
378c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
388c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dir.h>
398c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
408c424e8eSdownsj
418c424e8eSdownsj #include <ufs/ufs/dinode.h> /* for IFMT & friends */
428c424e8eSdownsj
438c424e8eSdownsj #include <stdio.h>
448c424e8eSdownsj #include <stdlib.h>
458c424e8eSdownsj #include <string.h>
46*b9fc9a72Sderaadt #include <limits.h>
478c424e8eSdownsj
488c424e8eSdownsj #include "fsck.h"
498c424e8eSdownsj #include "fsutil.h"
508c424e8eSdownsj #include "extern.h"
518c424e8eSdownsj
528c424e8eSdownsj char *lfname = "lost+found";
538c424e8eSdownsj int lfmode = 01777;
548c424e8eSdownsj struct ext2fs_dirtemplate emptydir = { 0, DIRBLKSIZ };
558c424e8eSdownsj struct ext2fs_dirtemplate dirhead = {
5665348f21Sjasoni 0, 12, 1, EXT2_FT_DIR, ".",
5765348f21Sjasoni 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
588c424e8eSdownsj };
598c424e8eSdownsj #undef DIRBLKSIZ
608c424e8eSdownsj
61c72b5b24Smillert static int expanddir(struct ext2fs_dinode *, char *);
62c72b5b24Smillert static void freedir(ino_t, ino_t);
63c72b5b24Smillert static struct ext2fs_direct *fsck_readdir(struct inodesc *);
64b6d2e2d5Sderaadt static struct bufarea *getdirblk(daddr32_t, long);
65c72b5b24Smillert static int lftempname(char *, ino_t);
66c72b5b24Smillert static int mkentry(struct inodesc *);
67c72b5b24Smillert static int chgino(struct inodesc *);
688c424e8eSdownsj
698c424e8eSdownsj /*
708c424e8eSdownsj * Propagate connected state through the tree.
718c424e8eSdownsj */
728c424e8eSdownsj void
propagate(void)738809fabbSderaadt propagate(void)
748c424e8eSdownsj {
750190393fSart struct inoinfo **inpp, *inp, *pinp;
768c424e8eSdownsj struct inoinfo **inpend;
778c424e8eSdownsj
788c424e8eSdownsj /*
798c424e8eSdownsj * Create a list of children for each directory.
808c424e8eSdownsj */
818c424e8eSdownsj inpend = &inpsort[inplast];
828c424e8eSdownsj for (inpp = inpsort; inpp < inpend; inpp++) {
838c424e8eSdownsj inp = *inpp;
848c424e8eSdownsj if (inp->i_parent == 0 ||
858c424e8eSdownsj inp->i_number == EXT2_ROOTINO)
868c424e8eSdownsj continue;
878c424e8eSdownsj pinp = getinoinfo(inp->i_parent);
888c424e8eSdownsj inp->i_parentp = pinp;
898c424e8eSdownsj inp->i_sibling = pinp->i_child;
908c424e8eSdownsj pinp->i_child = inp;
918c424e8eSdownsj }
928c424e8eSdownsj inp = getinoinfo(EXT2_ROOTINO);
938c424e8eSdownsj while (inp) {
948c424e8eSdownsj statemap[inp->i_number] = DFOUND;
958c424e8eSdownsj if (inp->i_child &&
968c424e8eSdownsj statemap[inp->i_child->i_number] == DSTATE)
978c424e8eSdownsj inp = inp->i_child;
988c424e8eSdownsj else if (inp->i_sibling)
998c424e8eSdownsj inp = inp->i_sibling;
1008c424e8eSdownsj else
1018c424e8eSdownsj inp = inp->i_parentp;
1028c424e8eSdownsj }
1038c424e8eSdownsj }
1048c424e8eSdownsj
1058c424e8eSdownsj /*
1068c424e8eSdownsj * Scan each entry in a directory block.
1078c424e8eSdownsj */
1088c424e8eSdownsj int
dirscan(struct inodesc * idesc)1098809fabbSderaadt dirscan(struct inodesc *idesc)
1108c424e8eSdownsj {
1110190393fSart struct ext2fs_direct *dp;
1120190393fSart struct bufarea *bp;
1138c424e8eSdownsj int dsize, n;
1148c424e8eSdownsj long blksiz;
1158c424e8eSdownsj char *dbuf = NULL;
1168c424e8eSdownsj
1178c424e8eSdownsj if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL) {
1188c424e8eSdownsj fprintf(stderr, "out of memory");
1198c424e8eSdownsj exit(8);
1208c424e8eSdownsj }
1218c424e8eSdownsj
1228c424e8eSdownsj if (idesc->id_type != DATA)
1238c424e8eSdownsj errexit("wrong type to dirscan %d\n", idesc->id_type);
1248c424e8eSdownsj if (idesc->id_entryno == 0 &&
1258c424e8eSdownsj (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0)
1268c424e8eSdownsj idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize);
1278c424e8eSdownsj blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
1288c424e8eSdownsj if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
1298c424e8eSdownsj idesc->id_filesize -= blksiz;
1305e12154eSdhill free(dbuf);
1318c424e8eSdownsj return (SKIP);
1328c424e8eSdownsj }
1338c424e8eSdownsj idesc->id_loc = 0;
1348c424e8eSdownsj for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
13560a51e06Spelikan dsize = letoh16(dp->e2d_reclen);
1368c424e8eSdownsj memcpy(dbuf, dp, (size_t)dsize);
1378c424e8eSdownsj idesc->id_dirp = (struct ext2fs_direct *)dbuf;
1388c424e8eSdownsj if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
1398c424e8eSdownsj bp = getdirblk(idesc->id_blkno, blksiz);
1408c424e8eSdownsj memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
1418c424e8eSdownsj (size_t)dsize);
1428c424e8eSdownsj dirty(bp);
1438c424e8eSdownsj sbdirty();
1448c424e8eSdownsj }
1458c424e8eSdownsj if (n & STOP) {
1468c424e8eSdownsj free(dbuf);
1478c424e8eSdownsj return (n);
1488c424e8eSdownsj }
1498c424e8eSdownsj }
1508c424e8eSdownsj free(dbuf);
1518c424e8eSdownsj return (idesc->id_filesize > 0 ? KEEPON : STOP);
1528c424e8eSdownsj }
1538c424e8eSdownsj
1548c424e8eSdownsj /*
1558c424e8eSdownsj * get next entry in a directory.
1568c424e8eSdownsj */
1578c424e8eSdownsj static struct ext2fs_direct *
fsck_readdir(struct inodesc * idesc)1588809fabbSderaadt fsck_readdir(struct inodesc *idesc)
1598c424e8eSdownsj {
1600190393fSart struct ext2fs_direct *dp, *ndp;
1610190393fSart struct bufarea *bp;
1628c424e8eSdownsj long size, blksiz, fix, dploc;
1638c424e8eSdownsj
1648c424e8eSdownsj blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
1658c424e8eSdownsj bp = getdirblk(idesc->id_blkno, blksiz);
1668c424e8eSdownsj if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 &&
1678c424e8eSdownsj idesc->id_loc < blksiz) {
1688c424e8eSdownsj dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
1698c424e8eSdownsj if (dircheck(idesc, dp))
1708c424e8eSdownsj goto dpok;
1718c424e8eSdownsj if (idesc->id_fix == IGNORE)
1728c424e8eSdownsj return (0);
1738c424e8eSdownsj fix = dofix(idesc, "DIRECTORY CORRUPTED");
1748c424e8eSdownsj bp = getdirblk(idesc->id_blkno, blksiz);
1758c424e8eSdownsj dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
17660a51e06Spelikan dp->e2d_reclen = htole16(sblock.e2fs_bsize);
1778c424e8eSdownsj dp->e2d_ino = 0;
1788c424e8eSdownsj dp->e2d_namlen = 0;
17965348f21Sjasoni dp->e2d_type = 0;
1808c424e8eSdownsj dp->e2d_name[0] = '\0';
1818c424e8eSdownsj if (fix)
1828c424e8eSdownsj dirty(bp);
1838c424e8eSdownsj idesc->id_loc += sblock.e2fs_bsize;
1848c424e8eSdownsj idesc->id_filesize -= sblock.e2fs_bsize;
1858c424e8eSdownsj return (dp);
1868c424e8eSdownsj }
1878c424e8eSdownsj dpok:
1888c424e8eSdownsj if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
1898c424e8eSdownsj return NULL;
1908c424e8eSdownsj dploc = idesc->id_loc;
1918c424e8eSdownsj dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
19260a51e06Spelikan idesc->id_loc += letoh16(dp->e2d_reclen);
19360a51e06Spelikan idesc->id_filesize -= letoh16(dp->e2d_reclen);
1948c424e8eSdownsj if ((idesc->id_loc % sblock.e2fs_bsize) == 0)
1958c424e8eSdownsj return (dp);
1968c424e8eSdownsj ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
1978c424e8eSdownsj if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
1988c424e8eSdownsj dircheck(idesc, ndp) == 0) {
1998c424e8eSdownsj size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
2008c424e8eSdownsj idesc->id_loc += size;
2018c424e8eSdownsj idesc->id_filesize -= size;
2028c424e8eSdownsj if (idesc->id_fix == IGNORE)
2038c424e8eSdownsj return (0);
2048c424e8eSdownsj fix = dofix(idesc, "DIRECTORY CORRUPTED");
2058c424e8eSdownsj bp = getdirblk(idesc->id_blkno, blksiz);
2068c424e8eSdownsj dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
20760a51e06Spelikan dp->e2d_reclen = htole16(letoh16(dp->e2d_reclen) + size);
2088c424e8eSdownsj if (fix)
2098c424e8eSdownsj dirty(bp);
2108c424e8eSdownsj }
2118c424e8eSdownsj return (dp);
2128c424e8eSdownsj }
2138c424e8eSdownsj
2148c424e8eSdownsj /*
2158c424e8eSdownsj * Verify that a directory entry is valid.
2168c424e8eSdownsj * This is a superset of the checks made in the kernel.
2178c424e8eSdownsj */
2188c424e8eSdownsj int
dircheck(struct inodesc * idesc,struct ext2fs_direct * dp)2198809fabbSderaadt dircheck(struct inodesc *idesc, struct ext2fs_direct *dp)
2208c424e8eSdownsj {
2218c424e8eSdownsj int size;
2228c424e8eSdownsj char *cp;
2238c424e8eSdownsj int spaceleft;
22460a51e06Spelikan u_int16_t reclen = letoh16(dp->e2d_reclen);
2258c424e8eSdownsj
2268c424e8eSdownsj spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
22760a51e06Spelikan if (letoh32(dp->e2d_ino) > maxino ||
22865348f21Sjasoni reclen == 0 ||
22965348f21Sjasoni reclen > spaceleft ||
23065348f21Sjasoni (reclen & 0x3) != 0)
2318c424e8eSdownsj return (0);
2328c424e8eSdownsj if (dp->e2d_ino == 0)
2338c424e8eSdownsj return (1);
23465348f21Sjasoni if (sblock.e2fs.e2fs_rev < E2FS_REV1 ||
23565348f21Sjasoni (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0)
23665348f21Sjasoni if (dp->e2d_type != 0)
23765348f21Sjasoni return (1);
2388c424e8eSdownsj size = EXT2FS_DIRSIZ(dp->e2d_namlen);
23965348f21Sjasoni if (reclen < size ||
2408c424e8eSdownsj idesc->id_filesize < size ||
24165348f21Sjasoni dp->e2d_namlen > EXT2FS_MAXNAMLEN)
2428c424e8eSdownsj return (0);
24365348f21Sjasoni for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++)
2448c424e8eSdownsj if (*cp == '\0' || (*cp++ == '/'))
2458c424e8eSdownsj return (0);
2468c424e8eSdownsj return (1);
2478c424e8eSdownsj }
2488c424e8eSdownsj
2498c424e8eSdownsj void
direrror(ino_t ino,char * errmesg)2508809fabbSderaadt direrror(ino_t ino, char *errmesg)
2518c424e8eSdownsj {
2528c424e8eSdownsj
2538c424e8eSdownsj fileerror(ino, ino, errmesg);
2548c424e8eSdownsj }
2558c424e8eSdownsj
2568c424e8eSdownsj void
fileerror(ino_t cwd,ino_t ino,char * errmesg)2578809fabbSderaadt fileerror(ino_t cwd, ino_t ino, char *errmesg)
2588c424e8eSdownsj {
2590190393fSart struct ext2fs_dinode *dp;
260*b9fc9a72Sderaadt char pathbuf[PATH_MAX + 1];
2618c424e8eSdownsj
2628c424e8eSdownsj pwarn("%s ", errmesg);
2638c424e8eSdownsj pinode(ino);
2648c424e8eSdownsj printf("\n");
265487a1948Stedu getpathname(pathbuf, sizeof pathbuf, cwd, ino);
2668c424e8eSdownsj if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) {
2678c424e8eSdownsj pfatal("NAME=%s\n", pathbuf);
2688c424e8eSdownsj return;
2698c424e8eSdownsj }
2708c424e8eSdownsj dp = ginode(ino);
2718c424e8eSdownsj if (ftypeok(dp))
2728c424e8eSdownsj pfatal("%s=%s\n",
27360a51e06Spelikan (letoh16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
2748c424e8eSdownsj else
2758c424e8eSdownsj pfatal("NAME=%s\n", pathbuf);
2768c424e8eSdownsj }
2778c424e8eSdownsj
2788c424e8eSdownsj void
adjust(struct inodesc * idesc,short lcnt)2798809fabbSderaadt adjust(struct inodesc *idesc, short lcnt)
2808c424e8eSdownsj {
2810190393fSart struct ext2fs_dinode *dp;
2828c424e8eSdownsj
2838c424e8eSdownsj dp = ginode(idesc->id_number);
28460a51e06Spelikan if (letoh16(dp->e2di_nlink) == lcnt) {
2858c424e8eSdownsj if (linkup(idesc->id_number, (ino_t)0) == 0)
2868c424e8eSdownsj clri(idesc, "UNREF", 0);
2878c424e8eSdownsj } else {
2888c424e8eSdownsj pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
28960a51e06Spelikan ((letoh16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
2908c424e8eSdownsj pinode(idesc->id_number);
2918c424e8eSdownsj printf(" COUNT %d SHOULD BE %d",
29260a51e06Spelikan letoh16(dp->e2di_nlink), letoh16(dp->e2di_nlink) - lcnt);
2938c424e8eSdownsj if (preen) {
2948c424e8eSdownsj if (lcnt < 0) {
2958c424e8eSdownsj printf("\n");
2968c424e8eSdownsj pfatal("LINK COUNT INCREASING");
2978c424e8eSdownsj }
2988c424e8eSdownsj printf(" (ADJUSTED)\n");
2998c424e8eSdownsj }
3008c424e8eSdownsj if (preen || reply("ADJUST") == 1) {
30160a51e06Spelikan dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) - lcnt);
3028c424e8eSdownsj inodirty();
3038c424e8eSdownsj }
3048c424e8eSdownsj }
3058c424e8eSdownsj }
3068c424e8eSdownsj
3078c424e8eSdownsj static int
mkentry(struct inodesc * idesc)3088809fabbSderaadt mkentry(struct inodesc *idesc)
3098c424e8eSdownsj {
3100190393fSart struct ext2fs_direct *dirp = idesc->id_dirp;
3118c424e8eSdownsj struct ext2fs_direct newent;
3128c424e8eSdownsj int newlen, oldlen;
3138c424e8eSdownsj
314a2fb4f80Skrw newent.e2d_type = EXT2_FT_UNKNOWN;
3158c424e8eSdownsj newent.e2d_namlen = strlen(idesc->id_name);
31665348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
31765348f21Sjasoni (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
31865348f21Sjasoni newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
3198c424e8eSdownsj newlen = EXT2FS_DIRSIZ(newent.e2d_namlen);
3208c424e8eSdownsj if (dirp->e2d_ino != 0)
3218c424e8eSdownsj oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen);
3228c424e8eSdownsj else
3238c424e8eSdownsj oldlen = 0;
32460a51e06Spelikan if (letoh16(dirp->e2d_reclen) - oldlen < newlen)
3258c424e8eSdownsj return (KEEPON);
32660a51e06Spelikan newent.e2d_reclen = htole16(letoh16(dirp->e2d_reclen) - oldlen);
32760a51e06Spelikan dirp->e2d_reclen = htole16(oldlen);
3288c424e8eSdownsj dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen);
32960a51e06Spelikan dirp->e2d_ino = htole32(idesc->id_parent); /* ino to be entered is in id_parent */
3308c424e8eSdownsj dirp->e2d_reclen = newent.e2d_reclen;
3318c424e8eSdownsj dirp->e2d_namlen = newent.e2d_namlen;
33265348f21Sjasoni dirp->e2d_type = newent.e2d_type;
33365348f21Sjasoni memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen));
3348c424e8eSdownsj return (ALTERED|STOP);
3358c424e8eSdownsj }
3368c424e8eSdownsj
3378c424e8eSdownsj static int
chgino(struct inodesc * idesc)3388809fabbSderaadt chgino(struct inodesc *idesc)
3398c424e8eSdownsj {
3400190393fSart struct ext2fs_direct *dirp = idesc->id_dirp;
34165348f21Sjasoni u_int16_t namlen = dirp->e2d_namlen;
3428c424e8eSdownsj
34365348f21Sjasoni if (strlen(idesc->id_name) != namlen ||
34465348f21Sjasoni strncmp(dirp->e2d_name, idesc->id_name, (int)namlen))
3458c424e8eSdownsj return (KEEPON);
34660a51e06Spelikan dirp->e2d_ino = htole32(idesc->id_parent);
34765348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
34865348f21Sjasoni (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
34965348f21Sjasoni dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
35065348f21Sjasoni else
35165348f21Sjasoni dirp->e2d_type = 0;
3528c424e8eSdownsj return (ALTERED|STOP);
3538c424e8eSdownsj }
3548c424e8eSdownsj
3558c424e8eSdownsj int
linkup(ino_t orphan,ino_t parentdir)3568809fabbSderaadt linkup(ino_t orphan, ino_t parentdir)
3578c424e8eSdownsj {
3580190393fSart struct ext2fs_dinode *dp;
3598c424e8eSdownsj int lostdir;
3608c424e8eSdownsj ino_t oldlfdir;
3618c424e8eSdownsj struct inodesc idesc;
3628c424e8eSdownsj char tempname[BUFSIZ];
3638c424e8eSdownsj
3648c424e8eSdownsj memset(&idesc, 0, sizeof(struct inodesc));
3658c424e8eSdownsj dp = ginode(orphan);
36660a51e06Spelikan lostdir = (letoh16(dp->e2di_mode) & IFMT) == IFDIR;
3678c424e8eSdownsj pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
3688c424e8eSdownsj pinode(orphan);
369935730fbSniallo if (preen && inosize(dp) == 0)
3708c424e8eSdownsj return (0);
3718c424e8eSdownsj if (preen)
3728c424e8eSdownsj printf(" (RECONNECTED)\n");
3738c424e8eSdownsj else
3748c424e8eSdownsj if (reply("RECONNECT") == 0)
3758c424e8eSdownsj return (0);
3768c424e8eSdownsj if (lfdir == 0) {
3778c424e8eSdownsj dp = ginode(EXT2_ROOTINO);
3788c424e8eSdownsj idesc.id_name = lfname;
3798c424e8eSdownsj idesc.id_type = DATA;
3808c424e8eSdownsj idesc.id_func = findino;
3818c424e8eSdownsj idesc.id_number = EXT2_ROOTINO;
3828c424e8eSdownsj if ((ckinode(dp, &idesc) & FOUND) != 0) {
3838c424e8eSdownsj lfdir = idesc.id_parent;
3848c424e8eSdownsj } else {
3858c424e8eSdownsj pwarn("NO lost+found DIRECTORY");
3868c424e8eSdownsj if (preen || reply("CREATE")) {
3878c424e8eSdownsj lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode);
3888c424e8eSdownsj if (lfdir != 0) {
3898c424e8eSdownsj if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) {
3908c424e8eSdownsj if (preen)
3918c424e8eSdownsj printf(" (CREATED)\n");
3928c424e8eSdownsj } else {
3938c424e8eSdownsj freedir(lfdir, EXT2_ROOTINO);
3948c424e8eSdownsj lfdir = 0;
3958c424e8eSdownsj if (preen)
3968c424e8eSdownsj printf("\n");
3978c424e8eSdownsj }
3988c424e8eSdownsj }
3998c424e8eSdownsj }
4008c424e8eSdownsj }
4018c424e8eSdownsj if (lfdir == 0) {
4028c424e8eSdownsj pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
4038c424e8eSdownsj printf("\n\n");
4048c424e8eSdownsj return (0);
4058c424e8eSdownsj }
4068c424e8eSdownsj }
4078c424e8eSdownsj dp = ginode(lfdir);
40860a51e06Spelikan if ((letoh16(dp->e2di_mode) & IFMT) != IFDIR) {
4098c424e8eSdownsj pfatal("lost+found IS NOT A DIRECTORY");
4108c424e8eSdownsj if (reply("REALLOCATE") == 0)
4118c424e8eSdownsj return (0);
4128c424e8eSdownsj oldlfdir = lfdir;
4138c424e8eSdownsj if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) {
4148c424e8eSdownsj pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4158c424e8eSdownsj return (0);
4168c424e8eSdownsj }
4178c424e8eSdownsj if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
4188c424e8eSdownsj pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4198c424e8eSdownsj return (0);
4208c424e8eSdownsj }
4218c424e8eSdownsj inodirty();
4228c424e8eSdownsj idesc.id_type = ADDR;
4238c424e8eSdownsj idesc.id_func = pass4check;
4248c424e8eSdownsj idesc.id_number = oldlfdir;
4258c424e8eSdownsj adjust(&idesc, lncntp[oldlfdir] + 1);
4268c424e8eSdownsj lncntp[oldlfdir] = 0;
4278c424e8eSdownsj dp = ginode(lfdir);
4288c424e8eSdownsj }
4298c424e8eSdownsj if (statemap[lfdir] != DFOUND) {
4308c424e8eSdownsj pfatal("SORRY. NO lost+found DIRECTORY\n\n");
4318c424e8eSdownsj return (0);
4328c424e8eSdownsj }
4338c424e8eSdownsj (void)lftempname(tempname, orphan);
4348c424e8eSdownsj if (makeentry(lfdir, orphan, tempname) == 0) {
4358c424e8eSdownsj pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
4368c424e8eSdownsj printf("\n\n");
4378c424e8eSdownsj return (0);
4388c424e8eSdownsj }
4398c424e8eSdownsj lncntp[orphan]--;
4408c424e8eSdownsj if (lostdir) {
4418c424e8eSdownsj if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
4428c424e8eSdownsj parentdir != (ino_t)-1)
4438c424e8eSdownsj (void)makeentry(orphan, lfdir, "..");
4448c424e8eSdownsj dp = ginode(lfdir);
44560a51e06Spelikan dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) +1);
4468c424e8eSdownsj inodirty();
4478c424e8eSdownsj lncntp[lfdir]++;
4483b92bd08Sderaadt pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
4498c424e8eSdownsj if (parentdir != (ino_t)-1)
4503b92bd08Sderaadt printf("PARENT WAS I=%llu\n",
4513b92bd08Sderaadt (unsigned long long)parentdir);
4528c424e8eSdownsj if (preen == 0)
4538c424e8eSdownsj printf("\n");
4548c424e8eSdownsj }
4558c424e8eSdownsj return (1);
4568c424e8eSdownsj }
4578c424e8eSdownsj
4588c424e8eSdownsj /*
4598c424e8eSdownsj * fix an entry in a directory.
4608c424e8eSdownsj */
4618c424e8eSdownsj int
changeino(ino_t dir,char * name,ino_t newnum)4628809fabbSderaadt changeino(ino_t dir, char *name, ino_t newnum)
4638c424e8eSdownsj {
4648c424e8eSdownsj struct inodesc idesc;
4658c424e8eSdownsj
4668c424e8eSdownsj memset(&idesc, 0, sizeof(struct inodesc));
4678c424e8eSdownsj idesc.id_type = DATA;
4688c424e8eSdownsj idesc.id_func = chgino;
4698c424e8eSdownsj idesc.id_number = dir;
4708c424e8eSdownsj idesc.id_fix = DONTKNOW;
4718c424e8eSdownsj idesc.id_name = name;
4728c424e8eSdownsj idesc.id_parent = newnum; /* new value for name */
4738c424e8eSdownsj return (ckinode(ginode(dir), &idesc));
4748c424e8eSdownsj }
4758c424e8eSdownsj
4768c424e8eSdownsj /*
4778c424e8eSdownsj * make an entry in a directory
4788c424e8eSdownsj */
4798c424e8eSdownsj int
makeentry(ino_t parent,ino_t ino,char * name)4808809fabbSderaadt makeentry(ino_t parent, ino_t ino, char *name)
4818c424e8eSdownsj {
4828c424e8eSdownsj struct ext2fs_dinode *dp;
4838c424e8eSdownsj struct inodesc idesc;
484*b9fc9a72Sderaadt char pathbuf[PATH_MAX + 1];
4858c424e8eSdownsj
4868c424e8eSdownsj if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO)
4878c424e8eSdownsj || parent >= maxino ||
4888c424e8eSdownsj (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino)
4898c424e8eSdownsj return (0);
4908c424e8eSdownsj memset(&idesc, 0, sizeof(struct inodesc));
4918c424e8eSdownsj idesc.id_type = DATA;
4928c424e8eSdownsj idesc.id_func = mkentry;
4938c424e8eSdownsj idesc.id_number = parent;
4948c424e8eSdownsj idesc.id_parent = ino; /* this is the inode to enter */
4958c424e8eSdownsj idesc.id_fix = DONTKNOW;
4968c424e8eSdownsj idesc.id_name = name;
4978c424e8eSdownsj dp = ginode(parent);
498935730fbSniallo if (inosize(dp) % sblock.e2fs_bsize) {
499935730fbSniallo inossize(dp, roundup(inosize(dp), sblock.e2fs_bsize));
5008c424e8eSdownsj inodirty();
5018c424e8eSdownsj }
5028c424e8eSdownsj if ((ckinode(dp, &idesc) & ALTERED) != 0)
5038c424e8eSdownsj return (1);
504487a1948Stedu getpathname(pathbuf, sizeof pathbuf, parent, parent);
5058c424e8eSdownsj dp = ginode(parent);
5068c424e8eSdownsj if (expanddir(dp, pathbuf) == 0)
5078c424e8eSdownsj return (0);
5088c424e8eSdownsj return (ckinode(dp, &idesc) & ALTERED);
5098c424e8eSdownsj }
5108c424e8eSdownsj
5118c424e8eSdownsj /*
5128c424e8eSdownsj * Attempt to expand the size of a directory
5138c424e8eSdownsj */
5148c424e8eSdownsj static int
expanddir(struct ext2fs_dinode * dp,char * name)5158809fabbSderaadt expanddir(struct ext2fs_dinode *dp, char *name)
5168c424e8eSdownsj {
517b6d2e2d5Sderaadt daddr32_t lastbn, newblk;
5180190393fSart struct bufarea *bp;
519a4df0321Sderaadt char *firstblk;
5208c424e8eSdownsj
521935730fbSniallo lastbn = lblkno(&sblock, inosize(dp));
52260a51e06Spelikan if (lastbn >= NDADDR - 1 || letoh32(dp->e2di_blocks[lastbn]) == 0 ||
523935730fbSniallo inosize(dp) == 0)
5248c424e8eSdownsj return (0);
5258c424e8eSdownsj if ((newblk = allocblk()) == 0)
5268c424e8eSdownsj return (0);
5278c424e8eSdownsj dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn];
52860a51e06Spelikan dp->e2di_blocks[lastbn] = htole32(newblk);
529935730fbSniallo inossize(dp, inosize(dp) + sblock.e2fs_bsize);
53060a51e06Spelikan dp->e2di_nblock = htole32(letoh32(dp->e2di_nblock) + 1);
53160a51e06Spelikan bp = getdirblk(letoh32(dp->e2di_blocks[lastbn + 1]),
5328c424e8eSdownsj sblock.e2fs_bsize);
5338c424e8eSdownsj if (bp->b_errs)
5348c424e8eSdownsj goto bad;
5355e12154eSdhill if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL) {
5365e12154eSdhill fprintf(stderr, "out of memory\n");
5375e12154eSdhill exit(8);
5385e12154eSdhill }
5398c424e8eSdownsj memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize);
5408c424e8eSdownsj bp = getdirblk(newblk, sblock.e2fs_bsize);
5415e12154eSdhill if (bp->b_errs) {
5425e12154eSdhill free(firstblk);
5438c424e8eSdownsj goto bad;
5445e12154eSdhill }
5458c424e8eSdownsj memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize);
5465e12154eSdhill free(firstblk);
5478c424e8eSdownsj dirty(bp);
54860a51e06Spelikan bp = getdirblk(letoh32(dp->e2di_blocks[lastbn + 1]),
5498c424e8eSdownsj sblock.e2fs_bsize);
5508c424e8eSdownsj if (bp->b_errs)
5518c424e8eSdownsj goto bad;
55260a51e06Spelikan emptydir.dot_reclen = htole16(sblock.e2fs_bsize);
5538c424e8eSdownsj memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
5548c424e8eSdownsj pwarn("NO SPACE LEFT IN %s", name);
5558c424e8eSdownsj if (preen)
5568c424e8eSdownsj printf(" (EXPANDED)\n");
5578c424e8eSdownsj else if (reply("EXPAND") == 0)
5588c424e8eSdownsj goto bad;
5598c424e8eSdownsj dirty(bp);
5608c424e8eSdownsj inodirty();
5618c424e8eSdownsj return (1);
5628c424e8eSdownsj bad:
5638c424e8eSdownsj dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1];
5648c424e8eSdownsj dp->e2di_blocks[lastbn + 1] = 0;
56560a51e06Spelikan dp->e2di_size = htole32(letoh32(dp->e2di_size) - sblock.e2fs_bsize);
566935730fbSniallo inossize(dp, inosize(dp) - sblock.e2fs_bsize);
56760a51e06Spelikan dp->e2di_nblock = htole32(letoh32(dp->e2di_nblock) - 1);
5688c424e8eSdownsj freeblk(newblk);
5698c424e8eSdownsj return (0);
5708c424e8eSdownsj }
5718c424e8eSdownsj
5728c424e8eSdownsj /*
5738c424e8eSdownsj * allocate a new directory
5748c424e8eSdownsj */
5758c424e8eSdownsj int
allocdir(ino_t parent,ino_t request,int mode)5768809fabbSderaadt allocdir(ino_t parent, ino_t request, int mode)
5778c424e8eSdownsj {
5788c424e8eSdownsj ino_t ino;
5798c424e8eSdownsj struct ext2fs_dinode *dp;
5800190393fSart struct bufarea *bp;
5818c424e8eSdownsj struct ext2fs_dirtemplate *dirp;
5828c424e8eSdownsj
5838c424e8eSdownsj ino = allocino(request, IFDIR|mode);
58460a51e06Spelikan dirhead.dot_reclen = htole16(12); /* XXX */
58560a51e06Spelikan dirhead.dotdot_reclen = htole16(sblock.e2fs_bsize - 12); /* XXX */
58665348f21Sjasoni dirhead.dot_namlen = 1;
58765348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
58865348f21Sjasoni (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
58965348f21Sjasoni dirhead.dot_type = EXT2_FT_DIR;
59065348f21Sjasoni else
59165348f21Sjasoni dirhead.dot_type = 0;
59265348f21Sjasoni dirhead.dotdot_namlen = 2;
59365348f21Sjasoni if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
59465348f21Sjasoni (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
59565348f21Sjasoni dirhead.dotdot_type = EXT2_FT_DIR;
59665348f21Sjasoni else
59765348f21Sjasoni dirhead.dotdot_type = 0;
5988c424e8eSdownsj dirp = &dirhead;
59960a51e06Spelikan dirp->dot_ino = htole32(ino);
60060a51e06Spelikan dirp->dotdot_ino = htole32(parent);
6018c424e8eSdownsj dp = ginode(ino);
60260a51e06Spelikan bp = getdirblk(letoh32(dp->e2di_blocks[0]), sblock.e2fs_bsize);
6038c424e8eSdownsj if (bp->b_errs) {
6048c424e8eSdownsj freeino(ino);
6058c424e8eSdownsj return (0);
6068c424e8eSdownsj }
6078c424e8eSdownsj memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate));
6088c424e8eSdownsj dirty(bp);
60960a51e06Spelikan dp->e2di_nlink = htole16(2);
6108c424e8eSdownsj inodirty();
6118c424e8eSdownsj if (ino == EXT2_ROOTINO) {
61260a51e06Spelikan lncntp[ino] = letoh16(dp->e2di_nlink);
6138c424e8eSdownsj cacheino(dp, ino);
6148c424e8eSdownsj return(ino);
6158c424e8eSdownsj }
6168c424e8eSdownsj if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
6178c424e8eSdownsj freeino(ino);
6188c424e8eSdownsj return (0);
6198c424e8eSdownsj }
6208c424e8eSdownsj cacheino(dp, ino);
6218c424e8eSdownsj statemap[ino] = statemap[parent];
6228c424e8eSdownsj if (statemap[ino] == DSTATE) {
62360a51e06Spelikan lncntp[ino] = letoh16(dp->e2di_nlink);
6248c424e8eSdownsj lncntp[parent]++;
6258c424e8eSdownsj }
6268c424e8eSdownsj dp = ginode(parent);
62760a51e06Spelikan dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) + 1);
6288c424e8eSdownsj inodirty();
6298c424e8eSdownsj return (ino);
6308c424e8eSdownsj }
6318c424e8eSdownsj
6328c424e8eSdownsj /*
6338c424e8eSdownsj * free a directory inode
6348c424e8eSdownsj */
6358c424e8eSdownsj static void
freedir(ino_t ino,ino_t parent)6368809fabbSderaadt freedir(ino_t ino, ino_t parent)
6378c424e8eSdownsj {
6388c424e8eSdownsj struct ext2fs_dinode *dp;
6398c424e8eSdownsj
6408c424e8eSdownsj if (ino != parent) {
6418c424e8eSdownsj dp = ginode(parent);
64260a51e06Spelikan dp->e2di_nlink = htole16(letoh16(dp->e2di_nlink) - 1);
6438c424e8eSdownsj inodirty();
6448c424e8eSdownsj }
6458c424e8eSdownsj freeino(ino);
6468c424e8eSdownsj }
6478c424e8eSdownsj
6488c424e8eSdownsj /*
6498c424e8eSdownsj * generate a temporary name for the lost+found directory.
6508c424e8eSdownsj */
6518c424e8eSdownsj static int
lftempname(char * bufp,ino_t ino)6528809fabbSderaadt lftempname(char *bufp, ino_t ino)
6538c424e8eSdownsj {
6540190393fSart ino_t in;
6550190393fSart char *cp;
6568c424e8eSdownsj int namlen;
6578c424e8eSdownsj
6588c424e8eSdownsj cp = bufp + 2;
6598c424e8eSdownsj for (in = maxino; in > 0; in /= 10)
6608c424e8eSdownsj cp++;
6618c424e8eSdownsj *--cp = 0;
6628c424e8eSdownsj namlen = cp - bufp;
6638c424e8eSdownsj in = ino;
6648c424e8eSdownsj while (cp > bufp) {
6658c424e8eSdownsj *--cp = (in % 10) + '0';
6668c424e8eSdownsj in /= 10;
6678c424e8eSdownsj }
6688c424e8eSdownsj *cp = '#';
6698c424e8eSdownsj return (namlen);
6708c424e8eSdownsj }
6718c424e8eSdownsj
6728c424e8eSdownsj /*
6738c424e8eSdownsj * Get a directory block.
6748c424e8eSdownsj * Insure that it is held until another is requested.
6758c424e8eSdownsj */
6768c424e8eSdownsj static struct bufarea *
getdirblk(daddr32_t blkno,long size)677b6d2e2d5Sderaadt getdirblk(daddr32_t blkno, long size)
6788c424e8eSdownsj {
6798c424e8eSdownsj
6808c424e8eSdownsj if (pdirbp != 0)
6818c424e8eSdownsj pdirbp->b_flags &= ~B_INUSE;
6828c424e8eSdownsj pdirbp = getdatablk(blkno, size);
6838c424e8eSdownsj return (pdirbp);
6848c424e8eSdownsj }
685