NetBSD/sbin/fsck_ffs/inode.c

815 lines
20 KiB
C
Raw Normal View History

2005-06-27 05:25:35 +04:00
/* $NetBSD: inode.c,v 1.55 2005/06/27 01:25:35 christos Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1980, 1986, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
1997-09-16 20:44:43 +04:00
static char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
#else
2005-06-27 05:25:35 +04:00
__RCSID("$NetBSD: inode.c,v 1.55 2005/06/27 01:25:35 christos Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
1994-04-25 22:23:19 +04:00
#include <sys/time.h>
1997-09-16 20:44:43 +04:00
#include <ufs/ufs/dinode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
1999-11-15 22:18:24 +03:00
#include <ufs/ufs/ufs_bswap.h>
1997-09-16 20:44:43 +04:00
#ifndef SMALL
1997-09-16 20:44:43 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <pwd.h>
1994-04-18 10:08:27 +04:00
#endif
#include <stdio.h>
1993-03-21 12:45:37 +03:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
1993-03-21 12:45:37 +03:00
#include "fsck.h"
1996-09-28 02:45:10 +04:00
#include "fsutil.h"
#include "extern.h"
1993-03-21 12:45:37 +03:00
static ino_t startinum;
2005-01-20 18:29:40 +03:00
static int iblock(struct inodesc *, long, u_int64_t);
static void swap_dinode1(union dinode *, int);
static void swap_dinode2(union dinode *, int);
int
2005-01-19 20:33:58 +03:00
ckinode(union dinode *dp, struct inodesc *idesc)
1993-03-21 12:45:37 +03:00
{
int ret, offset, i;
union dinode dino;
u_int64_t sizepb;
int64_t remsize;
daddr_t ndb;
mode_t mode;
char pathbuf[MAXPATHLEN + 1];
1993-03-21 12:45:37 +03:00
if (idesc->id_fix != IGNORE)
idesc->id_fix = DONTKNOW;
idesc->id_entryno = 0;
idesc->id_filesize = iswap64(DIP(dp, size));
mode = iswap16(DIP(dp, mode)) & IFMT;
if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
(idesc->id_filesize < sblock->fs_maxsymlinklen ||
(isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
(sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
1993-03-21 12:45:37 +03:00
return (KEEPON);
if (is_ufs2)
dino.dp2 = dp->dp2;
else
dino.dp1 = dp->dp1;
ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
for (i = 0; i < NDADDR; i++) {
if (--ndb == 0 &&
(offset = blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
1993-03-21 12:45:37 +03:00
idesc->id_numfrags =
numfrags(sblock, fragroundup(sblock, offset));
1993-03-21 12:45:37 +03:00
else
idesc->id_numfrags = sblock->fs_frag;
if (DIP(&dino, db[i]) == 0) {
if (idesc->id_type == DATA && ndb >= 0) {
/* An empty block in a directory XXX */
markclean = 0;
2003-07-13 12:16:15 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
2005-05-01 00:29:56 +04:00
pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
pathbuf);
if (reply("ADJUST LENGTH") == 1) {
dp = ginode(idesc->id_number);
DIP(dp, size) = iswap64(i *
sblock->fs_bsize);
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
rerun = 1;
inodirty();
}
}
1993-03-21 12:45:37 +03:00
continue;
}
if (is_ufs2)
idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
else
idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
if (idesc->id_type != DATA)
1993-03-21 12:45:37 +03:00
ret = (*idesc->id_func)(idesc);
else
ret = dirscan(idesc);
if (ret & STOP)
return (ret);
}
idesc->id_numfrags = sblock->fs_frag;
remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * NDADDR;
sizepb = sblock->fs_bsize;
for (i = 0; i < NIADDR; i++) {
if (DIP(&dino, ib[i])) {
if (is_ufs2)
idesc->id_blkno = iswap64(dino.dp2.di_ib[i]);
else
idesc->id_blkno = iswap32(dino.dp1.di_ib[i]);
ret = iblock(idesc, i + 1, remsize);
1993-03-21 12:45:37 +03:00
if (ret & STOP)
return (ret);
} else {
if (idesc->id_type == DATA && remsize > 0) {
/* An empty block in a directory XXX */
markclean = 0;
2003-07-13 12:16:15 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
pathbuf);
if (reply("ADJUST LENGTH") == 1) {
dp = ginode(idesc->id_number);
DIP(dp, size) =
iswap64(iswap64(DIP(dp, size))
- remsize);
remsize = 0;
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
rerun = 1;
inodirty();
break;
}
}
1993-03-21 12:45:37 +03:00
}
sizepb *= NINDIR(sblock);
remsize -= sizepb;
1993-03-21 12:45:37 +03:00
}
return (KEEPON);
}
static int
2005-01-19 20:33:58 +03:00
iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
1993-03-21 12:45:37 +03:00
{
struct bufarea *bp;
2005-01-19 20:33:58 +03:00
int i, n, (*func) (struct inodesc *), nif;
1996-05-25 13:57:36 +04:00
u_int64_t sizepb;
1993-03-21 12:45:37 +03:00
char buf[BUFSIZ];
char pathbuf[MAXPATHLEN + 1];
union dinode *dp;
1993-03-21 12:45:37 +03:00
if (idesc->id_type != DATA) {
1993-03-21 12:45:37 +03:00
func = idesc->id_func;
if (((n = (*func)(idesc)) & KEEPON) == 0)
return (n);
} else
func = dirscan;
if (chkrange(idesc->id_blkno, idesc->id_numfrags))
return (SKIP);
bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
1993-03-21 12:45:37 +03:00
ilevel--;
for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
sizepb *= NINDIR(sblock);
if (howmany(isize, sizepb) > NINDIR(sblock))
nif = NINDIR(sblock);
else
nif = howmany(isize, sizepb);
if (do_blkswap) { /* swap byte order of the whole blk */
if (is_ufs2) {
for (i = 0; i < nif; i++)
bp->b_un.b_indir2[i] =
bswap64(bp->b_un.b_indir2[i]);
} else {
for (i = 0; i < nif; i++)
bp->b_un.b_indir1[i] =
bswap32(bp->b_un.b_indir1[i]);
}
dirty(bp);
flush(fswritefd, bp);
}
if (idesc->id_func == pass1check && nif < NINDIR(sblock)) {
for (i = nif; i < NINDIR(sblock); i++) {
if (IBLK(bp, i) == 0)
1993-03-21 12:45:37 +03:00
continue;
1998-07-28 23:22:54 +04:00
(void)snprintf(buf, sizeof(buf),
"PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
1993-03-21 12:45:37 +03:00
if (dofix(idesc, buf)) {
IBLK(bp, i) = 0;
1993-03-21 12:45:37 +03:00
dirty(bp);
} else
markclean= 0;
1993-03-21 12:45:37 +03:00
}
flush(fswritefd, bp);
}
for (i = 0; i < nif; i++) {
if (IBLK(bp, i)) {
if (is_ufs2)
idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]);
else
idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]);
if (ilevel == 0)
1993-03-21 12:45:37 +03:00
n = (*func)(idesc);
else
n = iblock(idesc, ilevel, isize);
1993-03-21 12:45:37 +03:00
if (n & STOP) {
bp->b_flags &= ~B_INUSE;
return (n);
}
} else {
if (idesc->id_type == DATA && isize > 0) {
/* An empty block in a directory XXX */
markclean= 0;
2003-07-13 12:16:15 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
pathbuf);
if (reply("ADJUST LENGTH") == 1) {
dp = ginode(idesc->id_number);
DIP(dp, size) =
iswap64(iswap64(DIP(dp, size))
- isize);
isize = 0;
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
rerun = 1;
inodirty();
bp->b_flags &= ~B_INUSE;
return(STOP);
}
}
1993-03-21 12:45:37 +03:00
}
isize -= sizepb;
1993-03-21 12:45:37 +03:00
}
bp->b_flags &= ~B_INUSE;
return (KEEPON);
}
/*
* Check that a block in a legal block number.
* Return 0 if in range, 1 if out of range.
*/
int
2005-01-19 20:33:58 +03:00
chkrange(daddr_t blk, int cnt)
1993-03-21 12:45:37 +03:00
{
int c;
1993-03-21 12:45:37 +03:00
if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
cnt - 1 > maxfsblock - blk)
1993-03-21 12:45:37 +03:00
return (1);
if (cnt > sblock->fs_frag ||
fragnum(sblock, blk) + cnt > sblock->fs_frag) {
if (debug)
printf("bad size: blk %lld, offset %d, size %d\n",
(long long)blk, (int)fragnum(sblock, blk), cnt);
}
c = dtog(sblock, blk);
if (blk < cgdmin(sblock, c)) {
if ((blk + cnt) > cgsblock(sblock, c)) {
1993-03-21 12:45:37 +03:00
if (debug) {
printf("blk %lld < cgdmin %lld;",
(long long)blk,
(long long)cgdmin(sblock, c));
printf(" blk + cnt %lld > cgsbase %lld\n",
(long long)(blk + cnt),
(long long)cgsblock(sblock, c));
1993-03-21 12:45:37 +03:00
}
return (1);
}
} else {
if ((blk + cnt) > cgbase(sblock, c+1)) {
1993-03-21 12:45:37 +03:00
if (debug) {
printf("blk %lld >= cgdmin %lld;",
(long long)blk,
(long long)cgdmin(sblock, c));
printf(" blk + cnt %lld > sblock->fs_fpg %d\n",
(long long)(blk+cnt), sblock->fs_fpg);
1993-03-21 12:45:37 +03:00
}
return (1);
}
}
return (0);
}
/*
* General purpose interface for reading inodes.
*/
union dinode *
2005-01-19 20:33:58 +03:00
ginode(ino_t inumber)
1993-03-21 12:45:37 +03:00
{
daddr_t iblk;
int blkoff;
1993-03-21 12:45:37 +03:00
if (inumber < ROOTINO || inumber > maxino)
1997-09-16 20:44:43 +04:00
errx(EEXIT, "bad inode number %d to ginode", inumber);
1993-03-21 12:45:37 +03:00
if (startinum == 0 ||
inumber < startinum || inumber >= startinum + INOPB(sblock)) {
iblk = ino_to_fsba(sblock, inumber);
1993-03-21 12:45:37 +03:00
if (pbp != 0)
pbp->b_flags &= ~B_INUSE;
pbp = getdatablk(iblk, sblock->fs_bsize);
startinum = (inumber / INOPB(sblock)) * INOPB(sblock);
1993-03-21 12:45:37 +03:00
}
if (is_ufs2) {
blkoff = (inumber % INOPB(sblock)) * DINODE2_SIZE;
return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
}
blkoff = (inumber % INOPB(sblock)) * DINODE1_SIZE;
return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
}
static void
swap_dinode1(union dinode *dp, int n)
{
int i, j;
struct ufs1_dinode *dp1;
int32_t maxsymlinklen = sblock->fs_maxsymlinklen;
if (isappleufs)
maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
dp1 = (struct ufs1_dinode *)&dp->dp1;
for (i = 0; i < n; i++, dp1++) {
ffs_dinode1_swap(dp1, dp1);
if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
doinglevel2 ||
(maxsymlinklen < 0) ||
(iswap64(dp1->di_size) > maxsymlinklen)) {
for (j = 0; j < (NDADDR + NIADDR); j++)
dp1->di_db[j] = bswap32(dp1->di_db[j]);
}
}
}
static void
swap_dinode2(union dinode *dp, int n)
{
int i, j;
struct ufs2_dinode *dp2;
dp2 = (struct ufs2_dinode *)&dp->dp2;
for (i = 0; i < n; i++, dp2++) {
ffs_dinode2_swap(dp2, dp2);
if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++)
dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
}
}
1993-03-21 12:45:37 +03:00
}
/*
* Special purpose version of ginode used to optimize first pass
* over all the inodes in numerical order.
*/
ino_t nextino, lastinum, lastvalidinum;
1993-03-21 12:45:37 +03:00
long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
union dinode *inodebuf;
1993-03-21 12:45:37 +03:00
union dinode *
2005-01-19 20:33:58 +03:00
getnextinode(ino_t inumber)
1993-03-21 12:45:37 +03:00
{
long size;
daddr_t dblk;
static union dinode *dp;
union dinode *ret;
1993-03-21 12:45:37 +03:00
if (inumber != nextino++ || inumber > lastvalidinum)
1997-09-16 20:44:43 +04:00
errx(EEXIT, "bad inode number %d to nextinode", inumber);
1993-03-21 12:45:37 +03:00
if (inumber >= lastinum) {
readcnt++;
dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
1993-03-21 12:45:37 +03:00
if (readcnt % readpercg == 0) {
size = partialsize;
lastinum += partialcnt;
} else {
size = inobufsize;
lastinum += fullcnt;
}
(void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
if (doswap) {
if (is_ufs2)
swap_dinode2(inodebuf, lastinum - inumber);
else
swap_dinode1(inodebuf, lastinum - inumber);
bwrite(fswritefd, (char *)inodebuf, dblk, size);
}
dp = (union dinode *)inodebuf;
1993-03-21 12:45:37 +03:00
}
ret = dp;
dp = (union dinode *)
((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
return ret;
1993-03-21 12:45:37 +03:00
}
void
2005-01-19 20:33:58 +03:00
setinodebuf(ino_t inum)
1993-03-21 12:45:37 +03:00
{
if (inum % sblock->fs_ipg != 0)
errx(EEXIT, "bad inode number %d to setinodebuf", inum);
lastvalidinum = inum + sblock->fs_ipg - 1;
1993-03-21 12:45:37 +03:00
startinum = 0;
nextino = inum;
lastinum = inum;
1993-03-21 12:45:37 +03:00
readcnt = 0;
if (inodebuf != NULL)
return;
inobufsize = blkroundup(sblock, INOBUFSIZE);
fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
readpercg = sblock->fs_ipg / fullcnt;
partialcnt = sblock->fs_ipg % fullcnt;
partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
1993-03-21 12:45:37 +03:00
if (partialcnt != 0) {
readpercg++;
} else {
partialcnt = fullcnt;
partialsize = inobufsize;
}
if (inodebuf == NULL &&
(inodebuf = malloc((unsigned)inobufsize)) == NULL)
1997-09-16 20:44:43 +04:00
errx(EEXIT, "Cannot allocate space for inode buffer");
1993-03-21 12:45:37 +03:00
}
void
2005-01-19 20:33:58 +03:00
freeinodebuf(void)
1993-03-21 12:45:37 +03:00
{
if (inodebuf != NULL)
free((char *)inodebuf);
inodebuf = NULL;
}
/*
* Routines to maintain information about directory inodes.
* This is built during the first pass and used during the
* second and third passes.
*
* Enter inodes into the cache.
*/
void
2005-01-19 20:33:58 +03:00
cacheino(union dinode *dp, ino_t inumber)
1993-03-21 12:45:37 +03:00
{
struct inoinfo *inp;
2003-09-19 12:35:15 +04:00
struct inoinfo **inpp, **ninpsort;
unsigned int blks;
int i;
int64_t size;
1993-03-21 12:45:37 +03:00
size = iswap64(DIP(dp, size));
blks = howmany(size, sblock->fs_bsize);
1993-03-21 12:45:37 +03:00
if (blks > NDADDR)
blks = NDADDR + NIADDR;
inp = (struct inoinfo *) malloc(sizeof(*inp) + (blks - 1)
* sizeof (int64_t));
1993-03-21 12:45:37 +03:00
if (inp == NULL)
return;
inpp = &inphead[inumber % dirhash];
1993-03-21 12:45:37 +03:00
inp->i_nexthash = *inpp;
*inpp = inp;
2004-07-20 19:05:32 +04:00
inp->i_child = inp->i_sibling = 0;
if (inumber == ROOTINO)
inp->i_parent = ROOTINO;
else
inp->i_parent = (ino_t)0;
1993-03-21 12:45:37 +03:00
inp->i_dotdot = (ino_t)0;
inp->i_number = inumber;
inp->i_isize = size;
inp->i_numblks = blks;
for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
inp->i_blks[i] = DIP(dp, db[i]);
if (blks > NDADDR)
for (i = 0; i < NIADDR; i++)
inp->i_blks[NDADDR + i] = DIP(dp, ib[i]);
1993-03-21 12:45:37 +03:00
if (inplast == listmax) {
2003-09-19 12:35:15 +04:00
ninpsort = (struct inoinfo **)realloc((char *)inpsort,
(unsigned)(listmax + 100) * sizeof(struct inoinfo *));
1993-03-21 12:45:37 +03:00
if (inpsort == NULL)
1997-09-16 20:44:43 +04:00
errx(EEXIT, "cannot increase directory list");
2003-09-19 12:35:15 +04:00
inpsort = ninpsort;
listmax += 100;
1993-03-21 12:45:37 +03:00
}
inpsort[inplast++] = inp;
}
/*
* Look up an inode cache structure.
*/
struct inoinfo *
2005-01-19 20:33:58 +03:00
getinoinfo(ino_t inumber)
1993-03-21 12:45:37 +03:00
{
struct inoinfo *inp;
1993-03-21 12:45:37 +03:00
for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
1993-03-21 12:45:37 +03:00
if (inp->i_number != inumber)
continue;
return (inp);
}
1997-09-16 20:44:43 +04:00
errx(EEXIT, "cannot find inode %d", inumber);
1993-03-21 12:45:37 +03:00
return ((struct inoinfo *)0);
}
/*
* Clean up all the inode cache structure.
*/
void
2005-01-19 20:33:58 +03:00
inocleanup(void)
1993-03-21 12:45:37 +03:00
{
struct inoinfo **inpp;
1993-03-21 12:45:37 +03:00
if (inphead == NULL)
return;
for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
free((char *)(*inpp));
free((char *)inphead);
free((char *)inpsort);
inphead = inpsort = NULL;
}
void
2005-01-19 20:33:58 +03:00
inodirty(void)
1993-03-21 12:45:37 +03:00
{
dirty(pbp);
}
void
2005-06-27 05:25:35 +04:00
clri(struct inodesc *idesc, const char *type, int flag)
1993-03-21 12:45:37 +03:00
{
union dinode *dp;
1993-03-21 12:45:37 +03:00
dp = ginode(idesc->id_number);
if (flag == 1) {
pwarn("%s %s", type,
(iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
1993-03-21 12:45:37 +03:00
pinode(idesc->id_number);
}
if (preen || reply("CLEAR") == 1) {
if (preen)
printf(" (CLEARED)\n");
n_files--;
(void)ckinode(dp, idesc);
clearinode(dp);
inoinfo(idesc->id_number)->ino_state = USTATE;
1993-03-21 12:45:37 +03:00
inodirty();
} else
markclean= 0;
1993-03-21 12:45:37 +03:00
}
int
2005-01-19 20:33:58 +03:00
findname(struct inodesc *idesc)
1993-03-21 12:45:37 +03:00
{
struct direct *dirp = idesc->id_dirp;
2005-06-27 05:25:35 +04:00
size_t len;
char *buf;
1993-03-21 12:45:37 +03:00
if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
idesc->id_entryno++;
1993-03-21 12:45:37 +03:00
return (KEEPON);
}
2005-06-27 05:25:35 +04:00
if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
/* XXX: We don't fix but we ignore */
len = MAXPATHLEN;
}
/* this is namebuf from utilities.c */
buf = __UNCONST(idesc->id_name);
(void)memcpy(buf, dirp->d_name, (size_t)dirp->d_namlen + 1);
1993-03-21 12:45:37 +03:00
return (STOP|FOUND);
}
int
2005-01-19 20:33:58 +03:00
findino(struct inodesc *idesc)
1993-03-21 12:45:37 +03:00
{
struct direct *dirp = idesc->id_dirp;
1993-03-21 12:45:37 +03:00
if (dirp->d_ino == 0)
return (KEEPON);
if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
idesc->id_parent = iswap32(dirp->d_ino);
1993-03-21 12:45:37 +03:00
return (STOP|FOUND);
}
return (KEEPON);
}
int
2005-01-19 20:33:58 +03:00
clearentry(struct inodesc *idesc)
{
struct direct *dirp = idesc->id_dirp;
if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
idesc->id_entryno++;
return (KEEPON);
}
dirp->d_ino = 0;
return (STOP|FOUND|ALTERED);
}
void
2005-01-19 20:33:58 +03:00
pinode(ino_t ino)
1993-03-21 12:45:37 +03:00
{
union dinode *dp;
char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
time_t t;
1993-03-21 12:45:37 +03:00
printf(" I=%u ", ino);
1993-03-21 12:45:37 +03:00
if (ino < ROOTINO || ino > maxino)
return;
dp = ginode(ino);
printf(" OWNER=");
1994-04-18 10:08:27 +04:00
#ifndef SMALL
if ((pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
1993-03-21 12:45:37 +03:00
printf("%s ", pw->pw_name);
else
1994-04-18 10:08:27 +04:00
#endif
printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
printf("MODE=%o\n", iswap16(DIP(dp, mode)));
1993-03-21 12:45:37 +03:00
if (preen)
printf("%s: ", cdevname());
printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
t = iswap32(DIP(dp, mtime));
p = ctime(&t);
1993-03-21 12:45:37 +03:00
printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
}
void
2005-06-27 05:25:35 +04:00
blkerror(ino_t ino, const char *type, daddr_t blk)
1993-03-21 12:45:37 +03:00
{
struct inostat *info;
1993-03-21 12:45:37 +03:00
pfatal("%lld %s I=%u", (long long)blk, type, ino);
1993-03-21 12:45:37 +03:00
printf("\n");
info = inoinfo(ino);
switch (info->ino_state) {
1993-03-21 12:45:37 +03:00
case FSTATE:
info->ino_state = FCLEAR;
1993-03-21 12:45:37 +03:00
return;
case DSTATE:
info->ino_state = DCLEAR;
1993-03-21 12:45:37 +03:00
return;
case FCLEAR:
case DCLEAR:
return;
default:
errx(EEXIT, "BAD STATE %d TO BLKERR", info->ino_state);
1993-03-21 12:45:37 +03:00
/* NOTREACHED */
}
}
/*
* allocate an unused inode
*/
ino_t
2005-01-19 20:33:58 +03:00
allocino(ino_t request, int type)
1993-03-21 12:45:37 +03:00
{
ino_t ino;
union dinode *dp;
struct ufs1_dinode *dp1;
struct ufs2_dinode *dp2;
1995-12-15 01:17:26 +03:00
time_t t;
1999-11-15 22:18:24 +03:00
struct cg *cgp = cgrp;
int cg;
2005-06-02 04:38:41 +04:00
struct inostat *info = NULL;
1993-03-21 12:45:37 +03:00
if (request == 0)
request = ROOTINO;
else if (inoinfo(request)->ino_state != USTATE)
1993-03-21 12:45:37 +03:00
return (0);
for (ino = request; ino < maxino; ino++) {
info = inoinfo(ino);
if (info->ino_state == USTATE)
1993-03-21 12:45:37 +03:00
break;
}
1993-03-21 12:45:37 +03:00
if (ino == maxino)
return (0);
1999-11-15 22:18:24 +03:00
cg = ino_to_cg(sblock, ino);
/* If necessary, extend the inoinfo array. grow exponentially */
if ((ino % sblock->fs_ipg) >= inostathead[cg].il_numalloced) {
unsigned long newalloced, i;
newalloced = MIN(sblock->fs_ipg,
MAX(2 * inostathead[cg].il_numalloced, 10));
info = calloc(newalloced, sizeof(struct inostat));
if (info == NULL) {
pwarn("cannot alloc %lu bytes to extend inoinfo\n",
sizeof(struct inostat) * newalloced);
return 0;
}
memmove(info, inostathead[cg].il_stat,
inostathead[cg].il_numalloced * sizeof(*info));
for (i = inostathead[cg].il_numalloced; i < newalloced; i++) {
info[i].ino_state = USTATE;
}
if (inostathead[cg].il_numalloced)
free(inostathead[cg].il_stat);
inostathead[cg].il_stat = info;
inostathead[cg].il_numalloced = newalloced;
info = inoinfo(ino);
}
1999-11-15 22:18:24 +03:00
getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
if ((doswap && !needswap) || (!doswap && needswap))
ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
1999-11-15 22:18:24 +03:00
if (!cg_chkmagic(cgp, 0))
pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
if (doswap)
cgdirty();
setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
cgp->cg_cs.cs_nifree--;
1993-03-21 12:45:37 +03:00
switch (type & IFMT) {
case IFDIR:
info->ino_state = DSTATE;
1999-11-15 22:18:24 +03:00
cgp->cg_cs.cs_ndir++;
1993-03-21 12:45:37 +03:00
break;
case IFREG:
case IFLNK:
info->ino_state = FSTATE;
1993-03-21 12:45:37 +03:00
break;
default:
return (0);
}
1999-11-15 22:18:24 +03:00
cgdirty();
1993-03-21 12:45:37 +03:00
dp = ginode(ino);
if (is_ufs2) {
dp2 = &dp->dp2;
dp2->di_db[0] = iswap64(allocblk(1));
if (dp2->di_db[0] == 0) {
info->ino_state = USTATE;
return (0);
}
dp2->di_mode = iswap16(type);
dp2->di_flags = 0;
(void)time(&t);
dp2->di_atime = iswap64(t);
dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
dp2->di_size = iswap64(sblock->fs_fsize);
dp2->di_blocks = iswap64(btodb(sblock->fs_fsize));
} else {
dp1 = &dp->dp1;
dp1->di_db[0] = iswap32(allocblk(1));
if (dp1->di_db[0] == 0) {
info->ino_state = USTATE;
return (0);
}
dp1->di_mode = iswap16(type);
dp1->di_flags = 0;
(void)time(&t);
dp1->di_atime = iswap32(t);
dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
dp1->di_size = iswap64(sblock->fs_fsize);
dp1->di_blocks = iswap32(btodb(sblock->fs_fsize));
1993-03-21 12:45:37 +03:00
}
n_files++;
inodirty();
if (newinofmt)
info->ino_type = IFTODT(type);
1993-03-21 12:45:37 +03:00
return (ino);
}
/*
* deallocate an inode
*/
void
2005-01-19 20:33:58 +03:00
freeino(ino_t ino)
1993-03-21 12:45:37 +03:00
{
struct inodesc idesc;
union dinode *dp;
1993-03-21 12:45:37 +03:00
memset(&idesc, 0, sizeof(struct inodesc));
1993-03-21 12:45:37 +03:00
idesc.id_type = ADDR;
idesc.id_func = pass4check;
idesc.id_number = ino;
dp = ginode(ino);
(void)ckinode(dp, &idesc);
clearinode(dp);
inodirty();
inoinfo(ino)->ino_state = USTATE;
1993-03-21 12:45:37 +03:00
n_files--;
}