NetBSD/sbin/fsck_lfs/inode.c

646 lines
16 KiB
C
Raw Normal View History

/* $NetBSD: inode.c,v 1.41 2010/02/04 23:55:43 christos Exp $ */
/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Konrad E. Schroder <perseant@hhhh.org>.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1980, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* 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
* 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/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/dir.h>
#define vnode uvnode
#include <ufs/lfs/lfs.h>
#undef vnode
#include <err.h>
#ifndef SMALL
#include <pwd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include "bufcache.h"
#include "vnode.h"
#include "lfs_user.h"
#include "fsck.h"
#include "fsutil.h"
#include "extern.h"
extern SEGUSE *seg_table;
extern ufs_daddr_t *din_table;
static int iblock(struct inodesc *, long, u_int64_t);
int blksreqd(struct lfs *, int);
int lfs_maxino(void);
/*
* Get a dinode of a given inum.
* XXX combine this function with vget.
*/
struct ufs1_dinode *
ginode(ino_t ino)
{
struct uvnode *vp;
struct ubuf *bp;
Merge the short-lived perseant-lfsv2 branch into the trunk. Kernels and tools understand both v1 and v2 filesystems; newfs_lfs generates v2 by default. Changes for the v2 layout include: - Segments of non-PO2 size and arbitrary block offset, so these can be matched to convenient physical characteristics of the partition (e.g., stripe or track size and offset). - Address by fragment instead of by disk sector, paving the way for non-512-byte-sector devices. In theory fragments can be as large as you like, though in reality they must be smaller than MAXBSIZE in size. - Use serial number and filesystem identifier to ensure that roll-forward doesn't get old data and think it's new. Roll-forward is enabled for v2 filesystems, though not for v1 filesystems by default. - The inode free list is now a tailq, paving the way for undelete (undelete is not yet implemented, but can be without further non-backwards-compatible changes to disk structures). - Inode atime information is kept in the Ifile, instead of on the inode; that is, the inode is never written *just* because atime was changed. Because of this the inodes remain near the file data on the disk, rather than wandering all over as the disk is read repeatedly. This speeds up repeated reads by a small but noticeable amount. Other changes of note include: - The ifile written by newfs_lfs can now be of arbitrary length, it is no longer restricted to a single indirect block. - Fixed an old bug where ctime was changed every time a vnode was created. I need to look more closely to make sure that the times are only updated during write(2) and friends, not after-the-fact during a segment write, and certainly not by the cleaner.
2001-07-14 00:30:18 +04:00
IFILE *ifp;
2000-05-23 05:48:52 +04:00
vp = vget(fs, ino);
if (vp == NULL)
2000-05-23 05:48:52 +04:00
return NULL;
if (din_table[ino] == 0x0) {
LFS_IENTRY(ifp, fs, ino, bp);
din_table[ino] = ifp->if_daddr;
seg_table[dtosn(fs, ifp->if_daddr)].su_nbytes += DINODE1_SIZE;
brelse(bp, 0);
2000-05-23 05:48:52 +04:00
}
return (VTOI(vp)->i_din.ffs1_din);
}
/*
* Check validity of held blocks in an inode, recursing through all blocks.
*/
int
ckinode(struct ufs1_dinode *dp, struct inodesc *idesc)
{
ufs_daddr_t *ap, lbn;
long ret, n, ndb, offset;
struct ufs1_dinode dino;
u_int64_t remsize, sizepb;
mode_t mode;
char pathbuf[MAXPATHLEN + 1];
struct uvnode *vp, *thisvp;
2000-05-23 05:48:52 +04:00
if (idesc->id_fix != IGNORE)
idesc->id_fix = DONTKNOW;
idesc->id_entryno = 0;
idesc->id_filesize = dp->di_size;
mode = dp->di_mode & IFMT;
if (mode == IFBLK || mode == IFCHR ||
(mode == IFLNK && (dp->di_size < fs->lfs_maxsymlinklen ||
(fs->lfs_maxsymlinklen == 0 &&
dp->di_blocks == 0))))
2000-05-23 05:48:52 +04:00
return (KEEPON);
dino = *dp;
ndb = howmany(dino.di_size, fs->lfs_bsize);
2000-05-23 05:48:52 +04:00
thisvp = vget(fs, idesc->id_number);
for (lbn = 0; lbn < NDADDR; lbn++) {
ap = dino.di_db + lbn;
if (thisvp)
2000-05-23 05:48:52 +04:00
idesc->id_numfrags =
numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
else {
if (--ndb == 0 && (offset = blkoff(fs, dino.di_size)) != 0) {
idesc->id_numfrags =
numfrags(fs, fragroundup(fs, offset));
} else
idesc->id_numfrags = fs->lfs_frag;
}
2000-05-23 05:48:52 +04:00
if (*ap == 0) {
if (idesc->id_type == DATA && ndb >= 0) {
/* An empty block in a directory XXX */
2003-07-13 12:05:58 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]",
pathbuf, (long long)idesc->id_number);
2000-05-23 05:48:52 +04:00
if (reply("ADJUST LENGTH") == 1) {
vp = vget(fs, idesc->id_number);
dp = VTOD(vp);
2000-05-23 05:48:52 +04:00
dp->di_size = (ap - &dino.di_db[0]) *
fs->lfs_bsize;
2000-05-23 05:48:52 +04:00
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
2000-05-23 05:48:52 +04:00
rerun = 1;
inodirty(VTOI(vp));
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
} else
break;
2000-05-23 05:48:52 +04:00
}
continue;
}
idesc->id_blkno = *ap;
idesc->id_lblkno = ap - &dino.di_db[0];
if (idesc->id_type == ADDR) {
ret = (*idesc->id_func) (idesc);
2000-05-23 05:48:52 +04:00
} else
ret = dirscan(idesc);
if (ret & STOP)
return (ret);
}
idesc->id_numfrags = fs->lfs_frag;
remsize = dino.di_size - fs->lfs_bsize * NDADDR;
sizepb = fs->lfs_bsize;
2000-05-23 05:48:52 +04:00
for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
if (*ap) {
idesc->id_blkno = *ap;
ret = iblock(idesc, n, remsize);
if (ret & STOP)
return (ret);
} else {
if (idesc->id_type == DATA && remsize > 0) {
/* An empty block in a directory XXX */
2003-07-13 12:05:58 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]",
pathbuf, (long long)idesc->id_number);
2000-05-23 05:48:52 +04:00
if (reply("ADJUST LENGTH") == 1) {
vp = vget(fs, idesc->id_number);
dp = VTOD(vp);
2000-05-23 05:48:52 +04:00
dp->di_size -= remsize;
remsize = 0;
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
2000-05-23 05:48:52 +04:00
rerun = 1;
inodirty(VTOI(vp));
2000-05-23 05:48:52 +04:00
break;
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
} else
break;
2000-05-23 05:48:52 +04:00
}
}
sizepb *= NINDIR(fs);
2000-05-23 05:48:52 +04:00
remsize -= sizepb;
}
return (KEEPON);
}
static int
iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
{
ufs_daddr_t *ap, *aplim;
struct ubuf *bp;
int i, n, (*func) (struct inodesc *), nif;
u_int64_t sizepb;
char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
struct uvnode *devvp, *vp;
int diddirty = 0;
2000-05-23 05:48:52 +04:00
if (idesc->id_type == ADDR) {
func = idesc->id_func;
n = (*func) (idesc);
2000-05-23 05:48:52 +04:00
if ((n & KEEPON) == 0)
return (n);
} else
func = dirscan;
if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags)))
2000-05-23 05:48:52 +04:00
return (SKIP);
devvp = fs->lfs_devvp;
bread(devvp, fsbtodb(fs, idesc->id_blkno), fs->lfs_bsize,
NOCRED, 0, &bp);
2000-05-23 05:48:52 +04:00
ilevel--;
for (sizepb = fs->lfs_bsize, i = 0; i < ilevel; i++)
sizepb *= NINDIR(fs);
if (isize > sizepb * NINDIR(fs))
nif = NINDIR(fs);
2000-05-23 05:48:52 +04:00
else
nif = howmany(isize, sizepb);
if (idesc->id_func == pass1check && nif < NINDIR(fs)) {
aplim = ((ufs_daddr_t *) bp->b_data) + NINDIR(fs);
for (ap = ((ufs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) {
2000-05-23 05:48:52 +04:00
if (*ap == 0)
continue;
2005-08-19 06:07:18 +04:00
(void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%llu",
(unsigned long long)idesc->id_number);
2000-05-23 05:48:52 +04:00
if (dofix(idesc, buf)) {
*ap = 0;
++diddirty;
2000-05-23 05:48:52 +04:00
}
}
}
aplim = ((ufs_daddr_t *) bp->b_data) + nif;
for (ap = ((ufs_daddr_t *) bp->b_data); ap < aplim; ap++) {
2000-05-23 05:48:52 +04:00
if (*ap) {
idesc->id_blkno = *ap;
if (ilevel == 0) {
/*
* dirscan needs lblkno.
*/
idesc->id_lblkno++;
n = (*func) (idesc);
} else {
2000-05-23 05:48:52 +04:00
n = iblock(idesc, ilevel, isize);
}
2000-05-23 05:48:52 +04:00
if (n & STOP) {
if (diddirty)
VOP_BWRITE(bp);
else
brelse(bp, 0);
2000-05-23 05:48:52 +04:00
return (n);
}
} else {
if (idesc->id_type == DATA && isize > 0) {
/* An empty block in a directory XXX */
2003-07-13 12:05:58 +04:00
getpathname(pathbuf, sizeof(pathbuf),
idesc->id_number, idesc->id_number);
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]",
pathbuf, (long long)idesc->id_number);
2000-05-23 05:48:52 +04:00
if (reply("ADJUST LENGTH") == 1) {
vp = vget(fs, idesc->id_number);
VTOI(vp)->i_ffs1_size -= isize;
2000-05-23 05:48:52 +04:00
isize = 0;
printf(
"YOU MUST RERUN FSCK AFTERWARDS\n");
2000-05-23 05:48:52 +04:00
rerun = 1;
inodirty(VTOI(vp));
if (diddirty)
VOP_BWRITE(bp);
else
brelse(bp, 0);
2000-05-23 05:48:52 +04:00
return (STOP);
}
}
}
isize -= sizepb;
}
if (diddirty)
VOP_BWRITE(bp);
else
brelse(bp, 0);
2000-05-23 05:48:52 +04:00
return (KEEPON);
}
/*
* Check that a block in a legal block number.
* Return 0 if in range, 1 if out of range.
*/
int
2000-05-23 05:48:52 +04:00
chkrange(daddr_t blk, int cnt)
{
if (blk < sntod(fs, 0)) {
Merge the short-lived perseant-lfsv2 branch into the trunk. Kernels and tools understand both v1 and v2 filesystems; newfs_lfs generates v2 by default. Changes for the v2 layout include: - Segments of non-PO2 size and arbitrary block offset, so these can be matched to convenient physical characteristics of the partition (e.g., stripe or track size and offset). - Address by fragment instead of by disk sector, paving the way for non-512-byte-sector devices. In theory fragments can be as large as you like, though in reality they must be smaller than MAXBSIZE in size. - Use serial number and filesystem identifier to ensure that roll-forward doesn't get old data and think it's new. Roll-forward is enabled for v2 filesystems, though not for v1 filesystems by default. - The inode free list is now a tailq, paving the way for undelete (undelete is not yet implemented, but can be without further non-backwards-compatible changes to disk structures). - Inode atime information is kept in the Ifile, instead of on the inode; that is, the inode is never written *just* because atime was changed. Because of this the inodes remain near the file data on the disk, rather than wandering all over as the disk is read repeatedly. This speeds up repeated reads by a small but noticeable amount. Other changes of note include: - The ifile written by newfs_lfs can now be of arbitrary length, it is no longer restricted to a single indirect block. - Fixed an old bug where ctime was changed every time a vnode was created. I need to look more closely to make sure that the times are only updated during write(2) and friends, not after-the-fact during a segment write, and certainly not by the cleaner.
2001-07-14 00:30:18 +04:00
return (1);
}
if (blk > maxfsblock) {
return (1);
}
if (blk + cnt < sntod(fs, 0)) {
return (1);
}
Merge the short-lived perseant-lfsv2 branch into the trunk. Kernels and tools understand both v1 and v2 filesystems; newfs_lfs generates v2 by default. Changes for the v2 layout include: - Segments of non-PO2 size and arbitrary block offset, so these can be matched to convenient physical characteristics of the partition (e.g., stripe or track size and offset). - Address by fragment instead of by disk sector, paving the way for non-512-byte-sector devices. In theory fragments can be as large as you like, though in reality they must be smaller than MAXBSIZE in size. - Use serial number and filesystem identifier to ensure that roll-forward doesn't get old data and think it's new. Roll-forward is enabled for v2 filesystems, though not for v1 filesystems by default. - The inode free list is now a tailq, paving the way for undelete (undelete is not yet implemented, but can be without further non-backwards-compatible changes to disk structures). - Inode atime information is kept in the Ifile, instead of on the inode; that is, the inode is never written *just* because atime was changed. Because of this the inodes remain near the file data on the disk, rather than wandering all over as the disk is read repeatedly. This speeds up repeated reads by a small but noticeable amount. Other changes of note include: - The ifile written by newfs_lfs can now be of arbitrary length, it is no longer restricted to a single indirect block. - Fixed an old bug where ctime was changed every time a vnode was created. I need to look more closely to make sure that the times are only updated during write(2) and friends, not after-the-fact during a segment write, and certainly not by the cleaner.
2001-07-14 00:30:18 +04:00
if (blk + cnt > maxfsblock) {
2000-05-23 05:48:52 +04:00
return (1);
}
return (0);
}
/*
* 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
cacheino(struct ufs1_dinode * dp, ino_t inumber)
{
struct inoinfo *inp;
2003-09-19 12:29:58 +04:00
struct inoinfo **inpp, **ninpsort;
unsigned int blks;
2000-05-23 05:48:52 +04:00
blks = howmany(dp->di_size, fs->lfs_bsize);
2000-05-23 05:48:52 +04:00
if (blks > NDADDR)
blks = NDADDR + NIADDR;
inp = emalloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t));
2000-05-23 05:48:52 +04:00
inpp = &inphead[inumber % numdirs];
inp->i_nexthash = *inpp;
*inpp = inp;
inp->i_child = inp->i_sibling = inp->i_parentp = 0;
if (inumber == ROOTINO)
inp->i_parent = ROOTINO;
else
inp->i_parent = (ino_t) 0;
inp->i_dotdot = (ino_t) 0;
2000-05-23 05:48:52 +04:00
inp->i_number = inumber;
inp->i_isize = dp->di_size;
inp->i_numblks = blks * sizeof(ufs_daddr_t);
memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks);
2000-05-23 05:48:52 +04:00
if (inplast == listmax) {
ninpsort = erealloc(inpsort,
(listmax + 100) * sizeof(struct inoinfo *));
2003-09-19 12:29:58 +04:00
inpsort = ninpsort;
listmax += 100;
2000-05-23 05:48:52 +04:00
}
inpsort[inplast++] = inp;
}
/*
* Look up an inode cache structure.
*/
struct inoinfo *
2000-05-23 05:48:52 +04:00
getinoinfo(ino_t inumber)
{
struct inoinfo *inp;
2000-05-23 05:48:52 +04:00
for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
if (inp->i_number != inumber)
continue;
return (inp);
}
err(EEXIT, "cannot find inode %llu\n", (unsigned long long)inumber);
return ((struct inoinfo *) 0);
}
/*
* Clean up all the inode cache structure.
*/
void
inocleanup(void)
{
struct inoinfo **inpp;
2000-05-23 05:48:52 +04:00
if (inphead == NULL)
return;
for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
free((char *) (*inpp));
free((char *) inphead);
free((char *) inpsort);
2000-05-23 05:48:52 +04:00
inphead = inpsort = NULL;
}
void
inodirty(struct inode *ip)
{
ip->i_flag |= IN_MODIFIED;
}
void
2005-06-27 06:48:28 +04:00
clri(struct inodesc * idesc, const char *type, int flag)
{
struct uvnode *vp;
2000-05-23 05:48:52 +04:00
vp = vget(fs, idesc->id_number);
if (flag & 0x1) {
2000-05-23 05:48:52 +04:00
pwarn("%s %s", type,
(VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
2000-05-23 05:48:52 +04:00
pinode(idesc->id_number);
}
if ((flag & 0x2) || preen || reply("CLEAR") == 1) {
if (preen && flag != 2)
2000-05-23 05:48:52 +04:00
printf(" (CLEARED)\n");
n_files--;
(void) ckinode(VTOD(vp), idesc);
clearinode(idesc->id_number);
2000-05-23 05:48:52 +04:00
statemap[idesc->id_number] = USTATE;
vnode_destroy(vp);
return;
}
return;
}
void
clearinode(ino_t inumber)
{
struct ubuf *bp;
IFILE *ifp;
daddr_t daddr;
2000-05-23 05:48:52 +04:00
/* Send cleared inode to the free list */
2000-05-23 05:48:52 +04:00
LFS_IENTRY(ifp, fs, inumber, bp);
daddr = ifp->if_daddr;
if (daddr == LFS_UNUSED_DADDR) {
brelse(bp, 0);
return;
}
ifp->if_daddr = LFS_UNUSED_DADDR;
ifp->if_nextfree = fs->lfs_freehd;
fs->lfs_freehd = inumber;
sbdirty();
VOP_BWRITE(bp);
/*
* update segment usage.
*/
if (daddr != LFS_UNUSED_DADDR) {
SEGUSE *sup;
u_int32_t oldsn = dtosn(fs, daddr);
seg_table[oldsn].su_nbytes -= DINODE1_SIZE;
LFS_SEGENTRY(sup, fs, oldsn, bp);
sup->su_nbytes -= DINODE1_SIZE;
LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
2000-05-23 05:48:52 +04:00
}
}
int
findname(struct inodesc * idesc)
{
struct direct *dirp = idesc->id_dirp;
2005-06-27 06:48:28 +04:00
size_t len;
char *buf;
2000-05-23 05:48:52 +04:00
if (dirp->d_ino != idesc->id_parent)
return (KEEPON);
2005-06-27 06:48:28 +04:00
if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
/* Truncate it but don't overflow the buffer */
len = MAXPATHLEN;
}
/* this is namebuf with utils.h */
buf = __UNCONST(idesc->id_name);
(void)memcpy(buf, dirp->d_name, len);
2000-05-23 05:48:52 +04:00
return (STOP | FOUND);
}
int
findino(struct inodesc * idesc)
{
struct direct *dirp = idesc->id_dirp;
2000-05-23 05:48:52 +04:00
if (dirp->d_ino == 0)
return (KEEPON);
if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
2000-05-23 05:48:52 +04:00
idesc->id_parent = dirp->d_ino;
return (STOP | FOUND);
}
return (KEEPON);
}
void
2000-05-23 05:48:52 +04:00
pinode(ino_t ino)
{
struct ufs1_dinode *dp;
struct passwd *pw;
2000-05-23 05:48:52 +04:00
2005-08-19 06:07:18 +04:00
printf(" I=%llu ", (unsigned long long)ino);
if (ino < ROOTINO || ino >= maxino)
2000-05-23 05:48:52 +04:00
return;
dp = ginode(ino);
if (dp) {
printf(" OWNER=");
#ifndef SMALL
if (Uflag && (pw = getpwuid((int) dp->di_uid)) != 0)
2000-05-23 05:48:52 +04:00
printf("%s ", pw->pw_name);
else
#endif
printf("%u ", (unsigned) dp->di_uid);
2000-05-23 05:48:52 +04:00
printf("MODE=%o\n", dp->di_mode);
if (preen)
printf("%s: ", cdevname());
printf("SIZE=%llu ", (unsigned long long) dp->di_size);
printf("MTIME=%s ", print_mtime(dp->di_mtime));
2000-05-23 05:48:52 +04:00
}
}
void
2005-06-27 06:49:32 +04:00
blkerror(ino_t ino, const char *type, daddr_t blk)
{
2005-08-19 06:07:18 +04:00
pfatal("%lld %s I=%llu", (long long) blk, type,
(unsigned long long)ino);
2000-05-23 05:48:52 +04:00
printf("\n");
if (exitonfail)
exit(1);
switch (statemap[ino]) {
2000-05-23 05:48:52 +04:00
case FSTATE:
statemap[ino] = FCLEAR;
return;
2000-05-23 05:48:52 +04:00
case DSTATE:
statemap[ino] = DCLEAR;
return;
2000-05-23 05:48:52 +04:00
case FCLEAR:
case DCLEAR:
return;
2000-05-23 05:48:52 +04:00
default:
err(EEXIT, "BAD STATE %d TO BLKERR\n", statemap[ino]);
2000-05-23 05:48:52 +04:00
/* NOTREACHED */
}
}
/*
* allocate an unused inode
*/
ino_t
2000-05-23 05:48:52 +04:00
allocino(ino_t request, int type)
{
ino_t ino;
struct ufs1_dinode *dp;
time_t t;
struct uvnode *vp;
struct ubuf *bp;
2000-05-23 05:48:52 +04:00
if (request == 0)
request = ROOTINO;
else if (statemap[request] != USTATE)
return (0);
for (ino = request; ino < maxino; ino++)
if (statemap[ino] == USTATE)
break;
if (ino == maxino)
Various improvements to fsck_lfs, to wit: * Add lfs_balloc capability to the lfs library. * Extend the Ifile if we run out of free inodes when creating lost+found. * Don't roll forward if we have allocated a lost+found, to avoid conflicts when adding new files in roll-forward. * Make some messages slightly more verbose (e.g. include inode number, and use pwarn() instead of printf() so the messages include the device name when preening). * Change superblock detection/avoidance to use the offset table in the primary superblock, rather than looking at the contents. * Be more verbose about various operations when passed the -d flag, especially roll-forward. * Be more careful about dirops during roll forward, since the cleaner can sometimes write blocks from dirop vnodes. Detect and avoid this problem. * Always check the free list, even if given -i; if we're going to write it we have to check it first. * Mark inodes dirty when blocks are found during roll forward, so the inodes are written with the new block locations. * Update size of inodes if blocks beyond EOF are found during roll forward. * Fix segment accounting for blocks and inodes found during roll forward. * Report statistics on roll forward: how many new/deleted/moved files and how many updated blocks (or "nothing new"). * Don't care if the device being checked is really a device, if we have been passed the -f flag (to facilitate automated testing). * When writing to the disk, use the current time in the segment headers rathern than time 0. * When passed the -i flag, locate the partial segment containing the Ifile inode and use that to calculate lfs_offset, lfs_curseg, lfs_nextseg. (Again for automated testing.)
2006-07-19 03:37:13 +04:00
extend_ifile(fs);
2000-05-23 05:48:52 +04:00
switch (type & IFMT) {
case IFDIR:
statemap[ino] = DSTATE;
break;
case IFREG:
case IFLNK:
statemap[ino] = FSTATE;
break;
default:
return (0);
}
vp = lfs_valloc(fs, ino);
if (vp == NULL)
return (0);
dp = (VTOI(vp)->i_din.ffs1_din);
bp = getblk(vp, 0, fs->lfs_fsize);
VOP_BWRITE(bp);
2000-05-23 05:48:52 +04:00
dp->di_mode = type;
(void) time(&t);
2000-05-23 05:48:52 +04:00
dp->di_atime = t;
dp->di_mtime = dp->di_ctime = dp->di_atime;
dp->di_size = fs->lfs_fsize;
dp->di_blocks = btofsb(fs, fs->lfs_fsize);
2000-05-23 05:48:52 +04:00
n_files++;
inodirty(VTOI(vp));
typemap[ino] = IFTODT(type);
2000-05-23 05:48:52 +04:00
return (ino);
}
/*
* deallocate an inode
*/
void
2000-05-23 05:48:52 +04:00
freeino(ino_t ino)
{
struct inodesc idesc;
struct uvnode *vp;
2000-05-23 05:48:52 +04:00
memset(&idesc, 0, sizeof(struct inodesc));
idesc.id_type = ADDR;
idesc.id_func = pass4check;
idesc.id_number = ino;
vp = vget(fs, ino);
(void) ckinode(VTOD(vp), &idesc);
clearinode(ino);
2000-05-23 05:48:52 +04:00
statemap[ino] = USTATE;
vnode_destroy(vp);
2000-05-23 05:48:52 +04:00
n_files--;
}