2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-26 06:35:07 +00: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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 06:35:07 +00: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.
|
|
|
|
*/
|
|
|
|
|
1998-12-03 02:41:11 +00:00
|
|
|
#if 0
|
2002-08-25 13:10:45 +00:00
|
|
|
#ifndef lint
|
1997-03-11 12:20:21 +00:00
|
|
|
static const char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
2002-08-25 13:10:45 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2010-01-11 20:05:38 +00:00
|
|
|
#include <sys/sysctl.h>
|
1997-03-11 12:20:21 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
2001-03-21 09:48:03 +00:00
|
|
|
#include <ufs/ffs/fs.h>
|
1997-03-12 17:01:11 +00:00
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
#include <err.h>
|
2010-01-11 20:05:38 +00:00
|
|
|
#include <errno.h>
|
2002-07-31 12:01:14 +00:00
|
|
|
#include <stdint.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <string.h>
|
1997-03-11 12:20:21 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include "fsck.h"
|
|
|
|
|
|
|
|
#define MINDIRSIZE (sizeof (struct dirtemplate))
|
|
|
|
|
2010-01-11 19:52:40 +00:00
|
|
|
static int fix_extraneous(struct inoinfo *, struct inodesc *);
|
|
|
|
static int deleteentry(struct inodesc *);
|
2002-03-20 22:57:10 +00:00
|
|
|
static int blksort(const void *, const void *);
|
|
|
|
static int pass2check(struct inodesc *);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1995-04-02 14:52:29 +00:00
|
|
|
void
|
2002-03-20 22:57:10 +00:00
|
|
|
pass2(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2021-01-07 01:37:08 +00:00
|
|
|
struct inode ip;
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode *dp;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct inoinfo **inpp, *inp;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct inoinfo **inpend;
|
|
|
|
struct inodesc curino;
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode dino;
|
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
char pathbuf[MAXPATHLEN + 1];
|
|
|
|
|
2017-02-15 19:50:26 +00:00
|
|
|
switch (inoinfo(UFS_ROOTINO)->ino_state) {
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
case USTATE:
|
|
|
|
pfatal("ROOT INODE UNALLOCATED");
|
1998-03-08 09:59:44 +00:00
|
|
|
if (reply("ALLOCATE") == 0) {
|
|
|
|
ckfini(0);
|
1997-03-11 12:20:21 +00:00
|
|
|
exit(EEXIT);
|
1998-03-08 09:59:44 +00:00
|
|
|
}
|
2017-02-15 19:50:26 +00:00
|
|
|
if (allocdir(UFS_ROOTINO, UFS_ROOTINO, 0755) != UFS_ROOTINO)
|
1997-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DCLEAR:
|
|
|
|
pfatal("DUPS/BAD IN ROOT INODE");
|
|
|
|
if (reply("REALLOCATE")) {
|
2023-03-07 23:12:37 +00:00
|
|
|
freedirino(UFS_ROOTINO, UFS_ROOTINO);
|
2017-02-15 19:50:26 +00:00
|
|
|
if (allocdir(UFS_ROOTINO, UFS_ROOTINO, 0755) !=
|
|
|
|
UFS_ROOTINO)
|
1997-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-03-08 09:59:44 +00:00
|
|
|
if (reply("CONTINUE") == 0) {
|
|
|
|
ckfini(0);
|
1997-03-11 12:20:21 +00:00
|
|
|
exit(EEXIT);
|
1998-03-08 09:59:44 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FSTATE:
|
|
|
|
case FCLEAR:
|
2004-10-08 20:44:47 +00:00
|
|
|
case FZLINK:
|
1994-05-26 06:35:07 +00:00
|
|
|
pfatal("ROOT INODE NOT DIRECTORY");
|
|
|
|
if (reply("REALLOCATE")) {
|
2017-02-15 19:50:26 +00:00
|
|
|
freeino(UFS_ROOTINO);
|
|
|
|
if (allocdir(UFS_ROOTINO, UFS_ROOTINO, 0755) !=
|
|
|
|
UFS_ROOTINO)
|
1997-03-11 12:20:21 +00:00
|
|
|
errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-03-08 09:59:44 +00:00
|
|
|
if (reply("FIX") == 0) {
|
|
|
|
ckfini(0);
|
1997-03-11 12:20:21 +00:00
|
|
|
exit(EEXIT);
|
1998-03-08 09:59:44 +00:00
|
|
|
}
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(UFS_ROOTINO, &ip);
|
|
|
|
dp = ip.i_dp;
|
2018-03-17 12:59:55 +00:00
|
|
|
DIP_SET(dp, di_mode, DIP(dp, di_mode) & ~IFMT);
|
|
|
|
DIP_SET(dp, di_mode, DIP(dp, di_mode) | IFDIR);
|
2021-01-07 01:37:08 +00:00
|
|
|
inodirty(&ip);
|
|
|
|
irelse(&ip);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DSTATE:
|
2004-10-08 20:44:47 +00:00
|
|
|
case DZLINK:
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1998-12-03 02:27:35 +00:00
|
|
|
errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
|
2017-02-15 19:50:26 +00:00
|
|
|
inoinfo(UFS_ROOTINO)->ino_state);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2017-02-15 19:50:26 +00:00
|
|
|
inoinfo(UFS_ROOTINO)->ino_state = DFOUND;
|
|
|
|
inoinfo(UFS_WINO)->ino_state = FSTATE;
|
|
|
|
inoinfo(UFS_WINO)->ino_type = DT_WHT;
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Sort the directory list into disk block order.
|
|
|
|
*/
|
|
|
|
qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
|
|
|
|
/*
|
|
|
|
* Check the integrity of each directory.
|
|
|
|
*/
|
1997-03-11 12:20:21 +00:00
|
|
|
memset(&curino, 0, sizeof(struct inodesc));
|
1994-05-26 06:35:07 +00:00
|
|
|
curino.id_type = DATA;
|
|
|
|
curino.id_func = pass2check;
|
|
|
|
inpend = &inpsort[inplast];
|
|
|
|
for (inpp = inpsort; inpp < inpend; inpp++) {
|
2000-12-15 14:23:55 +00:00
|
|
|
if (got_siginfo) {
|
2002-07-31 12:01:14 +00:00
|
|
|
printf("%s: phase 2: dir %td of %d (%d%%)\n", cdevname,
|
2001-11-17 23:48:21 +00:00
|
|
|
inpp - inpsort, (int)inplast,
|
|
|
|
(int)((inpp - inpsort) * 100 / inplast));
|
2000-12-15 14:23:55 +00:00
|
|
|
got_siginfo = 0;
|
|
|
|
}
|
2004-02-28 07:50:42 +00:00
|
|
|
if (got_sigalarm) {
|
|
|
|
setproctitle("%s p2 %d%%", cdevname,
|
|
|
|
(int)((inpp - inpsort) * 100 / inplast));
|
|
|
|
got_sigalarm = 0;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
inp = *inpp;
|
|
|
|
if (inp->i_isize == 0)
|
|
|
|
continue;
|
|
|
|
if (inp->i_isize < MINDIRSIZE) {
|
|
|
|
direrror(inp->i_number, "DIRECTORY TOO SHORT");
|
|
|
|
inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
|
|
|
|
if (reply("FIX") == 1) {
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(inp->i_number, &ip);
|
|
|
|
DIP_SET(ip.i_dp, di_size, inp->i_isize);
|
|
|
|
inodirty(&ip);
|
|
|
|
irelse(&ip);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
|
|
|
|
getpathname(pathbuf, inp->i_number, inp->i_number);
|
1998-03-08 09:59:44 +00:00
|
|
|
if (usedsoftdep)
|
2002-07-31 12:01:14 +00:00
|
|
|
pfatal("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
|
|
|
|
"DIRECTORY", pathbuf,
|
|
|
|
(intmax_t)inp->i_isize, DIRBLKSIZ);
|
1998-03-08 09:59:44 +00:00
|
|
|
else
|
2002-07-31 12:01:14 +00:00
|
|
|
pwarn("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
|
|
|
|
"DIRECTORY", pathbuf,
|
|
|
|
(intmax_t)inp->i_isize, DIRBLKSIZ);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (preen)
|
|
|
|
printf(" (ADJUSTED)\n");
|
|
|
|
inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
|
|
|
|
if (preen || reply("ADJUST") == 1) {
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(inp->i_number, &ip);
|
|
|
|
DIP_SET(ip.i_dp, di_size,
|
2004-09-01 05:48:06 +00:00
|
|
|
roundup(inp->i_isize, DIRBLKSIZ));
|
2021-01-07 01:37:08 +00:00
|
|
|
inodirty(&ip);
|
|
|
|
irelse(&ip);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
dp = &dino;
|
|
|
|
memset(dp, 0, sizeof(struct ufs2_dinode));
|
2018-03-17 12:59:55 +00:00
|
|
|
DIP_SET(dp, di_mode, IFDIR);
|
2004-09-01 05:48:06 +00:00
|
|
|
DIP_SET(dp, di_size, inp->i_isize);
|
2017-02-15 19:50:26 +00:00
|
|
|
for (i = 0; i < MIN(inp->i_numblks, UFS_NDADDR); i++)
|
2004-09-01 05:48:06 +00:00
|
|
|
DIP_SET(dp, di_db[i], inp->i_blks[i]);
|
2017-02-15 19:50:26 +00:00
|
|
|
if (inp->i_numblks > UFS_NDADDR)
|
|
|
|
for (i = 0; i < UFS_NIADDR; i++)
|
|
|
|
DIP_SET(dp, di_ib[i],
|
|
|
|
inp->i_blks[UFS_NDADDR + i]);
|
1994-05-26 06:35:07 +00:00
|
|
|
curino.id_number = inp->i_number;
|
|
|
|
curino.id_parent = inp->i_parent;
|
|
|
|
(void)ckinode(dp, &curino);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now that the parents of all directories have been found,
|
|
|
|
* make another pass to verify the value of `..'
|
|
|
|
*/
|
|
|
|
for (inpp = inpsort; inpp < inpend; inpp++) {
|
|
|
|
inp = *inpp;
|
|
|
|
if (inp->i_parent == 0 || inp->i_isize == 0)
|
|
|
|
continue;
|
1998-12-03 02:27:35 +00:00
|
|
|
if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
INO_IS_DUNFOUND(inp->i_number)) {
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(inp->i_number)->ino_state = DFOUND;
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
check_dirdepth(inp);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
if (inp->i_dotdot == inp->i_parent ||
|
|
|
|
inp->i_dotdot == (ino_t)-1)
|
|
|
|
continue;
|
|
|
|
if (inp->i_dotdot == 0) {
|
|
|
|
inp->i_dotdot = inp->i_parent;
|
2022-08-17 21:19:59 +00:00
|
|
|
if (debug)
|
|
|
|
fileerror(inp->i_parent, inp->i_number,
|
|
|
|
"DEFERRED MISSING '..' FIX");
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)makeentry(inp->i_number, inp->i_parent, "..");
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(inp->i_parent)->ino_linkcnt--;
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-11 20:05:38 +00:00
|
|
|
/*
|
|
|
|
* Here we have:
|
|
|
|
* inp->i_number is directory with bad ".." in it.
|
|
|
|
* inp->i_dotdot is current value of "..".
|
|
|
|
* inp->i_parent is directory to which ".." should point.
|
|
|
|
*/
|
|
|
|
getpathname(pathbuf, inp->i_parent, inp->i_number);
|
2012-09-27 23:30:58 +00:00
|
|
|
printf("BAD INODE NUMBER FOR '..' in DIR I=%ju (%s)\n",
|
|
|
|
(uintmax_t)inp->i_number, pathbuf);
|
2010-01-11 20:05:38 +00:00
|
|
|
getpathname(pathbuf, inp->i_dotdot, inp->i_dotdot);
|
2012-09-27 23:30:58 +00:00
|
|
|
printf("CURRENTLY POINTS TO I=%ju (%s), ",
|
|
|
|
(uintmax_t)inp->i_dotdot, pathbuf);
|
2010-01-11 20:05:38 +00:00
|
|
|
getpathname(pathbuf, inp->i_parent, inp->i_parent);
|
2012-09-27 23:30:58 +00:00
|
|
|
printf("SHOULD POINT TO I=%ju (%s)",
|
|
|
|
(uintmax_t)inp->i_parent, pathbuf);
|
2010-01-11 20:05:38 +00:00
|
|
|
if (cursnapshot != 0) {
|
|
|
|
/*
|
|
|
|
* We need to:
|
|
|
|
* setcwd(inp->i_number);
|
|
|
|
* setdotdot(inp->i_dotdot, inp->i_parent);
|
|
|
|
*/
|
|
|
|
cmd.value = inp->i_number;
|
|
|
|
if (sysctlbyname("vfs.ffs.setcwd", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
/* kernel lacks support for these functions */
|
|
|
|
printf(" (IGNORED)\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cmd.value = inp->i_dotdot; /* verify same value */
|
|
|
|
cmd.size = inp->i_parent; /* new parent */
|
|
|
|
if (sysctlbyname("vfs.ffs.setdotdot", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
printf(" (FIX FAILED: %s)\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
inoinfo(inp->i_parent)->ino_linkcnt--;
|
|
|
|
inp->i_dotdot = inp->i_parent;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (preen)
|
|
|
|
printf(" (FIXED)\n");
|
|
|
|
else if (reply("FIX") == 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(inp->i_dotdot)->ino_linkcnt++;
|
|
|
|
inoinfo(inp->i_parent)->ino_linkcnt--;
|
1994-05-26 06:35:07 +00:00
|
|
|
inp->i_dotdot = inp->i_parent;
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
(void)changeino(inp->i_number, "..", inp->i_parent,
|
|
|
|
getinoinfo(inp->i_parent)->i_depth + 1);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Mark all the directories that can be found from the root.
|
|
|
|
*/
|
|
|
|
propagate();
|
|
|
|
}
|
|
|
|
|
1997-03-11 12:20:21 +00:00
|
|
|
static int
|
2002-03-20 22:57:10 +00:00
|
|
|
pass2check(struct inodesc *idesc)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-20 17:55:10 +00:00
|
|
|
struct direct *dirp = idesc->id_dirp;
|
2010-05-20 06:05:40 +00:00
|
|
|
char dirname[MAXPATHLEN + 1];
|
2002-03-20 17:55:10 +00:00
|
|
|
struct inoinfo *inp;
|
1994-05-26 06:35:07 +00:00
|
|
|
int n, entrysize, ret = 0;
|
2021-01-07 01:37:08 +00:00
|
|
|
struct inode ip;
|
2002-06-21 06:18:05 +00:00
|
|
|
union dinode *dp;
|
2002-07-30 13:01:25 +00:00
|
|
|
const char *errmsg;
|
2022-08-17 21:19:59 +00:00
|
|
|
struct direct proto, *newdirp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1995-05-30 06:12:45 +00:00
|
|
|
/*
|
1994-05-26 06:35:07 +00:00
|
|
|
* check for "."
|
|
|
|
*/
|
|
|
|
if (idesc->id_entryno != 0)
|
|
|
|
goto chk1;
|
|
|
|
if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
|
|
|
|
if (dirp->d_ino != idesc->id_number) {
|
|
|
|
direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
|
2022-08-17 21:19:59 +00:00
|
|
|
if (reply("FIX") == 1) {
|
|
|
|
dirp->d_ino = idesc->id_number;
|
1994-05-26 06:35:07 +00:00
|
|
|
ret |= ALTERED;
|
2022-08-17 21:19:59 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-05-12 23:44:15 +00:00
|
|
|
if (dirp->d_type != DT_DIR) {
|
1994-05-26 06:35:07 +00:00
|
|
|
direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
|
2022-08-17 21:19:59 +00:00
|
|
|
if (reply("FIX") == 1) {
|
|
|
|
dirp->d_type = DT_DIR;
|
1994-05-26 06:35:07 +00:00
|
|
|
ret |= ALTERED;
|
2022-08-17 21:19:59 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
goto chk1;
|
|
|
|
}
|
|
|
|
proto.d_ino = idesc->id_number;
|
2002-05-12 23:44:15 +00:00
|
|
|
proto.d_type = DT_DIR;
|
1994-05-26 06:35:07 +00:00
|
|
|
proto.d_namlen = 1;
|
|
|
|
(void)strcpy(proto.d_name, ".");
|
|
|
|
entrysize = DIRSIZ(0, &proto);
|
2022-08-17 21:19:59 +00:00
|
|
|
direrror(idesc->id_number, "MISSING '.'");
|
|
|
|
errmsg = "ADD '.' ENTRY";
|
|
|
|
if (dirp->d_reclen < entrysize + DIRSIZ(0, dirp)) {
|
|
|
|
/* Not enough space to add '.', replace first entry with '.' */
|
|
|
|
if (dirp->d_ino != 0) {
|
|
|
|
pwarn("\nFIRST ENTRY IN DIRECTORY CONTAINS %s\n",
|
|
|
|
dirp->d_name);
|
|
|
|
errmsg = "REPLACE WITH '.'";
|
|
|
|
}
|
|
|
|
if (reply(errmsg) == 0)
|
|
|
|
goto chk1;
|
1994-05-26 06:35:07 +00:00
|
|
|
proto.d_reclen = dirp->d_reclen;
|
1997-03-11 12:20:21 +00:00
|
|
|
memmove(dirp, &proto, (size_t)entrysize);
|
2022-08-17 21:19:59 +00:00
|
|
|
ret |= ALTERED;
|
1994-05-26 06:35:07 +00:00
|
|
|
} else {
|
2022-08-17 21:19:59 +00:00
|
|
|
/* Move over first entry and add '.' entry */
|
|
|
|
if (reply(errmsg) == 0)
|
|
|
|
goto chk1;
|
|
|
|
newdirp = (struct direct *)((char *)(dirp) + entrysize);
|
|
|
|
dirp->d_reclen -= entrysize;
|
|
|
|
memmove(newdirp, dirp, dirp->d_reclen);
|
1994-05-26 06:35:07 +00:00
|
|
|
proto.d_reclen = entrysize;
|
1997-03-11 12:20:21 +00:00
|
|
|
memmove(dirp, &proto, (size_t)entrysize);
|
1994-05-26 06:35:07 +00:00
|
|
|
idesc->id_entryno++;
|
2022-08-17 21:19:59 +00:00
|
|
|
inoinfo(idesc->id_number)->ino_linkcnt--;
|
|
|
|
dirp = newdirp;
|
|
|
|
ret |= ALTERED;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
chk1:
|
|
|
|
if (idesc->id_entryno > 1)
|
|
|
|
goto chk2;
|
|
|
|
inp = getinoinfo(idesc->id_number);
|
|
|
|
proto.d_ino = inp->i_parent;
|
2002-05-12 23:44:15 +00:00
|
|
|
proto.d_type = DT_DIR;
|
1994-05-26 06:35:07 +00:00
|
|
|
proto.d_namlen = 2;
|
|
|
|
(void)strcpy(proto.d_name, "..");
|
|
|
|
entrysize = DIRSIZ(0, &proto);
|
|
|
|
if (idesc->id_entryno == 0) {
|
|
|
|
n = DIRSIZ(0, dirp);
|
|
|
|
if (dirp->d_reclen < n + entrysize)
|
|
|
|
goto chk2;
|
|
|
|
proto.d_reclen = dirp->d_reclen - n;
|
|
|
|
dirp->d_reclen = n;
|
|
|
|
idesc->id_entryno++;
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(dirp->d_ino)->ino_linkcnt--;
|
1994-05-26 06:35:07 +00:00
|
|
|
dirp = (struct direct *)((char *)(dirp) + n);
|
1997-03-11 12:20:21 +00:00
|
|
|
memset(dirp, 0, (size_t)proto.d_reclen);
|
1994-05-26 06:35:07 +00:00
|
|
|
dirp->d_reclen = proto.d_reclen;
|
|
|
|
}
|
|
|
|
if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
|
2023-03-07 23:12:37 +00:00
|
|
|
if (dirp->d_ino > maxino) {
|
|
|
|
direrror(idesc->id_number, "BAD INODE NUMBER FOR '..'");
|
|
|
|
/*
|
|
|
|
* If we know parent set it now, otherwise let it
|
|
|
|
* point to the root inode and it will get cleaned
|
|
|
|
* up later if that is not correct.
|
|
|
|
*/
|
|
|
|
if (inp->i_parent != 0)
|
|
|
|
dirp->d_ino = inp->i_parent;
|
|
|
|
else
|
|
|
|
dirp->d_ino = UFS_ROOTINO;
|
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
inp->i_dotdot = dirp->d_ino;
|
2002-05-12 23:44:15 +00:00
|
|
|
if (dirp->d_type != DT_DIR) {
|
1994-05-26 06:35:07 +00:00
|
|
|
direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
|
|
|
|
dirp->d_type = DT_DIR;
|
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
|
|
|
}
|
|
|
|
goto chk2;
|
|
|
|
}
|
2022-08-17 21:19:59 +00:00
|
|
|
fileerror(inp->i_parent != 0 ? inp->i_parent : idesc->id_number,
|
|
|
|
idesc->id_number, "MISSING '..'");
|
|
|
|
errmsg = "ADD '..' ENTRY";
|
|
|
|
if (dirp->d_reclen < entrysize + DIRSIZ(0, dirp)) {
|
|
|
|
/* No space to add '..', replace second entry with '..' */
|
|
|
|
if (dirp->d_ino != 0) {
|
|
|
|
pfatal("SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
|
|
|
|
dirp->d_name);
|
|
|
|
errmsg = "REPLACE WITH '..'";
|
|
|
|
}
|
|
|
|
if (reply(errmsg) == 0) {
|
|
|
|
inp->i_dotdot = (ino_t)-1;
|
|
|
|
goto chk2;
|
|
|
|
}
|
|
|
|
if (proto.d_ino == 0) {
|
|
|
|
/* Defer processing until parent known */
|
|
|
|
idesc->id_entryno++;
|
|
|
|
if (debug)
|
|
|
|
printf("(FIX DEFERRED)\n");
|
|
|
|
}
|
|
|
|
inp->i_dotdot = proto.d_ino;
|
1994-05-26 06:35:07 +00:00
|
|
|
proto.d_reclen = dirp->d_reclen;
|
1997-03-11 12:20:21 +00:00
|
|
|
memmove(dirp, &proto, (size_t)entrysize);
|
2022-08-17 21:19:59 +00:00
|
|
|
ret |= ALTERED;
|
|
|
|
} else {
|
|
|
|
/* Move over second entry and add '..' entry */
|
|
|
|
if (reply(errmsg) == 0) {
|
|
|
|
inp->i_dotdot = (ino_t)-1;
|
|
|
|
goto chk2;
|
|
|
|
}
|
|
|
|
if (proto.d_ino == 0) {
|
|
|
|
/* Defer processing until parent known */
|
|
|
|
idesc->id_entryno++;
|
|
|
|
if (debug)
|
|
|
|
printf("(FIX DEFERRED)\n");
|
|
|
|
}
|
|
|
|
inp->i_dotdot = proto.d_ino;
|
|
|
|
if (dirp->d_ino == 0) {
|
|
|
|
proto.d_reclen = dirp->d_reclen;
|
|
|
|
memmove(dirp, &proto, (size_t)entrysize);
|
|
|
|
} else {
|
|
|
|
newdirp = (struct direct *)((char *)(dirp) + entrysize);
|
|
|
|
dirp->d_reclen -= entrysize;
|
|
|
|
memmove(newdirp, dirp, dirp->d_reclen);
|
|
|
|
proto.d_reclen = entrysize;
|
|
|
|
memmove(dirp, &proto, (size_t)entrysize);
|
|
|
|
if (dirp->d_ino != 0) {
|
|
|
|
idesc->id_entryno++;
|
|
|
|
inoinfo(dirp->d_ino)->ino_linkcnt--;
|
|
|
|
}
|
|
|
|
dirp = newdirp;
|
|
|
|
}
|
|
|
|
ret |= ALTERED;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
chk2:
|
|
|
|
if (dirp->d_ino == 0)
|
|
|
|
return (ret|KEEPON);
|
|
|
|
if (dirp->d_namlen <= 2 &&
|
|
|
|
dirp->d_name[0] == '.' &&
|
|
|
|
idesc->id_entryno >= 2) {
|
|
|
|
if (dirp->d_namlen == 1) {
|
|
|
|
direrror(idesc->id_number, "EXTRA '.' ENTRY");
|
|
|
|
dirp->d_ino = 0;
|
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
|
|
|
return (KEEPON | ret);
|
|
|
|
}
|
|
|
|
if (dirp->d_name[1] == '.') {
|
|
|
|
direrror(idesc->id_number, "EXTRA '..' ENTRY");
|
|
|
|
dirp->d_ino = 0;
|
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
|
|
|
return (KEEPON | ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idesc->id_entryno++;
|
|
|
|
n = 0;
|
|
|
|
if (dirp->d_ino > maxino) {
|
|
|
|
fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
|
|
|
|
n = reply("REMOVE");
|
2017-02-15 19:50:26 +00:00
|
|
|
} else if (((dirp->d_ino == UFS_WINO && dirp->d_type != DT_WHT) ||
|
|
|
|
(dirp->d_ino != UFS_WINO && dirp->d_type == DT_WHT))) {
|
1997-03-11 12:20:21 +00:00
|
|
|
fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
|
2017-02-15 19:50:26 +00:00
|
|
|
dirp->d_ino = UFS_WINO;
|
1997-03-11 12:20:21 +00:00
|
|
|
dirp->d_type = DT_WHT;
|
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
1994-05-26 06:35:07 +00:00
|
|
|
} else {
|
|
|
|
again:
|
1998-12-03 02:27:35 +00:00
|
|
|
switch (inoinfo(dirp->d_ino)->ino_state) {
|
1994-05-26 06:35:07 +00:00
|
|
|
case USTATE:
|
|
|
|
if (idesc->id_entryno <= 2)
|
|
|
|
break;
|
|
|
|
fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
|
|
|
|
n = reply("REMOVE");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DCLEAR:
|
|
|
|
case FCLEAR:
|
|
|
|
if (idesc->id_entryno <= 2)
|
|
|
|
break;
|
1998-12-03 02:27:35 +00:00
|
|
|
if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
|
1994-05-26 06:35:07 +00:00
|
|
|
errmsg = "DUP/BAD";
|
1998-03-08 09:59:44 +00:00
|
|
|
else if (!preen && !usedsoftdep)
|
1994-05-26 06:35:07 +00:00
|
|
|
errmsg = "ZERO LENGTH DIRECTORY";
|
2010-05-20 06:05:40 +00:00
|
|
|
else if (cursnapshot == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
n = 1;
|
|
|
|
break;
|
2010-05-20 06:05:40 +00:00
|
|
|
} else {
|
|
|
|
getpathname(dirname, idesc->id_number,
|
|
|
|
dirp->d_ino);
|
2012-09-27 23:30:58 +00:00
|
|
|
pwarn("ZERO LENGTH DIRECTORY %s I=%ju",
|
|
|
|
dirname, (uintmax_t)dirp->d_ino);
|
2010-05-20 06:05:40 +00:00
|
|
|
/*
|
|
|
|
* We need to:
|
|
|
|
* setcwd(idesc->id_parent);
|
|
|
|
* rmdir(dirp->d_name);
|
|
|
|
*/
|
|
|
|
cmd.value = idesc->id_number;
|
|
|
|
if (sysctlbyname("vfs.ffs.setcwd", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
/* kernel lacks support */
|
|
|
|
printf(" (IGNORED)\n");
|
|
|
|
n = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rmdir(dirp->d_name) == -1) {
|
|
|
|
printf(" (REMOVAL FAILED: %s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
n = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* ".." reference to parent is removed */
|
|
|
|
inoinfo(idesc->id_number)->ino_linkcnt--;
|
|
|
|
printf(" (REMOVED)\n");
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
fileerror(idesc->id_number, dirp->d_ino, errmsg);
|
|
|
|
if ((n = reply("REMOVE")) == 1)
|
|
|
|
break;
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(dirp->d_ino, &ip);
|
|
|
|
dp = ip.i_dp;
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(dirp->d_ino)->ino_state =
|
2018-03-17 12:59:55 +00:00
|
|
|
(DIP(dp, di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
|
2002-06-21 06:18:05 +00:00
|
|
|
inoinfo(dirp->d_ino)->ino_linkcnt = DIP(dp, di_nlink);
|
2021-01-07 01:37:08 +00:00
|
|
|
irelse(&ip);
|
1994-05-26 06:35:07 +00:00
|
|
|
goto again;
|
|
|
|
|
|
|
|
case DSTATE:
|
2004-10-08 20:44:47 +00:00
|
|
|
case DZLINK:
|
1998-12-03 02:27:35 +00:00
|
|
|
if (inoinfo(idesc->id_number)->ino_state == DFOUND)
|
|
|
|
inoinfo(dirp->d_ino)->ino_state = DFOUND;
|
2002-08-25 13:10:45 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
case DFOUND:
|
|
|
|
inp = getinoinfo(dirp->d_ino);
|
2010-01-11 19:52:40 +00:00
|
|
|
if (idesc->id_entryno > 2) {
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
if (inp->i_parent == 0) {
|
2010-01-11 19:52:40 +00:00
|
|
|
inp->i_parent = idesc->id_number;
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
check_dirdepth(inp);
|
|
|
|
} else if ((n = fix_extraneous(inp, idesc))) {
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
Improvement in UFS/FFS directory placement when doing mkdir(2).
The algorithm for laying out new directories was devised in the 1980s
and markedly improved the performance of the filesystem. In those days
large disks had at most 100 cylinder groups and often as few as 10-20.
Modern multi-terrabyte disks have thousands of cylinder groups. The
original algorithm does not handle these large sizes well. This change
attempts to expand the scope of the original algorithm to work well
with these much larger disks while still retaining the properties
of the original algorithm for small disks.
The filesystem implementation is divided into policy routines and
implementation routines. The policy routines can be changed in any
way desired without risk of corrupting the filesystem. The policy
requests are handled by the implementation layer. If the policy
asks for an available resource, it is granted. But if it asks for
an already in-use resource, then the implementation will provide
an available one nearby the request. Thus it is impossible for a
policy to double allocate. This change is limited to the policy
implementation.
This change updates the ffs_dirpref() routine which is responsible
for selecting the cylinder group into which a new directory should
be placed. If we are near the root of the filesystem we aim to
spread them out as much as possible. As we descend deeper from the
root we cluster them closer together around their parent as we
expect them to be more closely interactive. Higher-level directories
like usr/src/sys and usr/src/bin should be separated while the
directories in these areas are more likely to be accessed together
so should be closer. And directories within commands or kernel
subsystems should be closer still.
We pick a range of cylinder groups around the cylinder group of the
directory in which we are being created. The size of the range for
our search is based on our depth from the root of our filesystem.
We then probe that range based on how many directories are already
present. The first new directory is at 1/2 (middle) of the range;
the second is in the first 1/4 of the range, then at 3/4, 1/8, 3/8,
5/8, 7/8, 1/16, 3/16, 5/16, etc.
It is desirable to store the depth of a directory in its on-disk
inode so that it is available when we need it. We add a new field
di_dirdepth to track the depth of each directory. Because there are
few spare fields left in the inode, we choose to share an existing
field in the inode rather than having one of our own. Specifically
we create a union with the di_freelink field. The di_freelink field
is used to track inodes that have been unlinked but remain referenced.
It is not needed until a rmdir(2) operation has been done on a
directory. At that point, the directory has no contents and even
if it is kept active as a current directory is no longer able to
have any new directories or files created in it. Thus the use of
di_dirdepth and di_freelink will never coincide.
Reported by: Timo Voelker
Reviewed by: kib
Tested by: Peter Holm
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39246
2023-03-30 04:09:39 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-08-25 13:10:45 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
case FSTATE:
|
2004-10-08 20:44:47 +00:00
|
|
|
case FZLINK:
|
2002-05-12 23:44:15 +00:00
|
|
|
if (dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fileerror(idesc->id_number, dirp->d_ino,
|
|
|
|
"BAD TYPE VALUE");
|
1998-12-03 02:27:35 +00:00
|
|
|
dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
|
1994-05-26 06:35:07 +00:00
|
|
|
if (reply("FIX") == 1)
|
|
|
|
ret |= ALTERED;
|
|
|
|
}
|
1998-12-03 02:27:35 +00:00
|
|
|
inoinfo(dirp->d_ino)->ino_linkcnt--;
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-09-27 23:30:58 +00:00
|
|
|
errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
|
|
|
|
inoinfo(dirp->d_ino)->ino_state,
|
|
|
|
(uintmax_t)dirp->d_ino);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
return (ret|KEEPON);
|
|
|
|
dirp->d_ino = 0;
|
|
|
|
return (ret|KEEPON|ALTERED);
|
|
|
|
}
|
|
|
|
|
2010-01-11 19:52:40 +00:00
|
|
|
static int
|
|
|
|
fix_extraneous(struct inoinfo *inp, struct inodesc *idesc)
|
|
|
|
{
|
2010-01-11 20:05:38 +00:00
|
|
|
char *cp;
|
2021-01-07 01:37:08 +00:00
|
|
|
struct inode ip;
|
2010-01-11 19:52:40 +00:00
|
|
|
struct inodesc dotdesc;
|
|
|
|
char oldname[MAXPATHLEN + 1];
|
|
|
|
char newname[MAXPATHLEN + 1];
|
2011-04-27 02:55:03 +00:00
|
|
|
|
2010-01-11 19:52:40 +00:00
|
|
|
/*
|
|
|
|
* If we have not yet found "..", look it up now so we know
|
|
|
|
* which inode the directory itself believes is its parent.
|
|
|
|
*/
|
|
|
|
if (inp->i_dotdot == 0) {
|
|
|
|
memset(&dotdesc, 0, sizeof(struct inodesc));
|
|
|
|
dotdesc.id_type = DATA;
|
|
|
|
dotdesc.id_number = idesc->id_dirp->d_ino;
|
|
|
|
dotdesc.id_func = findino;
|
|
|
|
dotdesc.id_name = strdup("..");
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(dotdesc.id_number, &ip);
|
|
|
|
if ((ckinode(ip.i_dp, &dotdesc) & FOUND))
|
2010-01-11 19:52:40 +00:00
|
|
|
inp->i_dotdot = dotdesc.id_parent;
|
2021-01-07 01:37:08 +00:00
|
|
|
irelse(&ip);
|
2023-03-22 22:57:26 +00:00
|
|
|
free(dotdesc.id_name);
|
2010-01-11 19:52:40 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We have the previously found old name (inp->i_parent) and the
|
|
|
|
* just found new name (idesc->id_number). We have five cases:
|
|
|
|
* 1) ".." is missing - can remove either name, choose to delete
|
|
|
|
* new one and let fsck create ".." pointing to old name.
|
|
|
|
* 2) Both new and old are in same directory, choose to delete
|
|
|
|
* the new name and let fsck fix ".." if it is wrong.
|
|
|
|
* 3) ".." does not point to the new name, so delete it and let
|
|
|
|
* fsck fix ".." to point to the old one if it is wrong.
|
|
|
|
* 4) ".." points to the old name only, so delete the new one.
|
|
|
|
* 5) ".." points to the new name only, so delete the old one.
|
|
|
|
*
|
|
|
|
* For cases 1-4 we eliminate the new name;
|
|
|
|
* for case 5 we eliminate the old name.
|
|
|
|
*/
|
|
|
|
if (inp->i_dotdot == 0 || /* Case 1 */
|
|
|
|
idesc->id_number == inp->i_parent || /* Case 2 */
|
|
|
|
inp->i_dotdot != idesc->id_number || /* Case 3 */
|
|
|
|
inp->i_dotdot == inp->i_parent) { /* Case 4 */
|
|
|
|
getpathname(newname, idesc->id_number, idesc->id_number);
|
|
|
|
if (strcmp(newname, "/") != 0)
|
|
|
|
strcat (newname, "/");
|
|
|
|
strcat(newname, idesc->id_dirp->d_name);
|
|
|
|
getpathname(oldname, inp->i_number, inp->i_number);
|
2010-01-11 20:05:38 +00:00
|
|
|
pwarn("%s IS AN EXTRANEOUS HARD LINK TO DIRECTORY %s",
|
2010-01-11 19:52:40 +00:00
|
|
|
newname, oldname);
|
|
|
|
if (cursnapshot != 0) {
|
|
|
|
/*
|
|
|
|
* We need to
|
|
|
|
* setcwd(idesc->id_number);
|
|
|
|
* unlink(idesc->id_dirp->d_name);
|
|
|
|
*/
|
2010-01-11 20:05:38 +00:00
|
|
|
cmd.value = idesc->id_number;
|
|
|
|
if (sysctlbyname("vfs.ffs.setcwd", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
printf(" (IGNORED)\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2010-01-11 23:33:30 +00:00
|
|
|
cmd.value = (intptr_t)idesc->id_dirp->d_name;
|
2010-01-11 20:05:38 +00:00
|
|
|
cmd.size = inp->i_number; /* verify same name */
|
|
|
|
if (sysctlbyname("vfs.ffs.unlink", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
printf(" (UNLINK FAILED: %s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
printf(" (REMOVED)\n");
|
2010-01-11 19:52:40 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (preen) {
|
|
|
|
printf(" (REMOVED)\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (reply("REMOVE"));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* None of the first four cases above, so must be case (5).
|
|
|
|
* Eliminate the old name and make the new the name the parent.
|
|
|
|
*/
|
|
|
|
getpathname(oldname, inp->i_parent, inp->i_number);
|
|
|
|
getpathname(newname, inp->i_number, inp->i_number);
|
2010-01-11 20:05:38 +00:00
|
|
|
pwarn("%s IS AN EXTRANEOUS HARD LINK TO DIRECTORY %s", oldname,
|
2010-01-11 19:52:40 +00:00
|
|
|
newname);
|
|
|
|
if (cursnapshot != 0) {
|
|
|
|
/*
|
|
|
|
* We need to
|
|
|
|
* setcwd(inp->i_parent);
|
|
|
|
* unlink(last component of oldname pathname);
|
|
|
|
*/
|
2010-01-11 20:05:38 +00:00
|
|
|
cmd.value = inp->i_parent;
|
|
|
|
if (sysctlbyname("vfs.ffs.setcwd", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
printf(" (IGNORED)\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2012-01-03 18:51:58 +00:00
|
|
|
if ((cp = strchr(oldname, '/')) == NULL) {
|
2010-01-11 20:05:38 +00:00
|
|
|
printf(" (IGNORED)\n");
|
|
|
|
return (0);
|
|
|
|
}
|
2010-01-11 23:33:30 +00:00
|
|
|
cmd.value = (intptr_t)(cp + 1);
|
2010-01-11 20:05:38 +00:00
|
|
|
cmd.size = inp->i_number; /* verify same name */
|
|
|
|
if (sysctlbyname("vfs.ffs.unlink", 0, 0,
|
|
|
|
&cmd, sizeof cmd) == -1) {
|
|
|
|
printf(" (UNLINK FAILED: %s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
printf(" (REMOVED)\n");
|
|
|
|
inp->i_parent = idesc->id_number; /* reparent to correct dir */
|
2010-01-11 19:52:40 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (!preen && !reply("REMOVE"))
|
|
|
|
return (0);
|
|
|
|
memset(&dotdesc, 0, sizeof(struct inodesc));
|
|
|
|
dotdesc.id_type = DATA;
|
|
|
|
dotdesc.id_number = inp->i_parent; /* directory in which name appears */
|
|
|
|
dotdesc.id_parent = inp->i_number; /* inode number in entry to delete */
|
|
|
|
dotdesc.id_func = deleteentry;
|
2021-01-07 01:37:08 +00:00
|
|
|
ginode(dotdesc.id_number, &ip);
|
|
|
|
if ((ckinode(ip.i_dp, &dotdesc) & FOUND) && preen)
|
2010-01-11 19:52:40 +00:00
|
|
|
printf(" (REMOVED)\n");
|
2021-01-07 01:37:08 +00:00
|
|
|
irelse(&ip);
|
2010-01-11 19:52:40 +00:00
|
|
|
inp->i_parent = idesc->id_number; /* reparent to correct directory */
|
|
|
|
inoinfo(inp->i_number)->ino_linkcnt++; /* name gone, return reference */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
deleteentry(struct inodesc *idesc)
|
|
|
|
{
|
|
|
|
struct direct *dirp = idesc->id_dirp;
|
|
|
|
|
|
|
|
if (idesc->id_entryno++ < 2 || dirp->d_ino != idesc->id_parent)
|
|
|
|
return (KEEPON);
|
|
|
|
dirp->d_ino = 0;
|
|
|
|
return (ALTERED|STOP|FOUND);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Routine to sort disk blocks.
|
|
|
|
*/
|
1997-03-11 12:20:21 +00:00
|
|
|
static int
|
2002-03-20 22:57:10 +00:00
|
|
|
blksort(const void *arg1, const void *arg2)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
2002-07-30 13:01:25 +00:00
|
|
|
return ((*(struct inoinfo * const *)arg1)->i_blks[0] -
|
|
|
|
(*(struct inoinfo * const *)arg2)->i_blks[0]);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|