2020-04-03 22:36:32 +03:00
|
|
|
/* $NetBSD: dir.c,v 1.47 2020/04/03 19:36:33 joerg Exp $ */
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2003-08-07 14:04:22 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-03-18 05:02:18 +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.
|
|
|
|
*/
|
|
|
|
|
2003-03-28 11:09:52 +03:00
|
|
|
#include <sys/types.h>
|
1999-03-18 05:02:18 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
2003-03-28 11:09:52 +03:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
#include <ufs/lfs/lfs.h>
|
2015-07-28 08:09:34 +03:00
|
|
|
#include <ufs/lfs/lfs_accessors.h>
|
2013-06-08 06:16:03 +04:00
|
|
|
#include <ufs/lfs/lfs_inode.h>
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2003-03-28 11:09:52 +03:00
|
|
|
#include <err.h>
|
1999-03-18 05:02:18 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-03-28 11:09:52 +03:00
|
|
|
#include "bufcache.h"
|
2005-09-13 08:14:17 +04:00
|
|
|
#include "lfs_user.h"
|
2003-03-28 11:09:52 +03:00
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
#include "fsck.h"
|
|
|
|
#include "fsutil.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2005-06-27 06:48:28 +04:00
|
|
|
const char *lfname = "lost+found";
|
2015-09-20 07:51:43 +03:00
|
|
|
#if 0
|
2013-06-08 06:12:56 +04:00
|
|
|
struct lfs_dirtemplate emptydir = {
|
2006-10-16 07:21:05 +04:00
|
|
|
.dot_ino = 0,
|
2013-06-08 06:14:46 +04:00
|
|
|
.dot_reclen = LFS_DIRBLKSIZ,
|
2006-10-16 07:21:05 +04:00
|
|
|
};
|
2013-06-08 06:12:56 +04:00
|
|
|
struct lfs_dirtemplate dirhead = {
|
2006-10-16 07:21:05 +04:00
|
|
|
.dot_ino = 0,
|
|
|
|
.dot_reclen = 12,
|
2013-06-08 06:12:56 +04:00
|
|
|
.dot_type = LFS_DT_DIR,
|
2006-10-16 07:21:05 +04:00
|
|
|
.dot_namlen = 1,
|
|
|
|
.dot_name = ".",
|
|
|
|
.dotdot_ino = 0,
|
2013-06-08 06:14:46 +04:00
|
|
|
.dotdot_reclen = LFS_DIRBLKSIZ - 12,
|
2013-06-08 06:12:56 +04:00
|
|
|
.dotdot_type = LFS_DT_DIR,
|
2006-10-16 07:21:05 +04:00
|
|
|
.dotdot_namlen = 2,
|
|
|
|
.dotdot_name = ".."
|
1999-03-18 05:02:18 +03:00
|
|
|
};
|
2013-06-08 06:12:56 +04:00
|
|
|
struct lfs_odirtemplate odirhead = {
|
2006-10-16 07:21:05 +04:00
|
|
|
.dot_ino = 0,
|
|
|
|
.dot_reclen = 12,
|
|
|
|
.dot_namlen = 1,
|
|
|
|
.dot_name = ".",
|
|
|
|
.dotdot_ino = 0,
|
2013-06-08 06:14:46 +04:00
|
|
|
.dotdot_reclen = LFS_DIRBLKSIZ - 12,
|
2006-10-16 07:21:05 +04:00
|
|
|
.dotdot_namlen = 2,
|
|
|
|
.dotdot_name = ".."
|
1999-03-18 05:02:18 +03:00
|
|
|
};
|
2015-09-01 09:16:58 +03:00
|
|
|
#endif
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2015-08-12 21:28:00 +03:00
|
|
|
static int expanddir(struct uvnode *, union lfs_dinode *, char *);
|
2003-03-28 11:09:52 +03:00
|
|
|
static void freedir(ino_t, ino_t);
|
2015-09-21 04:24:23 +03:00
|
|
|
static LFS_DIRHEADER *fsck_readdir(struct uvnode *, struct inodesc *);
|
2003-03-28 11:09:52 +03:00
|
|
|
static int lftempname(char *, ino_t);
|
|
|
|
static int mkentry(struct inodesc *);
|
|
|
|
static int chgino(struct inodesc *);
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Propagate connected state through the tree.
|
|
|
|
*/
|
|
|
|
void
|
2005-01-19 22:41:59 +03:00
|
|
|
propagate(void)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
struct inoinfo **inpp, *inp, *pinp;
|
1999-03-18 05:02:18 +03:00
|
|
|
struct inoinfo **inpend;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a list of children for each directory.
|
|
|
|
*/
|
|
|
|
inpend = &inpsort[inplast];
|
|
|
|
for (inpp = inpsort; inpp < inpend; inpp++) {
|
|
|
|
inp = *inpp;
|
|
|
|
if (inp->i_parent == 0 ||
|
2013-06-06 04:52:50 +04:00
|
|
|
inp->i_number == ULFS_ROOTINO)
|
1999-03-18 05:02:18 +03:00
|
|
|
continue;
|
|
|
|
pinp = getinoinfo(inp->i_parent);
|
|
|
|
inp->i_parentp = pinp;
|
|
|
|
inp->i_sibling = pinp->i_child;
|
|
|
|
pinp->i_child = inp;
|
|
|
|
}
|
2013-06-06 04:52:50 +04:00
|
|
|
inp = getinoinfo(ULFS_ROOTINO);
|
1999-03-18 05:02:18 +03:00
|
|
|
while (inp) {
|
|
|
|
statemap[inp->i_number] = DFOUND;
|
|
|
|
if (inp->i_child &&
|
|
|
|
statemap[inp->i_child->i_number] == DSTATE)
|
|
|
|
inp = inp->i_child;
|
|
|
|
else if (inp->i_sibling)
|
|
|
|
inp = inp->i_sibling;
|
|
|
|
else
|
|
|
|
inp = inp->i_parentp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan each entry in a directory block.
|
|
|
|
*/
|
|
|
|
int
|
2003-03-28 11:09:52 +03:00
|
|
|
dirscan(struct inodesc *idesc)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dp;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct ubuf *bp;
|
|
|
|
int dsize, n;
|
|
|
|
long blksiz;
|
2013-06-08 06:14:46 +04:00
|
|
|
char dbuf[LFS_DIRBLKSIZ];
|
2003-03-28 11:09:52 +03:00
|
|
|
struct uvnode *vp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
if (idesc->id_type != DATA)
|
2008-03-17 02:17:55 +03:00
|
|
|
errexit("wrong type to dirscan %d", idesc->id_type);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (idesc->id_entryno == 0 &&
|
2013-06-08 06:14:46 +04:00
|
|
|
(idesc->id_filesize & (LFS_DIRBLKSIZ - 1)) != 0)
|
|
|
|
idesc->id_filesize = roundup(idesc->id_filesize, LFS_DIRBLKSIZ);
|
2015-07-24 09:56:41 +03:00
|
|
|
blksiz = idesc->id_numfrags * lfs_sb_getfsize(fs);
|
2010-02-17 02:20:30 +03:00
|
|
|
if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc->id_filesize -= blksiz;
|
|
|
|
return (SKIP);
|
|
|
|
}
|
|
|
|
idesc->id_loc = 0;
|
2003-03-28 11:09:52 +03:00
|
|
|
|
|
|
|
vp = vget(fs, idesc->id_number);
|
|
|
|
for (dp = fsck_readdir(vp, idesc); dp != NULL;
|
|
|
|
dp = fsck_readdir(vp, idesc)) {
|
2015-09-15 17:58:05 +03:00
|
|
|
dsize = lfs_dir_getreclen(fs, dp);
|
2003-03-28 11:09:52 +03:00
|
|
|
memcpy(dbuf, dp, (size_t) dsize);
|
2015-09-21 04:24:23 +03:00
|
|
|
idesc->id_dirp = (LFS_DIRHEADER *) dbuf;
|
2003-03-28 11:09:52 +03:00
|
|
|
if ((n = (*idesc->id_func) (idesc)) & ALTERED) {
|
2015-03-29 22:35:58 +03:00
|
|
|
bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
|
2003-03-28 11:09:52 +03:00
|
|
|
memcpy(bp->b_data + idesc->id_loc - dsize, dbuf,
|
|
|
|
(size_t) dsize);
|
|
|
|
VOP_BWRITE(bp);
|
1999-03-18 05:02:18 +03:00
|
|
|
sbdirty();
|
|
|
|
}
|
2000-05-23 05:48:52 +04:00
|
|
|
if (n & STOP)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
return (idesc->id_filesize > 0 ? KEEPON : STOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get next entry in a directory.
|
|
|
|
*/
|
2015-09-21 04:24:23 +03:00
|
|
|
static LFS_DIRHEADER *
|
2003-03-28 11:09:52 +03:00
|
|
|
fsck_readdir(struct uvnode *vp, struct inodesc *idesc)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dp, *ndp;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct ubuf *bp;
|
|
|
|
long size, blksiz, fix, dploc;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2015-07-24 09:56:41 +03:00
|
|
|
blksiz = idesc->id_numfrags * lfs_sb_getfsize(fs);
|
2015-03-29 22:35:58 +03:00
|
|
|
bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
|
2013-06-08 06:14:46 +04:00
|
|
|
if (idesc->id_loc % LFS_DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc->id_loc < blksiz) {
|
2015-09-21 04:24:23 +03:00
|
|
|
dp = (LFS_DIRHEADER *) (bp->b_data + idesc->id_loc);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (dircheck(idesc, dp))
|
|
|
|
goto dpok;
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (idesc->id_fix == IGNORE)
|
|
|
|
return (0);
|
|
|
|
fix = dofix(idesc, "DIRECTORY CORRUPTED");
|
2015-03-29 22:35:58 +03:00
|
|
|
bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
|
2015-09-21 04:24:23 +03:00
|
|
|
dp = (LFS_DIRHEADER *) (bp->b_data + idesc->id_loc);
|
2015-09-15 17:58:05 +03:00
|
|
|
lfs_dir_setino(fs, dp, 0);
|
2015-09-01 09:16:58 +03:00
|
|
|
lfs_dir_settype(fs, dp, LFS_DT_UNKNOWN);
|
|
|
|
lfs_dir_setnamlen(fs, dp, 0);
|
2015-09-15 18:01:22 +03:00
|
|
|
lfs_dir_setreclen(fs, dp, LFS_DIRBLKSIZ);
|
|
|
|
/* for now at least, don't zero the old contents */
|
2015-09-15 18:02:01 +03:00
|
|
|
/*lfs_copydirname(fs, lfs_dir_nameptr(fs, dp), "", 0, LFS_DIRBLKSIZ);*/
|
|
|
|
lfs_dir_nameptr(fs, dp)[0] = '\0';
|
1999-03-18 05:02:18 +03:00
|
|
|
if (fix)
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
|
|
|
else
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
2013-06-08 06:14:46 +04:00
|
|
|
idesc->id_loc += LFS_DIRBLKSIZ;
|
|
|
|
idesc->id_filesize -= LFS_DIRBLKSIZ;
|
1999-03-18 05:02:18 +03:00
|
|
|
return (dp);
|
|
|
|
}
|
|
|
|
dpok:
|
2003-03-28 11:09:52 +03:00
|
|
|
if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) {
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
1999-03-18 05:02:18 +03:00
|
|
|
return NULL;
|
2003-03-28 11:09:52 +03:00
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
dploc = idesc->id_loc;
|
2015-09-21 04:24:23 +03:00
|
|
|
dp = (LFS_DIRHEADER *) (bp->b_data + dploc);
|
2015-09-15 17:58:05 +03:00
|
|
|
idesc->id_loc += lfs_dir_getreclen(fs, dp);
|
|
|
|
idesc->id_filesize -= lfs_dir_getreclen(fs, dp);
|
2013-06-08 06:14:46 +04:00
|
|
|
if ((idesc->id_loc % LFS_DIRBLKSIZ) == 0) {
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
2003-03-28 11:09:52 +03:00
|
|
|
return dp;
|
|
|
|
}
|
2015-09-21 04:24:23 +03:00
|
|
|
ndp = (LFS_DIRHEADER *) (bp->b_data + idesc->id_loc);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
|
|
|
|
dircheck(idesc, ndp) == 0) {
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
2013-06-08 06:14:46 +04:00
|
|
|
size = LFS_DIRBLKSIZ - (idesc->id_loc % LFS_DIRBLKSIZ);
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc->id_loc += size;
|
|
|
|
idesc->id_filesize -= size;
|
|
|
|
if (idesc->id_fix == IGNORE)
|
2003-03-28 11:09:52 +03:00
|
|
|
return 0;
|
1999-03-18 05:02:18 +03:00
|
|
|
fix = dofix(idesc, "DIRECTORY CORRUPTED");
|
2015-03-29 22:35:58 +03:00
|
|
|
bread(vp, idesc->id_lblkno, blksiz, 0, &bp);
|
2015-09-21 04:24:23 +03:00
|
|
|
dp = (LFS_DIRHEADER *) (bp->b_data + dploc);
|
2015-09-15 17:58:05 +03:00
|
|
|
lfs_dir_setreclen(fs, dp, lfs_dir_getreclen(fs, dp) + size);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (fix)
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
|
|
|
else
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
2003-03-28 11:09:52 +03:00
|
|
|
} else
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
2003-03-28 11:09:52 +03:00
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
return (dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that a directory entry is valid.
|
|
|
|
* This is a superset of the checks made in the kernel.
|
|
|
|
*/
|
|
|
|
int
|
2015-09-21 04:24:23 +03:00
|
|
|
dircheck(struct inodesc *idesc, LFS_DIRHEADER *dp)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
int size;
|
2015-09-15 18:01:22 +03:00
|
|
|
const char *cp;
|
2003-03-28 11:09:52 +03:00
|
|
|
u_char namlen, type;
|
|
|
|
int spaceleft;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2013-06-08 06:14:46 +04:00
|
|
|
spaceleft = LFS_DIRBLKSIZ - (idesc->id_loc % LFS_DIRBLKSIZ);
|
2015-09-15 17:58:05 +03:00
|
|
|
if (lfs_dir_getino(fs, dp) >= maxino ||
|
|
|
|
lfs_dir_getreclen(fs, dp) == 0 ||
|
|
|
|
lfs_dir_getreclen(fs, dp) > spaceleft ||
|
|
|
|
(lfs_dir_getreclen(fs, dp) & 0x3) != 0) {
|
2000-05-23 05:48:52 +04:00
|
|
|
pwarn("ino too large, reclen=0, reclen>space, or reclen&3!=0\n");
|
2015-09-15 17:58:05 +03:00
|
|
|
pwarn("dp->d_ino = 0x%jx\tdp->d_reclen = 0x%x\n",
|
|
|
|
(uintmax_t)lfs_dir_getino(fs, dp),
|
|
|
|
lfs_dir_getreclen(fs, dp));
|
|
|
|
pwarn("maxino = %ju\tspaceleft = 0x%x\n",
|
|
|
|
(uintmax_t)maxino, spaceleft);
|
2000-05-23 05:48:52 +04:00
|
|
|
return (0);
|
|
|
|
}
|
2015-09-15 17:58:05 +03:00
|
|
|
if (lfs_dir_getino(fs, dp) == 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (1);
|
2015-09-01 09:16:58 +03:00
|
|
|
size = LFS_DIRSIZ(fs, dp);
|
|
|
|
namlen = lfs_dir_getnamlen(fs, dp);
|
|
|
|
type = lfs_dir_gettype(fs, dp);
|
2015-09-15 17:58:05 +03:00
|
|
|
if (lfs_dir_getreclen(fs, dp) < size ||
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc->id_filesize < size ||
|
2003-03-28 11:09:52 +03:00
|
|
|
/* namlen > MAXNAMLEN || */
|
2000-05-23 05:48:52 +04:00
|
|
|
type > 15) {
|
|
|
|
printf("reclen<size, filesize<size, namlen too large, or type>15\n");
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
2000-05-23 05:48:52 +04:00
|
|
|
}
|
2015-09-15 18:02:01 +03:00
|
|
|
cp = lfs_dir_nameptr(fs, dp);
|
2015-09-15 18:01:22 +03:00
|
|
|
for (size = 0; size < namlen; size++)
|
1999-03-18 05:02:18 +03:00
|
|
|
if (*cp == '\0' || (*cp++ == '/')) {
|
2000-05-23 05:48:52 +04:00
|
|
|
printf("name contains NUL or /\n");
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
2000-05-23 05:48:52 +04:00
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
if (*cp != '\0') {
|
2000-05-23 05:48:52 +04:00
|
|
|
printf("name size misstated\n");
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
2000-05-23 05:48:52 +04:00
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-06-27 06:49:32 +04:00
|
|
|
direrror(ino_t ino, const char *errmesg)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
fileerror(ino, ino, errmesg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-06-27 06:49:32 +04:00
|
|
|
fileerror(ino_t cwd, ino_t ino, const char *errmesg)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
char pathbuf[MAXPATHLEN + 1];
|
|
|
|
struct uvnode *vp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
pwarn("%s ", errmesg);
|
|
|
|
pinode(ino);
|
|
|
|
printf("\n");
|
2006-09-01 23:52:48 +04:00
|
|
|
pwarn("PARENT=%lld\n", (long long)cwd);
|
2003-07-13 12:05:58 +04:00
|
|
|
getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
|
2013-06-06 04:52:50 +04:00
|
|
|
if (ino < ULFS_ROOTINO || ino >= maxino) {
|
1999-03-18 05:02:18 +03:00
|
|
|
pfatal("NAME=%s\n", pathbuf);
|
|
|
|
return;
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, ino);
|
|
|
|
if (vp == NULL)
|
2000-05-23 05:48:52 +04:00
|
|
|
pfatal("INO is NULL\n");
|
|
|
|
else {
|
2003-03-28 11:09:52 +03:00
|
|
|
if (ftypeok(VTOD(vp)))
|
2000-05-23 05:48:52 +04:00
|
|
|
pfatal("%s=%s\n",
|
2015-09-01 09:08:37 +03:00
|
|
|
(lfs_dino_getmode(fs, VTOI(vp)->i_din) & LFS_IFMT) == LFS_IFDIR ?
|
2003-03-28 11:09:52 +03:00
|
|
|
"DIR" : "FILE", pathbuf);
|
2000-05-23 05:48:52 +04:00
|
|
|
else
|
|
|
|
pfatal("NAME=%s\n", pathbuf);
|
|
|
|
}
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-03-28 11:09:52 +03:00
|
|
|
adjust(struct inodesc *idesc, short lcnt)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
struct uvnode *vp;
|
2015-08-12 21:28:00 +03:00
|
|
|
union lfs_dinode *dp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: (1) since lcnt is apparently a delta, rename it; (2)
|
|
|
|
* why is it a value to *subtract*? that is unnecessarily
|
|
|
|
* confusing.
|
|
|
|
*/
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, idesc->id_number);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
if (lfs_dino_getnlink(fs, dp) == lcnt) {
|
2003-03-28 11:09:52 +03:00
|
|
|
if (linkup(idesc->id_number, (ino_t) 0) == 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
clri(idesc, "UNREF", 0);
|
|
|
|
} else {
|
|
|
|
pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
|
2015-08-12 21:28:00 +03:00
|
|
|
((lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? "DIR" : "FILE"));
|
1999-03-18 05:02:18 +03:00
|
|
|
pinode(idesc->id_number);
|
|
|
|
printf(" COUNT %d SHOULD BE %d",
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_getnlink(fs, dp), lfs_dino_getnlink(fs, dp) - lcnt);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (preen) {
|
|
|
|
if (lcnt < 0) {
|
|
|
|
printf("\n");
|
|
|
|
pfatal("LINK COUNT INCREASING");
|
|
|
|
}
|
|
|
|
printf(" (ADJUSTED)\n");
|
|
|
|
}
|
|
|
|
if (preen || reply("ADJUST") == 1) {
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setnlink(fs, dp,
|
|
|
|
lfs_dino_getnlink(fs, dp) - lcnt);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-03-28 11:09:52 +03:00
|
|
|
mkentry(struct inodesc *idesc)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dirp = idesc->id_dirp;
|
2015-09-01 09:16:58 +03:00
|
|
|
unsigned namlen;
|
2015-09-15 18:01:22 +03:00
|
|
|
unsigned newreclen, oldreclen;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2015-09-15 18:01:22 +03:00
|
|
|
/* figure the length needed for id_name */
|
2015-09-01 09:16:58 +03:00
|
|
|
namlen = strlen(idesc->id_name);
|
2015-09-21 04:22:18 +03:00
|
|
|
newreclen = LFS_DIRECTSIZ(fs, namlen);
|
2015-09-15 18:01:22 +03:00
|
|
|
|
|
|
|
/* find the minimum record length for the existing name */
|
2015-09-15 17:58:05 +03:00
|
|
|
if (lfs_dir_getino(fs, dirp) != 0)
|
2015-09-15 18:01:22 +03:00
|
|
|
oldreclen = LFS_DIRSIZ(fs, dirp);
|
1999-03-18 05:02:18 +03:00
|
|
|
else
|
2015-09-15 18:01:22 +03:00
|
|
|
oldreclen = 0;
|
|
|
|
|
|
|
|
/* Can we insert here? */
|
|
|
|
if (lfs_dir_getreclen(fs, dirp) - oldreclen < newreclen)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (KEEPON);
|
2015-09-15 18:01:22 +03:00
|
|
|
|
|
|
|
/* Divide the record; all but oldreclen goes to the new record */
|
|
|
|
newreclen = lfs_dir_getreclen(fs, dirp) - oldreclen;
|
|
|
|
lfs_dir_setreclen(fs, dirp, oldreclen);
|
|
|
|
|
|
|
|
/* advance the pointer to the new record */
|
|
|
|
dirp = LFS_NEXTDIR(fs, dirp);
|
|
|
|
|
|
|
|
/* write record; ino to be entered is in id_parent */
|
2015-09-15 17:58:05 +03:00
|
|
|
lfs_dir_setino(fs, dirp, idesc->id_parent);
|
2015-09-15 18:01:22 +03:00
|
|
|
lfs_dir_setreclen(fs, dirp, newreclen);
|
2015-09-01 09:16:58 +03:00
|
|
|
lfs_dir_settype(fs, dirp, typemap[idesc->id_parent]);
|
|
|
|
lfs_dir_setnamlen(fs, dirp, namlen);
|
2015-09-15 18:02:01 +03:00
|
|
|
lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), idesc->id_name,
|
2015-09-15 18:01:38 +03:00
|
|
|
namlen, newreclen);
|
|
|
|
|
2000-05-23 05:48:52 +04:00
|
|
|
return (ALTERED | STOP);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-03-28 11:09:52 +03:00
|
|
|
chgino(struct inodesc *idesc)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dirp = idesc->id_dirp;
|
2015-09-01 09:16:58 +03:00
|
|
|
int namlen;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2015-09-01 09:16:58 +03:00
|
|
|
namlen = lfs_dir_getnamlen(fs, dirp);
|
2015-09-15 18:02:01 +03:00
|
|
|
if (memcmp(lfs_dir_nameptr(fs, dirp), idesc->id_name, namlen + 1))
|
1999-03-18 05:02:18 +03:00
|
|
|
return (KEEPON);
|
2015-09-15 17:58:05 +03:00
|
|
|
lfs_dir_setino(fs, dirp, idesc->id_parent);
|
2015-09-01 09:16:58 +03:00
|
|
|
lfs_dir_settype(fs, dirp, typemap[idesc->id_parent]);
|
2000-05-23 05:48:52 +04:00
|
|
|
return (ALTERED | STOP);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-05-23 05:48:52 +04:00
|
|
|
linkup(ino_t orphan, ino_t parentdir)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-08-12 21:28:00 +03:00
|
|
|
union lfs_dinode *dp;
|
2003-03-28 11:09:52 +03:00
|
|
|
int lostdir;
|
|
|
|
ino_t oldlfdir;
|
|
|
|
struct inodesc idesc;
|
|
|
|
char tempname[BUFSIZ];
|
|
|
|
struct uvnode *vp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, orphan);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
lostdir = (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR;
|
1999-03-18 05:02:18 +03:00
|
|
|
pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
|
|
|
|
pinode(orphan);
|
2015-08-12 21:28:00 +03:00
|
|
|
if (preen && lfs_dino_getsize(fs, dp) == 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
if (preen)
|
|
|
|
printf(" (RECONNECTED)\n");
|
2000-05-23 05:48:52 +04:00
|
|
|
else if (reply("RECONNECT") == 0)
|
|
|
|
return (0);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (lfdir == 0) {
|
2013-06-06 04:52:50 +04:00
|
|
|
dp = ginode(ULFS_ROOTINO);
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc.id_name = lfname;
|
|
|
|
idesc.id_type = DATA;
|
|
|
|
idesc.id_func = findino;
|
2013-06-06 04:52:50 +04:00
|
|
|
idesc.id_number = ULFS_ROOTINO;
|
1999-03-18 05:02:18 +03:00
|
|
|
if ((ckinode(dp, &idesc) & FOUND) != 0) {
|
|
|
|
lfdir = idesc.id_parent;
|
|
|
|
} else {
|
|
|
|
pwarn("NO lost+found DIRECTORY");
|
|
|
|
if (preen || reply("CREATE")) {
|
2013-06-06 04:52:50 +04:00
|
|
|
lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (lfdir != 0) {
|
2013-06-06 04:52:50 +04:00
|
|
|
if (makeentry(ULFS_ROOTINO, lfdir, lfname) != 0) {
|
1999-03-18 05:02:18 +03:00
|
|
|
if (preen)
|
|
|
|
printf(" (CREATED)\n");
|
|
|
|
} else {
|
2013-06-06 04:52:50 +04:00
|
|
|
freedir(lfdir, ULFS_ROOTINO);
|
1999-03-18 05:02:18 +03:00
|
|
|
lfdir = 0;
|
|
|
|
if (preen)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lfdir == 0) {
|
|
|
|
pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
|
|
|
|
printf("\n\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, lfdir);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
if ((lfs_dino_getmode(fs, dp) & LFS_IFMT) != LFS_IFDIR) {
|
1999-03-18 05:02:18 +03:00
|
|
|
pfatal("lost+found IS NOT A DIRECTORY");
|
|
|
|
if (reply("REALLOCATE") == 0)
|
|
|
|
return (0);
|
|
|
|
oldlfdir = lfdir;
|
2013-06-06 04:52:50 +04:00
|
|
|
if ((lfdir = allocdir(ULFS_ROOTINO, (ino_t) 0, lfmode)) == 0) {
|
1999-03-18 05:02:18 +03:00
|
|
|
pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2013-06-06 04:52:50 +04:00
|
|
|
if ((changeino(ULFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
|
1999-03-18 05:02:18 +03:00
|
|
|
pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
idesc.id_type = ADDR;
|
|
|
|
idesc.id_func = pass4check;
|
|
|
|
idesc.id_number = oldlfdir;
|
|
|
|
adjust(&idesc, lncntp[oldlfdir] + 1);
|
|
|
|
lncntp[oldlfdir] = 0;
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, lfdir);
|
|
|
|
dp = VTOD(vp);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
if (statemap[lfdir] != DFOUND) {
|
|
|
|
pfatal("SORRY. NO lost+found DIRECTORY\n\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
(void) lftempname(tempname, orphan);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (makeentry(lfdir, orphan, tempname) == 0) {
|
|
|
|
pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
|
|
|
|
printf("\n\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
lncntp[orphan]--;
|
|
|
|
if (lostdir) {
|
|
|
|
if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
|
2003-03-28 11:09:52 +03:00
|
|
|
parentdir != (ino_t) - 1)
|
|
|
|
(void) makeentry(orphan, lfdir, "..");
|
|
|
|
vp = vget(fs, lfdir);
|
2015-09-01 09:08:37 +03:00
|
|
|
lfs_dino_setnlink(fs, VTOI(vp)->i_din,
|
|
|
|
lfs_dino_getnlink(fs, VTOI(vp)->i_din) + 1);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
lncntp[lfdir]++;
|
2005-08-19 06:07:18 +04:00
|
|
|
pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
|
2003-03-28 11:09:52 +03:00
|
|
|
if (parentdir != (ino_t) - 1)
|
2005-08-19 06:07:18 +04:00
|
|
|
printf("PARENT WAS I=%llu\n",
|
|
|
|
(unsigned long long)parentdir);
|
1999-03-18 05:02:18 +03:00
|
|
|
if (preen == 0)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fix an entry in a directory.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-27 06:48:28 +04:00
|
|
|
changeino(ino_t dir, const char *name, ino_t newnum)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
struct inodesc idesc;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
|
|
|
idesc.id_type = DATA;
|
|
|
|
idesc.id_func = chgino;
|
|
|
|
idesc.id_number = dir;
|
|
|
|
idesc.id_fix = DONTKNOW;
|
|
|
|
idesc.id_name = name;
|
|
|
|
idesc.id_parent = newnum; /* new value for name */
|
2003-03-28 11:09:52 +03:00
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
return (ckinode(ginode(dir), &idesc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make an entry in a directory
|
|
|
|
*/
|
|
|
|
int
|
2005-06-27 06:48:28 +04:00
|
|
|
makeentry(ino_t parent, ino_t ino, const char *name)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2015-08-12 21:28:00 +03:00
|
|
|
union lfs_dinode *dp;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct inodesc idesc;
|
|
|
|
char pathbuf[MAXPATHLEN + 1];
|
|
|
|
struct uvnode *vp;
|
2015-08-12 21:28:00 +03:00
|
|
|
uint64_t size;
|
2000-05-23 05:48:52 +04:00
|
|
|
|
2013-06-06 04:52:50 +04:00
|
|
|
if (parent < ULFS_ROOTINO || parent >= maxino ||
|
|
|
|
ino < ULFS_ROOTINO || ino >= maxino)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
memset(&idesc, 0, sizeof(struct inodesc));
|
|
|
|
idesc.id_type = DATA;
|
|
|
|
idesc.id_func = mkentry;
|
|
|
|
idesc.id_number = parent;
|
|
|
|
idesc.id_parent = ino; /* this is the inode to enter */
|
|
|
|
idesc.id_fix = DONTKNOW;
|
|
|
|
idesc.id_name = name;
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, parent);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
size = lfs_dino_getsize(fs, dp);
|
|
|
|
if (size % LFS_DIRBLKSIZ) {
|
|
|
|
size = roundup(size, LFS_DIRBLKSIZ);
|
|
|
|
lfs_dino_setsize(fs, dp, size);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
if ((ckinode(dp, &idesc) & ALTERED) != 0)
|
|
|
|
return (1);
|
2003-07-13 12:05:58 +04:00
|
|
|
getpathname(pathbuf, sizeof(pathbuf), parent, parent);
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, parent);
|
|
|
|
dp = VTOD(vp);
|
|
|
|
if (expanddir(vp, dp, pathbuf) == 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
return (ckinode(dp, &idesc) & ALTERED);
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:51:43 +03:00
|
|
|
/*
|
|
|
|
* Initialize a completely empty directory block.
|
|
|
|
* (block size is LFS_DIRBLKSIZ)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
zerodirblk(void *buf)
|
|
|
|
{
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dirp;
|
2015-09-20 07:51:43 +03:00
|
|
|
|
|
|
|
dirp = buf;
|
|
|
|
lfs_dir_setino(fs, dirp, 0);
|
|
|
|
lfs_dir_setreclen(fs, dirp, LFS_DIRBLKSIZ);
|
|
|
|
lfs_dir_settype(fs, dirp, LFS_DT_UNKNOWN);
|
|
|
|
lfs_dir_setnamlen(fs, dirp, 0);
|
|
|
|
lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "", 0,
|
|
|
|
LFS_DIRBLKSIZ);
|
|
|
|
}
|
|
|
|
|
1999-03-18 05:02:18 +03:00
|
|
|
/*
|
|
|
|
* Attempt to expand the size of a directory
|
|
|
|
*/
|
|
|
|
static int
|
2015-08-12 21:28:00 +03:00
|
|
|
expanddir(struct uvnode *vp, union lfs_dinode *dp, char *name)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2005-06-08 23:09:55 +04:00
|
|
|
daddr_t lastbn;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct ubuf *bp;
|
2013-06-08 06:14:46 +04:00
|
|
|
char *cp, firstblk[LFS_DIRBLKSIZ];
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2015-08-12 21:28:00 +03:00
|
|
|
lastbn = lfs_lblkno(fs, lfs_dino_getsize(fs, dp));
|
|
|
|
if (lastbn >= ULFS_NDADDR - 1 || lfs_dino_getdb(fs, dp, lastbn) == 0 ||
|
|
|
|
lfs_dino_getsize(fs, dp) == 0)
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setdb(fs, dp, lastbn + 1, lfs_dino_getdb(fs, dp, lastbn));
|
|
|
|
lfs_dino_setdb(fs, dp, lastbn, 0);
|
2015-07-24 09:56:41 +03:00
|
|
|
bp = getblk(vp, lastbn, lfs_sb_getbsize(fs));
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setsize(fs, dp,
|
|
|
|
lfs_dino_getsize(fs, dp) + lfs_sb_getbsize(fs));
|
|
|
|
lfs_dino_setblocks(fs, dp,
|
|
|
|
lfs_dino_getblocks(fs, dp) + lfs_btofsb(fs, lfs_sb_getbsize(fs)));
|
|
|
|
bread(vp, lfs_dino_getdb(fs, dp, lastbn + 1),
|
2015-03-29 22:35:58 +03:00
|
|
|
(long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
|
2003-03-28 11:09:52 +03:00
|
|
|
if (bp->b_flags & B_ERROR)
|
1999-03-18 05:02:18 +03:00
|
|
|
goto bad;
|
2013-06-08 06:14:46 +04:00
|
|
|
memcpy(firstblk, bp->b_data, LFS_DIRBLKSIZ);
|
2015-07-24 09:56:41 +03:00
|
|
|
bread(vp, lastbn, lfs_sb_getbsize(fs), 0, &bp);
|
2003-03-28 11:09:52 +03:00
|
|
|
if (bp->b_flags & B_ERROR)
|
1999-03-18 05:02:18 +03:00
|
|
|
goto bad;
|
2013-06-08 06:14:46 +04:00
|
|
|
memcpy(bp->b_data, firstblk, LFS_DIRBLKSIZ);
|
|
|
|
for (cp = &bp->b_data[LFS_DIRBLKSIZ];
|
2015-07-24 09:56:41 +03:00
|
|
|
cp < &bp->b_data[lfs_sb_getbsize(fs)];
|
2013-06-08 06:14:46 +04:00
|
|
|
cp += LFS_DIRBLKSIZ)
|
2015-09-20 07:51:43 +03:00
|
|
|
zerodirblk(cp);
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
2015-08-12 21:28:00 +03:00
|
|
|
bread(vp, lfs_dino_getdb(fs, dp, lastbn + 1),
|
2015-03-29 22:35:58 +03:00
|
|
|
(long) lfs_dblksize(fs, dp, lastbn + 1), 0, &bp);
|
2003-03-28 11:09:52 +03:00
|
|
|
if (bp->b_flags & B_ERROR)
|
1999-03-18 05:02:18 +03:00
|
|
|
goto bad;
|
2015-09-20 07:51:43 +03:00
|
|
|
zerodirblk(bp->b_data);
|
1999-03-18 05:02:18 +03:00
|
|
|
pwarn("NO SPACE LEFT IN %s", name);
|
|
|
|
if (preen)
|
|
|
|
printf(" (EXPANDED)\n");
|
|
|
|
else if (reply("EXPAND") == 0)
|
|
|
|
goto bad;
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
return (1);
|
|
|
|
bad:
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setdb(fs, dp, lastbn, lfs_dino_getdb(fs, dp, lastbn + 1));
|
|
|
|
lfs_dino_setdb(fs, dp, lastbn + 1, 0);
|
|
|
|
lfs_dino_setsize(fs, dp,
|
|
|
|
lfs_dino_getsize(fs, dp) - lfs_sb_getbsize(fs));
|
|
|
|
lfs_dino_setblocks(fs, dp,
|
|
|
|
lfs_dino_getblocks(fs, dp) - lfs_btofsb(fs, lfs_sb_getbsize(fs)));
|
1999-03-18 05:02:18 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a new directory
|
|
|
|
*/
|
|
|
|
int
|
2000-05-23 05:48:52 +04:00
|
|
|
allocdir(ino_t parent, ino_t request, int mode)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
ino_t ino;
|
|
|
|
char *cp;
|
2015-08-12 21:28:00 +03:00
|
|
|
union lfs_dinode *dp;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct ubuf *bp;
|
2015-09-21 04:24:23 +03:00
|
|
|
LFS_DIRHEADER *dirp;
|
2003-03-28 11:09:52 +03:00
|
|
|
struct uvnode *vp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
2013-06-08 06:11:11 +04:00
|
|
|
ino = allocino(request, LFS_IFDIR | mode);
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, ino);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
bread(vp, lfs_dino_getdb(fs, dp, 0), lfs_sb_getfsize(fs), 0, &bp);
|
2003-03-28 11:09:52 +03:00
|
|
|
if (bp->b_flags & B_ERROR) {
|
2007-10-09 01:39:49 +04:00
|
|
|
brelse(bp, 0);
|
1999-03-18 05:02:18 +03:00
|
|
|
freeino(ino);
|
|
|
|
return (0);
|
|
|
|
}
|
2015-09-21 04:24:23 +03:00
|
|
|
dirp = (LFS_DIRHEADER *)bp->b_data;
|
2015-09-20 07:51:43 +03:00
|
|
|
/* . */
|
|
|
|
lfs_dir_setino(fs, dirp, ino);
|
2015-09-21 04:22:18 +03:00
|
|
|
lfs_dir_setreclen(fs, dirp, LFS_DIRECTSIZ(fs, 1));
|
2015-09-20 07:51:43 +03:00
|
|
|
lfs_dir_settype(fs, dirp, LFS_DT_DIR);
|
|
|
|
lfs_dir_setnamlen(fs, dirp, 1);
|
|
|
|
lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
|
2015-09-21 04:22:18 +03:00
|
|
|
LFS_DIRECTSIZ(fs, 1));
|
2015-09-20 07:51:43 +03:00
|
|
|
/* .. */
|
|
|
|
dirp = LFS_NEXTDIR(fs, dirp);
|
|
|
|
lfs_dir_setino(fs, dirp, parent);
|
2015-09-21 04:22:18 +03:00
|
|
|
lfs_dir_setreclen(fs, dirp, LFS_DIRBLKSIZ - LFS_DIRECTSIZ(fs, 1));
|
2015-09-20 07:51:43 +03:00
|
|
|
lfs_dir_settype(fs, dirp, LFS_DT_DIR);
|
|
|
|
lfs_dir_setnamlen(fs, dirp, 2);
|
|
|
|
lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
|
2015-09-21 04:22:18 +03:00
|
|
|
LFS_DIRBLKSIZ - LFS_DIRECTSIZ(fs, 1));
|
2013-06-08 06:14:46 +04:00
|
|
|
for (cp = &bp->b_data[LFS_DIRBLKSIZ];
|
2015-07-24 09:56:41 +03:00
|
|
|
cp < &bp->b_data[lfs_sb_getfsize(fs)];
|
2015-09-20 07:51:43 +03:00
|
|
|
cp += LFS_DIRBLKSIZ) {
|
|
|
|
zerodirblk(cp);
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
VOP_BWRITE(bp);
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setnlink(fs, dp, 2);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
2013-06-06 04:52:50 +04:00
|
|
|
if (ino == ULFS_ROOTINO) {
|
2015-08-12 21:28:00 +03:00
|
|
|
lncntp[ino] = lfs_dino_getnlink(fs, dp);
|
1999-03-18 05:02:18 +03:00
|
|
|
cacheino(dp, ino);
|
2000-05-23 05:48:52 +04:00
|
|
|
return (ino);
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
|
|
|
|
freeino(ino);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
cacheino(dp, ino);
|
|
|
|
statemap[ino] = statemap[parent];
|
|
|
|
if (statemap[ino] == DSTATE) {
|
2015-08-12 21:28:00 +03:00
|
|
|
lncntp[ino] = lfs_dino_getnlink(fs, dp);
|
1999-03-18 05:02:18 +03:00
|
|
|
lncntp[parent]++;
|
|
|
|
}
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, parent);
|
|
|
|
dp = VTOD(vp);
|
2015-08-12 21:28:00 +03:00
|
|
|
lfs_dino_setnlink(fs, dp, lfs_dino_getnlink(fs, dp) + 1);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
return (ino);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free a directory inode
|
|
|
|
*/
|
|
|
|
static void
|
2000-05-23 05:48:52 +04:00
|
|
|
freedir(ino_t ino, ino_t parent)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
struct uvnode *vp;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
if (ino != parent) {
|
2003-03-28 11:09:52 +03:00
|
|
|
vp = vget(fs, parent);
|
2015-09-01 09:08:37 +03:00
|
|
|
lfs_dino_setnlink(fs, VTOI(vp)->i_din,
|
|
|
|
lfs_dino_getnlink(fs, VTOI(vp)->i_din) - 1);
|
2003-03-28 11:09:52 +03:00
|
|
|
inodirty(VTOI(vp));
|
1999-03-18 05:02:18 +03:00
|
|
|
}
|
|
|
|
freeino(ino);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate a temporary name for the lost+found directory.
|
|
|
|
*/
|
|
|
|
static int
|
2000-05-23 05:48:52 +04:00
|
|
|
lftempname(char *bufp, ino_t ino)
|
1999-03-18 05:02:18 +03:00
|
|
|
{
|
2003-03-28 11:09:52 +03:00
|
|
|
ino_t in;
|
|
|
|
char *cp;
|
|
|
|
int namlen;
|
1999-03-18 05:02:18 +03:00
|
|
|
|
|
|
|
cp = bufp + 2;
|
|
|
|
for (in = maxino; in > 0; in /= 10)
|
|
|
|
cp++;
|
|
|
|
*--cp = 0;
|
|
|
|
namlen = cp - bufp;
|
|
|
|
in = ino;
|
|
|
|
while (cp > bufp) {
|
|
|
|
*--cp = (in % 10) + '0';
|
|
|
|
in /= 10;
|
|
|
|
}
|
|
|
|
*cp = '#';
|
|
|
|
return (namlen);
|
|
|
|
}
|