2014-04-05 16:32:27 +04:00
|
|
|
/* $NetBSD: inode.c,v 1.71 2014/04/05 12:32:27 justin Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-06-08 22:57:30 +04: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.
|
2003-08-07 14:04:11 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-09-14 18:36:29 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 20:44:43 +04:00
|
|
|
static char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2014-04-05 16:32:27 +04:00
|
|
|
__RCSID("$NetBSD: inode.c,v 1.71 2014/04/05 12:32:27 justin Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#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>
|
2011-03-06 20:08:10 +03:00
|
|
|
#include <sys/stat.h>
|
1997-09-16 20:44:43 +04:00
|
|
|
|
1994-06-08 22:57:30 +04:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
1998-03-18 20:01:23 +03:00
|
|
|
#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
|
|
|
|
1998-04-01 19:38:58 +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
|
1994-12-05 23:15:31 +03:00
|
|
|
#include <stdio.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-04-01 19:38:58 +04:00
|
|
|
#include <time.h>
|
1994-12-05 23:15:31 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "fsck.h"
|
1996-09-28 02:45:10 +04:00
|
|
|
#include "fsutil.h"
|
1994-12-05 23:15:31 +03:00
|
|
|
#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);
|
2003-04-02 14:39:19 +04:00
|
|
|
static void swap_dinode1(union dinode *, int);
|
|
|
|
static void swap_dinode2(union dinode *, int);
|
1994-12-05 23:15:31 +03:00
|
|
|
|
|
|
|
int
|
2005-01-19 20:33:58 +03:00
|
|
|
ckinode(union dinode *dp, struct inodesc *idesc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
int ret, offset, i;
|
|
|
|
union dinode dino;
|
1998-03-18 20:01:23 +03:00
|
|
|
u_int64_t sizepb;
|
|
|
|
int64_t remsize;
|
2003-04-02 14:39:19 +04:00
|
|
|
daddr_t ndb;
|
1994-06-08 22:57:30 +04:00
|
|
|
mode_t mode;
|
1996-10-12 00:15:46 +04:00
|
|
|
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;
|
2003-04-02 14:39:19 +04:00
|
|
|
idesc->id_filesize = iswap64(DIP(dp, size));
|
|
|
|
mode = iswap16(DIP(dp, mode)) & IFMT;
|
1994-06-08 22:57:30 +04:00
|
|
|
if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
|
1998-03-18 20:01:23 +03:00
|
|
|
(idesc->id_filesize < sblock->fs_maxsymlinklen ||
|
2002-09-29 00:11:05 +04:00
|
|
|
(isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
|
2003-04-02 14:39:19 +04:00
|
|
|
(sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
|
1993-03-21 12:45:37 +03:00
|
|
|
return (KEEPON);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
dino.dp2 = dp->dp2;
|
|
|
|
else
|
|
|
|
dino.dp1 = dp->dp1;
|
|
|
|
ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
|
2013-01-22 13:39:11 +04:00
|
|
|
for (i = 0; i < UFS_NDADDR; i++) {
|
1998-03-18 20:01:23 +03:00
|
|
|
if (--ndb == 0 &&
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
(offset = ffs_blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
idesc->id_numfrags =
|
2013-06-23 11:28:36 +04:00
|
|
|
ffs_numfrags(sblock, ffs_fragroundup(sblock, offset));
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1998-03-18 20:01:23 +03:00
|
|
|
idesc->id_numfrags = sblock->fs_frag;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (DIP(&dino, db[i]) == 0) {
|
1996-10-12 00:15:46 +04:00
|
|
|
if (idesc->id_type == DATA && ndb >= 0) {
|
|
|
|
/* An empty block in a directory XXX */
|
1998-03-18 20:01:23 +03:00
|
|
|
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",
|
1996-10-12 00:15:46 +04:00
|
|
|
pathbuf);
|
|
|
|
if (reply("ADJUST LENGTH") == 1) {
|
|
|
|
dp = ginode(idesc->id_number);
|
2006-04-21 19:00:49 +04:00
|
|
|
DIP_SET(dp, size, iswap64(i *
|
|
|
|
sblock->fs_bsize));
|
1996-10-12 00:15:46 +04:00
|
|
|
printf(
|
|
|
|
"YOU MUST RERUN FSCK AFTERWARDS\n");
|
|
|
|
rerun = 1;
|
|
|
|
inodirty();
|
|
|
|
}
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
1996-10-12 00:15:46 +04:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
|
|
|
|
else
|
|
|
|
idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
|
2004-05-25 18:54:55 +04:00
|
|
|
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);
|
|
|
|
}
|
1998-03-18 20:01:23 +03:00
|
|
|
idesc->id_numfrags = sblock->fs_frag;
|
2013-01-22 13:39:11 +04:00
|
|
|
remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * UFS_NDADDR;
|
1998-03-18 20:01:23 +03:00
|
|
|
sizepb = sblock->fs_bsize;
|
2013-01-22 13:39:11 +04:00
|
|
|
for (i = 0; i < UFS_NIADDR; i++) {
|
2003-04-02 14:39:19 +04:00
|
|
|
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);
|
1996-10-12 00:15:46 +04:00
|
|
|
} else {
|
|
|
|
if (idesc->id_type == DATA && remsize > 0) {
|
|
|
|
/* An empty block in a directory XXX */
|
1998-03-18 20:01:23 +03:00
|
|
|
markclean = 0;
|
2003-07-13 12:16:15 +04:00
|
|
|
getpathname(pathbuf, sizeof(pathbuf),
|
|
|
|
idesc->id_number, idesc->id_number);
|
1996-10-12 00:15:46 +04:00
|
|
|
pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
|
|
|
|
pathbuf);
|
|
|
|
if (reply("ADJUST LENGTH") == 1) {
|
|
|
|
dp = ginode(idesc->id_number);
|
2006-04-21 19:00:49 +04:00
|
|
|
DIP_SET(dp, size,
|
2003-04-02 14:39:19 +04:00
|
|
|
iswap64(iswap64(DIP(dp, size))
|
2006-04-21 19:00:49 +04:00
|
|
|
- remsize));
|
1996-10-12 00:15:46 +04:00
|
|
|
remsize = 0;
|
|
|
|
printf(
|
|
|
|
"YOU MUST RERUN FSCK AFTERWARDS\n");
|
|
|
|
rerun = 1;
|
|
|
|
inodirty();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
sizepb *= FFS_NINDIR(sblock);
|
1994-06-08 22:57:30 +04:00
|
|
|
remsize -= sizepb;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return (KEEPON);
|
|
|
|
}
|
|
|
|
|
1996-09-23 20:18:31 +04:00
|
|
|
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
|
|
|
{
|
1997-09-14 18:36:29 +04: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];
|
1996-10-12 00:15:46 +04:00
|
|
|
char pathbuf[MAXPATHLEN + 1];
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-05-25 18:54:55 +04: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);
|
1998-03-18 20:01:23 +03:00
|
|
|
bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
ilevel--;
|
1998-03-18 20:01:23 +03:00
|
|
|
for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
sizepb *= FFS_NINDIR(sblock);
|
|
|
|
if (howmany(isize, sizepb) > (size_t)FFS_NINDIR(sblock))
|
|
|
|
nif = FFS_NINDIR(sblock);
|
1996-05-21 20:58:12 +04:00
|
|
|
else
|
|
|
|
nif = howmany(isize, sizepb);
|
1998-03-18 20:01:23 +03:00
|
|
|
if (do_blkswap) { /* swap byte order of the whole blk */
|
2003-04-02 14:39:19 +04:00
|
|
|
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] =
|
2003-04-08 18:59:52 +04:00
|
|
|
bswap32(bp->b_un.b_indir1[i]);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
1998-03-18 20:01:23 +03:00
|
|
|
dirty(bp);
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
}
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
if (idesc->id_func == pass1check && nif < FFS_NINDIR(sblock)) {
|
|
|
|
for (i = nif; i < FFS_NINDIR(sblock); i++) {
|
2003-04-02 14:39:19 +04:00
|
|
|
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),
|
2005-08-19 06:07:18 +04:00
|
|
|
"PARTIALLY TRUNCATED INODE I=%llu",
|
|
|
|
(unsigned long long)idesc->id_number);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (dofix(idesc, buf)) {
|
2006-04-21 19:00:49 +04:00
|
|
|
IBLK_SET(bp, i, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
dirty(bp);
|
1998-03-18 20:01:23 +03:00
|
|
|
} else
|
2008-07-08 12:14:37 +04:00
|
|
|
markclean = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
flush(fswritefd, bp);
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
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]);
|
1994-06-08 22:57:30 +04:00
|
|
|
if (ilevel == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
n = (*func)(idesc);
|
1994-06-08 22:57:30 +04:00
|
|
|
else
|
|
|
|
n = iblock(idesc, ilevel, isize);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (n & STOP) {
|
|
|
|
bp->b_flags &= ~B_INUSE;
|
|
|
|
return (n);
|
|
|
|
}
|
1996-10-12 00:15:46 +04:00
|
|
|
} else {
|
|
|
|
if (idesc->id_type == DATA && isize > 0) {
|
|
|
|
/* An empty block in a directory XXX */
|
2008-07-08 12:14:37 +04:00
|
|
|
markclean = 0;
|
2003-07-13 12:16:15 +04:00
|
|
|
getpathname(pathbuf, sizeof(pathbuf),
|
|
|
|
idesc->id_number, idesc->id_number);
|
1996-10-12 00:15:46 +04:00
|
|
|
pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
|
|
|
|
pathbuf);
|
|
|
|
if (reply("ADJUST LENGTH") == 1) {
|
|
|
|
dp = ginode(idesc->id_number);
|
2006-04-21 19:00:49 +04:00
|
|
|
DIP_SET(dp, size,
|
2003-04-02 14:39:19 +04:00
|
|
|
iswap64(iswap64(DIP(dp, size))
|
2006-04-21 19:00:49 +04:00
|
|
|
- isize));
|
1996-10-12 00:15:46 +04:00
|
|
|
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
|
|
|
}
|
1994-06-08 22:57:30 +04: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.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
2005-01-19 20:33:58 +03:00
|
|
|
chkrange(daddr_t blk, int cnt)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
int c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-04-27 03:46:22 +04:00
|
|
|
if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
|
|
|
|
cnt - 1 > maxfsblock - blk)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (1);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (cnt > sblock->fs_frag ||
|
2013-06-24 02:03:34 +04:00
|
|
|
ffs_fragnum(sblock, blk) + cnt > sblock->fs_frag) {
|
2003-04-02 14:39:19 +04:00
|
|
|
if (debug)
|
|
|
|
printf("bad size: blk %lld, offset %d, size %d\n",
|
2013-06-24 02:03:34 +04:00
|
|
|
(long long)blk, (int)ffs_fragnum(sblock, blk), cnt);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
1998-03-18 20:01:23 +03:00
|
|
|
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) {
|
2003-01-25 00:55:02 +03:00
|
|
|
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 {
|
1998-03-18 20:01:23 +03:00
|
|
|
if ((blk + cnt) > cgbase(sblock, c+1)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug) {
|
2003-01-25 00:55:02 +03:00
|
|
|
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.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *
|
2005-01-19 20:33:58 +03:00
|
|
|
ginode(ino_t inumber)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-01-25 00:55:02 +03:00
|
|
|
daddr_t iblk;
|
1998-10-23 05:13:33 +04:00
|
|
|
int blkoff;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2013-01-22 13:39:11 +04:00
|
|
|
if (inumber < UFS_ROOTINO || inumber > maxino)
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("bad inode number %llu to ginode",
|
2005-08-19 06:07:18 +04:00
|
|
|
(unsigned long long)inumber);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (startinum == 0 ||
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
inumber < startinum || inumber >= startinum + FFS_INOPB(sblock)) {
|
1998-03-18 20:01:23 +03:00
|
|
|
iblk = ino_to_fsba(sblock, inumber);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (pbp != 0)
|
|
|
|
pbp->b_flags &= ~B_INUSE;
|
1998-03-18 20:01:23 +03:00
|
|
|
pbp = getdatablk(iblk, sblock->fs_bsize);
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
startinum = (inumber / FFS_INOPB(sblock)) * FFS_INOPB(sblock);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2) {
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
blkoff = (inumber % FFS_INOPB(sblock)) * DINODE2_SIZE;
|
2003-04-02 14:39:19 +04:00
|
|
|
return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
|
|
|
|
}
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
blkoff = (inumber % FFS_INOPB(sblock)) * DINODE1_SIZE;
|
2003-04-02 14:39:19 +04:00
|
|
|
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;
|
2004-01-03 20:56:21 +03:00
|
|
|
int32_t maxsymlinklen = sblock->fs_maxsymlinklen;
|
|
|
|
if (isappleufs)
|
|
|
|
maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
dp1 = (struct ufs1_dinode *)&dp->dp1;
|
|
|
|
for (i = 0; i < n; i++, dp1++) {
|
|
|
|
ffs_dinode1_swap(dp1, dp1);
|
2004-01-03 13:11:41 +03:00
|
|
|
if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
|
2004-01-03 20:27:35 +03:00
|
|
|
doinglevel2 ||
|
2004-01-03 20:56:21 +03:00
|
|
|
(maxsymlinklen < 0) ||
|
2009-04-11 11:31:21 +04:00
|
|
|
(iswap64(dp1->di_size) > (uint64_t)maxsymlinklen)) {
|
2014-04-05 16:32:27 +04:00
|
|
|
for (j = 0; j < UFS_NDADDR; j++)
|
2003-04-02 14:39:19 +04:00
|
|
|
dp1->di_db[j] = bswap32(dp1->di_db[j]);
|
2014-04-05 16:32:27 +04:00
|
|
|
for (j = 0; j < UFS_NIADDR; j++)
|
|
|
|
dp1->di_ib[j] = bswap32(dp1->di_ib[j]);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-04-05 16:32:27 +04:00
|
|
|
for (j = 0; j < UFS_NXADDR; j++)
|
2003-04-02 14:39:19 +04:00
|
|
|
dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
|
2014-04-05 16:32:27 +04:00
|
|
|
for (j = 0; j < UFS_NDADDR; j++)
|
|
|
|
dp2->di_db[j] = bswap64(dp2->di_db[j]);
|
|
|
|
for (j = 0; j < UFS_NIADDR; j++)
|
|
|
|
dp2->di_ib[j] = bswap64(dp2->di_ib[j]);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
ino_t nextino, lastinum, lastvalidinum;
|
1993-03-21 12:45:37 +03:00
|
|
|
long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *inodebuf;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-04-02 14:39:19 +04: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;
|
2003-01-25 00:55:02 +03:00
|
|
|
daddr_t dblk;
|
2003-04-02 14:39:19 +04:00
|
|
|
static union dinode *dp;
|
|
|
|
union dinode *ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-05-01 00:24:32 +04:00
|
|
|
if (inumber != nextino++ || inumber > lastvalidinum)
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("bad inode number %llu to nextinode",
|
2005-08-19 06:07:18 +04:00
|
|
|
(unsigned long long)inumber);
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (inumber >= lastinum) {
|
|
|
|
readcnt++;
|
2013-06-23 06:06:04 +04:00
|
|
|
dblk = FFS_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;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
(void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
|
1998-03-18 20:01:23 +03:00
|
|
|
if (doswap) {
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
swap_dinode2(inodebuf, lastinum - inumber);
|
|
|
|
else
|
|
|
|
swap_dinode1(inodebuf, lastinum - inumber);
|
1998-03-18 20:01:23 +03:00
|
|
|
bwrite(fswritefd, (char *)inodebuf, dblk, size);
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
dp = (union dinode *)inodebuf;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
ret = dp;
|
|
|
|
dp = (union dinode *)
|
|
|
|
((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
|
|
|
|
return ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
setinodebuf(ino_t inum)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (inum % sblock->fs_ipg != 0)
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("bad inode number %llu to setinodebuf",
|
2005-08-19 06:07:18 +04:00
|
|
|
(unsigned long long)inum);
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
lastvalidinum = inum + sblock->fs_ipg - 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
startinum = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
nextino = inum;
|
|
|
|
lastinum = inum;
|
1993-03-21 12:45:37 +03:00
|
|
|
readcnt = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (inodebuf != NULL)
|
|
|
|
return;
|
2013-06-23 11:28:36 +04:00
|
|
|
inobufsize = ffs_blkroundup(sblock, INOBUFSIZE);
|
2003-04-02 14:39:19 +04:00
|
|
|
fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
|
1998-03-18 20:01:23 +03:00
|
|
|
readpercg = sblock->fs_ipg / fullcnt;
|
|
|
|
partialcnt = sblock->fs_ipg % fullcnt;
|
2003-04-02 14:39:19 +04:00
|
|
|
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 &&
|
2003-04-02 14:39:19 +04:00
|
|
|
(inodebuf = malloc((unsigned)inobufsize)) == NULL)
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("Cannot allocate space for inode buffer");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +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.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
cacheino(union dinode *dp, ino_t inumber)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
struct inoinfo *inp;
|
2003-09-19 12:35:15 +04:00
|
|
|
struct inoinfo **inpp, **ninpsort;
|
2009-04-11 11:31:21 +04:00
|
|
|
unsigned int i, blks, extra;
|
2003-04-02 14:39:19 +04:00
|
|
|
int64_t size;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
size = iswap64(DIP(dp, size));
|
|
|
|
blks = howmany(size, sblock->fs_bsize);
|
2013-01-22 13:39:11 +04:00
|
|
|
if (blks > UFS_NDADDR)
|
|
|
|
blks = UFS_NDADDR + UFS_NIADDR;
|
2007-04-12 09:19:18 +04:00
|
|
|
if (blks > 0)
|
|
|
|
extra = (blks - 1) * sizeof (int64_t);
|
|
|
|
else
|
|
|
|
extra = 0;
|
|
|
|
inp = malloc(sizeof(*inp) + extra);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (inp == NULL)
|
|
|
|
return;
|
2003-04-02 14:39:19 +04:00
|
|
|
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;
|
2013-01-22 13:39:11 +04:00
|
|
|
if (inumber == UFS_ROOTINO)
|
|
|
|
inp->i_parent = UFS_ROOTINO;
|
1995-03-20 03:40:10 +03:00
|
|
|
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;
|
2003-04-02 14:39:19 +04:00
|
|
|
inp->i_isize = size;
|
|
|
|
inp->i_numblks = blks;
|
2013-01-22 13:39:11 +04:00
|
|
|
for (i = 0; i < (blks < UFS_NDADDR ? blks : UFS_NDADDR); i++)
|
2003-04-02 14:39:19 +04:00
|
|
|
inp->i_blks[i] = DIP(dp, db[i]);
|
2013-01-22 13:39:11 +04:00
|
|
|
if (blks > UFS_NDADDR)
|
|
|
|
for (i = 0; i < UFS_NIADDR; i++)
|
|
|
|
inp->i_blks[UFS_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)
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("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
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
struct inoinfo *inp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-04-02 14:39:19 +04: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);
|
|
|
|
}
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("cannot find inode %llu", (unsigned long long)inumber);
|
1993-03-21 12:45:37 +03:00
|
|
|
return ((struct inoinfo *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up all the inode cache structure.
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
inocleanup(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04: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;
|
|
|
|
}
|
1994-06-08 22:57:30 +04:00
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
inodirty(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
dirty(pbp);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
dp = ginode(idesc->id_number);
|
|
|
|
if (flag == 1) {
|
|
|
|
pwarn("%s %s", type,
|
2003-04-02 14:39:19 +04:00
|
|
|
(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--;
|
2011-03-06 20:08:10 +03:00
|
|
|
/*
|
|
|
|
* ckinode will call id_func (actually always pass4check)
|
|
|
|
* which will update the block count
|
|
|
|
*/
|
|
|
|
if (idesc->id_type != SNAP)
|
|
|
|
update_uquot(idesc->id_number,
|
|
|
|
idesc->id_uid, idesc->id_gid, 0, -1);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)ckinode(dp, idesc);
|
|
|
|
clearinode(dp);
|
2003-04-02 14:39:19 +04:00
|
|
|
inoinfo(idesc->id_number)->ino_state = USTATE;
|
1993-03-21 12:45:37 +03:00
|
|
|
inodirty();
|
1998-03-18 20:01:23 +03:00
|
|
|
} else
|
2008-07-08 12:14:37 +04:00
|
|
|
markclean = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
2005-01-19 20:33:58 +03:00
|
|
|
findname(struct inodesc *idesc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04: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
|
|
|
|
2003-04-02 14:39:19 +04: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);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
int
|
2005-01-19 20:33:58 +03:00
|
|
|
findino(struct inodesc *idesc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-14 18:36:29 +04: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 &&
|
2013-01-22 13:39:11 +04:00
|
|
|
iswap32(dirp->d_ino) >= UFS_ROOTINO && iswap32(dirp->d_ino) <= maxino) {
|
1998-03-18 20:01:23 +03:00
|
|
|
idesc->id_parent = iswap32(dirp->d_ino);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (STOP|FOUND);
|
|
|
|
}
|
|
|
|
return (KEEPON);
|
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
int
|
2005-01-19 20:33:58 +03:00
|
|
|
clearentry(struct inodesc *idesc)
|
2003-04-02 14:39:19 +04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
pinode(ino_t ino)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct passwd *pw;
|
|
|
|
|
2005-08-19 06:07:18 +04:00
|
|
|
printf(" I=%llu ", (unsigned long long)ino);
|
2013-01-22 13:39:11 +04:00
|
|
|
if (ino < UFS_ROOTINO || ino > maxino)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
dp = ginode(ino);
|
|
|
|
printf(" OWNER=");
|
1994-04-18 10:08:27 +04:00
|
|
|
#ifndef SMALL
|
2008-10-09 20:56:23 +04:00
|
|
|
if (Uflag && (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
|
2003-04-02 14:39:19 +04:00
|
|
|
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)
|
1996-09-23 20:18:31 +04:00
|
|
|
printf("%s: ", cdevname());
|
2003-04-02 14:39:19 +04:00
|
|
|
printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
|
2010-02-05 02:55:42 +03:00
|
|
|
printf("MTIME=%s ", print_mtime(iswap32(DIP(dp, mtime))));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
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
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
struct inostat *info;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-08-19 06:07:18 +04:00
|
|
|
pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\n");
|
2003-04-02 14:39:19 +04:00
|
|
|
info = inoinfo(ino);
|
|
|
|
switch (info->ino_state) {
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
case FSTATE:
|
2003-04-02 14:39:19 +04:00
|
|
|
info->ino_state = FCLEAR;
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case DSTATE:
|
2003-04-02 14:39:19 +04:00
|
|
|
info->ino_state = DCLEAR;
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case FCLEAR:
|
|
|
|
case DCLEAR:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2008-02-24 00:41:47 +03:00
|
|
|
errexit("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
|
|
|
{
|
1997-09-14 18:36:29 +04:00
|
|
|
ino_t ino;
|
2003-04-02 14:39:19 +04:00
|
|
|
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;
|
2011-03-06 20:08:10 +03:00
|
|
|
int nfrags;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (request == 0)
|
2013-01-22 13:39:11 +04:00
|
|
|
request = UFS_ROOTINO;
|
2003-04-02 14:39:19 +04:00
|
|
|
else if (inoinfo(request)->ino_state != USTATE)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
2003-04-02 14:39:19 +04:00
|
|
|
for (ino = request; ino < maxino; ino++) {
|
|
|
|
info = inoinfo(ino);
|
|
|
|
if (info->ino_state == USTATE)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
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);
|
2004-10-08 21:33:52 +04:00
|
|
|
/* If necessary, extend the inoinfo array. grow exponentially */
|
2009-04-11 11:31:21 +04:00
|
|
|
if ((ino % sblock->fs_ipg) >= (uint64_t)inostathead[cg].il_numalloced) {
|
2004-10-08 21:33:52 +04:00
|
|
|
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))
|
2003-04-02 14:39:19 +04:00
|
|
|
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--;
|
2011-03-06 20:08:10 +03:00
|
|
|
sblock->fs_cstotal.cs_nifree--;
|
|
|
|
sblock->fs_cs(fs, cg).cs_nifree--;
|
|
|
|
sbdirty();
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (type & IFMT) {
|
|
|
|
case IFDIR:
|
2003-04-02 14:39:19 +04:00
|
|
|
info->ino_state = DSTATE;
|
1999-11-15 22:18:24 +03:00
|
|
|
cgp->cg_cs.cs_ndir++;
|
2011-03-06 20:08:10 +03:00
|
|
|
nfrags = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case IFREG:
|
2011-03-06 20:08:10 +03:00
|
|
|
info->ino_state = FSTATE;
|
|
|
|
nfrags = sblock->fs_frag;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case IFLNK:
|
2003-04-02 14:39:19 +04:00
|
|
|
info->ino_state = FSTATE;
|
2011-03-06 20:08:10 +03:00
|
|
|
nfrags = 1;
|
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);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2) {
|
|
|
|
dp2 = &dp->dp2;
|
2011-03-06 20:08:10 +03:00
|
|
|
dp2->di_db[0] = iswap64(allocblk(nfrags));
|
2003-04-02 14:39:19 +04:00
|
|
|
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;
|
2013-06-23 11:28:36 +04:00
|
|
|
dp2->di_size = iswap64(ffs_lfragtosize(sblock, nfrags));
|
|
|
|
dp2->di_blocks = iswap64(btodb(ffs_lfragtosize(sblock, nfrags)));
|
2003-04-02 14:39:19 +04:00
|
|
|
} else {
|
|
|
|
dp1 = &dp->dp1;
|
2011-03-06 20:08:10 +03:00
|
|
|
dp1->di_db[0] = iswap32(allocblk(nfrags));
|
2003-04-02 14:39:19 +04:00
|
|
|
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;
|
2013-06-23 11:28:36 +04:00
|
|
|
dp1->di_size = iswap64(ffs_lfragtosize(sblock, nfrags));
|
|
|
|
dp1->di_blocks = iswap32(btodb(ffs_lfragtosize(sblock, nfrags)));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
n_files++;
|
|
|
|
inodirty();
|
1994-06-08 22:57:30 +04:00
|
|
|
if (newinofmt)
|
2003-04-02 14:39:19 +04:00
|
|
|
info->ino_type = IFTODT(type);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (ino);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deallocate an inode
|
|
|
|
*/
|
1994-12-05 23:15:31 +03:00
|
|
|
void
|
2005-01-19 20:33:58 +03:00
|
|
|
freeino(ino_t ino)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct inodesc idesc;
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
2011-03-06 20:08:10 +03:00
|
|
|
struct cg *cgp = cgrp;
|
|
|
|
int cg;
|
|
|
|
|
|
|
|
cg = ino_to_cg(sblock, ino);
|
|
|
|
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);
|
|
|
|
if (!cg_chkmagic(cgp, 0)) {
|
|
|
|
pwarn("CG %d: FREEINO: BAD MAGIC NUMBER\n", cg);
|
|
|
|
cgp = NULL;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-23 18:26:58 +04:00
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
1993-03-21 12:45:37 +03:00
|
|
|
idesc.id_func = pass4check;
|
|
|
|
idesc.id_number = ino;
|
|
|
|
dp = ginode(ino);
|
2011-03-06 20:08:10 +03:00
|
|
|
idesc.id_uid = iswap32(DIP(dp, uid));
|
|
|
|
idesc.id_gid = iswap32(DIP(dp, gid));
|
|
|
|
if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT)
|
|
|
|
idesc.id_type = SNAP;
|
|
|
|
else
|
|
|
|
idesc.id_type = ADDR;
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)ckinode(dp, &idesc);
|
|
|
|
clearinode(dp);
|
|
|
|
inodirty();
|
2003-04-02 14:39:19 +04:00
|
|
|
inoinfo(ino)->ino_state = USTATE;
|
2011-03-06 20:08:10 +03:00
|
|
|
if (idesc.id_type != SNAP)
|
|
|
|
update_uquot(idesc.id_number,
|
|
|
|
idesc.id_uid, idesc.id_gid, 0, -1);
|
1993-03-21 12:45:37 +03:00
|
|
|
n_files--;
|
2011-03-06 20:08:10 +03:00
|
|
|
if (cgp) {
|
|
|
|
clrbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
|
|
|
|
cgp->cg_cs.cs_nifree++;
|
|
|
|
sblock->fs_cstotal.cs_nifree++;
|
|
|
|
sblock->fs_cs(fs, cg).cs_nifree++;
|
|
|
|
sbdirty();
|
|
|
|
cgdirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read a data block from inode */
|
|
|
|
ssize_t
|
|
|
|
readblk(union dinode *dp, off_t offset, struct bufarea **bp)
|
|
|
|
{
|
2013-06-23 11:28:36 +04:00
|
|
|
daddr_t blkno = ffs_lblkno(sblock, offset);
|
2011-03-06 20:08:10 +03:00
|
|
|
daddr_t iblkno;
|
|
|
|
int type = IFMT & iswap16(DIP(dp, mode));
|
|
|
|
ssize_t filesize = iswap64(DIP(dp, size));
|
|
|
|
int ilevel;
|
|
|
|
daddr_t nblks;
|
|
|
|
const daddr_t naddrperblk = sblock->fs_bsize /
|
|
|
|
(is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
|
|
|
|
struct bufarea *ibp;
|
|
|
|
|
|
|
|
*bp = NULL;
|
|
|
|
offset &= ~(sblock->fs_bsize - 1);
|
|
|
|
|
|
|
|
if (type != IFREG)
|
|
|
|
return 0;
|
|
|
|
if (offset >= filesize)
|
|
|
|
return 0; /* short read */
|
2013-01-22 13:39:11 +04:00
|
|
|
if (blkno < UFS_NDADDR) {
|
2011-03-06 20:08:10 +03:00
|
|
|
blkno = is_ufs2 ? iswap64(dp->dp2.di_db[blkno]) :
|
|
|
|
iswap32(dp->dp1.di_db[blkno]);
|
|
|
|
if (blkno == 0)
|
|
|
|
return 0;
|
|
|
|
*bp = getdatablk(blkno, sblock->fs_bsize);
|
|
|
|
return (bp != NULL) ? sblock->fs_bsize : 0;
|
|
|
|
}
|
2013-01-22 13:39:11 +04:00
|
|
|
blkno -= UFS_NDADDR;
|
2011-03-06 20:08:10 +03:00
|
|
|
/* find indir level */
|
|
|
|
for (ilevel = 1, nblks = naddrperblk;
|
2013-01-22 13:39:11 +04:00
|
|
|
ilevel <= UFS_NIADDR;
|
2011-03-06 20:08:10 +03:00
|
|
|
ilevel++, nblks *= naddrperblk) {
|
|
|
|
if (blkno < nblks)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
blkno -= nblks;
|
|
|
|
}
|
2013-01-22 13:39:11 +04:00
|
|
|
if (ilevel > UFS_NIADDR)
|
2011-03-06 20:08:10 +03:00
|
|
|
errexit("bad ofsset %" PRIu64 " to readblk", offset);
|
|
|
|
|
|
|
|
/* get the first indirect block */
|
|
|
|
iblkno = is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
|
|
|
|
iswap32(dp->dp1.di_ib[ilevel - 1]);
|
|
|
|
if (iblkno == 0)
|
|
|
|
return 0;
|
|
|
|
ibp = getdatablk(iblkno, sblock->fs_bsize);
|
|
|
|
/* walk indirect blocks up to the data block */
|
|
|
|
for (; ilevel >0 ; ilevel--) {
|
|
|
|
nblks = nblks / naddrperblk;
|
|
|
|
if (is_ufs2)
|
|
|
|
iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
|
|
|
|
else
|
|
|
|
iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
|
|
|
|
if (iblkno == 0)
|
|
|
|
return 0;
|
|
|
|
blkno = blkno % nblks;
|
|
|
|
ibp->b_flags &= ~B_INUSE;
|
|
|
|
ibp = getdatablk(iblkno, sblock->fs_bsize);
|
|
|
|
}
|
|
|
|
*bp = ibp;
|
|
|
|
return sblock->fs_bsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bufarea * getnewblk(daddr_t *);
|
|
|
|
static struct bufarea *
|
|
|
|
getnewblk(daddr_t *blkno)
|
|
|
|
{
|
|
|
|
struct bufarea *bp;
|
|
|
|
*blkno = allocblk(sblock->fs_frag);
|
|
|
|
if (*blkno == 0)
|
|
|
|
return NULL;
|
|
|
|
bp = getdatablk(*blkno, sblock->fs_bsize);
|
|
|
|
memset(bp->b_un.b_buf, 0, sblock->fs_bsize);
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* expand given inode by one full fs block */
|
|
|
|
struct bufarea *
|
|
|
|
expandfile(union dinode *dp)
|
|
|
|
{
|
|
|
|
uint64_t filesize = iswap64(DIP(dp, size));
|
|
|
|
daddr_t newblk, blkno, iblkno, nblks;
|
|
|
|
daddr_t di_blocks;
|
|
|
|
int ilevel;
|
|
|
|
const daddr_t naddrperblk = sblock->fs_bsize /
|
|
|
|
(is_ufs2 ? sizeof(uint64_t) : sizeof(uint32_t));
|
|
|
|
struct bufarea *ibp, *bp = NULL;
|
|
|
|
|
|
|
|
di_blocks = is_ufs2 ? iswap64(dp->dp2.di_blocks) :
|
|
|
|
iswap32(dp->dp1.di_blocks);
|
|
|
|
/* compute location of new block */
|
2013-06-23 11:28:36 +04:00
|
|
|
blkno = ffs_lblkno(sblock, filesize);
|
2011-03-06 20:08:10 +03:00
|
|
|
|
2013-01-22 13:39:11 +04:00
|
|
|
if (blkno < UFS_NDADDR) {
|
2011-03-06 20:08:10 +03:00
|
|
|
/* easy way: allocate a direct block */
|
|
|
|
if ((bp = getnewblk(&newblk)) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
di_blocks += btodb(sblock->fs_bsize);
|
|
|
|
|
|
|
|
if (is_ufs2) {
|
|
|
|
dp->dp2.di_db[blkno] = iswap64(newblk);
|
|
|
|
} else {
|
|
|
|
dp->dp1.di_db[blkno] = iswap32(newblk);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
2013-01-22 13:39:11 +04:00
|
|
|
blkno -= UFS_NDADDR;
|
2011-03-06 20:08:10 +03:00
|
|
|
/* find indir level */
|
|
|
|
for (ilevel = 1, nblks = naddrperblk;
|
2013-01-22 13:39:11 +04:00
|
|
|
ilevel <= UFS_NIADDR;
|
2011-03-06 20:08:10 +03:00
|
|
|
ilevel++, nblks *= naddrperblk) {
|
|
|
|
if (blkno < nblks)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
blkno -= nblks;
|
|
|
|
}
|
2013-01-22 13:39:11 +04:00
|
|
|
if (ilevel > UFS_NIADDR)
|
2011-03-06 20:08:10 +03:00
|
|
|
errexit("bad filesize %" PRIu64 " to expandfile", filesize);
|
|
|
|
|
|
|
|
/* get the first indirect block, allocating if needed */
|
|
|
|
if ((is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
|
|
|
|
iswap32(dp->dp1.di_ib[ilevel - 1])) == 0) {
|
|
|
|
if ((ibp = getnewblk(&newblk)) == NULL)
|
|
|
|
return 0;
|
|
|
|
di_blocks += btodb(sblock->fs_bsize);
|
|
|
|
if (is_ufs2)
|
|
|
|
dp->dp2.di_ib[ilevel - 1] = iswap64(newblk);
|
|
|
|
else
|
|
|
|
dp->dp1.di_ib[ilevel - 1] = iswap32(newblk);
|
|
|
|
} else {
|
|
|
|
ibp = getdatablk(is_ufs2 ? iswap64(dp->dp2.di_ib[ilevel - 1]) :
|
2013-12-02 22:46:52 +04:00
|
|
|
iswap32(dp->dp1.di_ib[ilevel - 1]), sblock->fs_bsize);
|
2011-03-06 20:08:10 +03:00
|
|
|
}
|
|
|
|
/* walk indirect blocks up to the data block */
|
|
|
|
for (; ilevel >0 ; ilevel--) {
|
|
|
|
nblks = nblks / naddrperblk;
|
|
|
|
if (is_ufs2)
|
|
|
|
iblkno = iswap64(ibp->b_un.b_indir2[blkno / nblks]);
|
|
|
|
else
|
|
|
|
iblkno = iswap32(ibp->b_un.b_indir1[blkno / nblks]);
|
|
|
|
if (iblkno == 0) {
|
|
|
|
if ((bp = getnewblk(&newblk)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
di_blocks += btodb(sblock->fs_bsize);
|
|
|
|
if (is_ufs2)
|
|
|
|
ibp->b_un.b_indir2[blkno / nblks] =
|
|
|
|
iswap64(newblk);
|
|
|
|
else
|
|
|
|
ibp->b_un.b_indir1[blkno / nblks] =
|
|
|
|
iswap32(newblk);
|
|
|
|
dirty(ibp);
|
|
|
|
ibp->b_flags &= ~B_INUSE;
|
|
|
|
ibp = bp;
|
|
|
|
} else {
|
|
|
|
ibp->b_flags &= ~B_INUSE;
|
|
|
|
ibp = getdatablk(iblkno, sblock->fs_bsize);
|
|
|
|
bp = NULL;
|
|
|
|
}
|
|
|
|
blkno = blkno % nblks;
|
|
|
|
}
|
|
|
|
if (bp == NULL) {
|
|
|
|
errexit("INTERNAL ERROR: "
|
|
|
|
"expandfile() failed to allocate a new block\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
filesize += sblock->fs_bsize;
|
|
|
|
if (is_ufs2) {
|
|
|
|
dp->dp2.di_size = iswap64(filesize);
|
|
|
|
dp->dp2.di_blocks = iswap64(di_blocks);
|
|
|
|
} else {
|
|
|
|
dp->dp1.di_size = iswap64(filesize);
|
|
|
|
dp->dp1.di_blocks = iswap32(di_blocks);
|
|
|
|
}
|
|
|
|
inodirty();
|
|
|
|
return bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|