2015-04-29 04:49:25 +03:00
|
|
|
/* $NetBSD: mkfs.c,v 1.124 2015/04/29 01:49:25 christos Exp $ */
|
2003-08-07 14:04:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2003-04-02 14:39:19 +04:00
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Marshall
|
|
|
|
* Kirk McKusick and Network Associates Laboratories, the Security
|
|
|
|
* Research Division of Network Associates, Inc. under DARPA/SPAWAR
|
|
|
|
* contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
|
|
|
|
* research program
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-01 02:20:30 +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 18:05:39 +04:00
|
|
|
static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2015-04-29 04:49:25 +03:00
|
|
|
__RCSID("$NetBSD: mkfs.c,v 1.124 2015/04/29 01:49:25 christos 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>
|
2002-01-07 15:00:09 +03:00
|
|
|
#include <sys/mman.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
1998-03-18 20:10:15 +03:00
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
2011-03-06 20:08:10 +03:00
|
|
|
#include <ufs/ufs/quota2.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
#include <ufs/ffs/fs.h>
|
1998-03-18 20:10:15 +03:00
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
2006-01-15 22:49:25 +03:00
|
|
|
#include <sys/ioctl.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/disklabel.h>
|
|
|
|
|
2002-01-07 15:00:09 +03:00
|
|
|
#include <err.h>
|
2001-10-14 05:38:53 +04:00
|
|
|
#include <errno.h>
|
1994-12-18 08:09:39 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-07-01 02:20:30 +04:00
|
|
|
#include <stdlib.h>
|
2003-09-10 21:25:14 +04:00
|
|
|
#include <stddef.h>
|
1994-12-18 08:09:39 +03:00
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
#ifndef STANDALONE
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
2000-12-01 15:03:27 +03:00
|
|
|
|
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode {
|
|
|
|
struct ufs1_dinode dp1;
|
|
|
|
struct ufs2_dinode dp2;
|
|
|
|
};
|
|
|
|
|
2003-05-02 07:26:11 +04:00
|
|
|
static void initcg(int, const struct timeval *);
|
|
|
|
static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
|
2015-04-29 04:49:25 +03:00
|
|
|
union Buffer;
|
|
|
|
static int makedir(union Buffer *, struct direct *, int);
|
2000-12-01 14:52:54 +03:00
|
|
|
static daddr_t alloc(int, int);
|
2003-04-02 14:39:19 +04:00
|
|
|
static void iput(union dinode *, ino_t);
|
2000-12-01 14:52:54 +03:00
|
|
|
static void rdfs(daddr_t, int, void *);
|
|
|
|
static void wtfs(daddr_t, int, void *);
|
|
|
|
static int isblock(struct fs *, unsigned char *, int);
|
|
|
|
static void clrblock(struct fs *, unsigned char *, int);
|
|
|
|
static void setblock(struct fs *, unsigned char *, int);
|
2003-04-02 14:39:19 +04:00
|
|
|
static int ilog2(int);
|
2003-09-10 21:25:14 +04:00
|
|
|
static void zap_old_sblock(int);
|
2002-01-18 11:59:18 +03:00
|
|
|
#ifdef MFS
|
2002-01-07 15:00:09 +03:00
|
|
|
static void *mkfs_malloc(size_t size);
|
2002-01-18 11:59:18 +03:00
|
|
|
#endif
|
1997-09-16 18:05:39 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-06-08 23:27:32 +04:00
|
|
|
* make file system for cylinder-group style file systems
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2002-01-07 15:00:09 +03:00
|
|
|
#define UMASK 0755
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct fs fs;
|
2014-04-26 18:15:08 +04:00
|
|
|
char data[SBLOCKSIZE];
|
2015-04-28 18:15:53 +03:00
|
|
|
} *fsun;
|
|
|
|
#define sblock fsun->fs
|
2003-08-15 19:24:21 +04:00
|
|
|
|
2015-04-28 18:15:53 +03:00
|
|
|
union Buffer {
|
2014-04-26 18:15:08 +04:00
|
|
|
struct quota2_header q2h;
|
|
|
|
char data[MAXBSIZE];
|
2015-04-28 18:15:53 +03:00
|
|
|
};
|
2014-04-26 18:15:08 +04:00
|
|
|
|
2003-08-15 19:24:21 +04:00
|
|
|
struct csum *fscs_0; /* first block of cylinder summaries */
|
|
|
|
struct csum *fscs_next; /* place for next summary */
|
|
|
|
struct csum *fscs_end; /* end of summary buffer */
|
|
|
|
struct csum *fscs_reset; /* place for next summary after write */
|
|
|
|
uint fs_csaddr; /* fragment number to write to */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct cg cg;
|
|
|
|
char pad[MAXBSIZE];
|
2015-04-28 18:15:53 +03:00
|
|
|
} *cgun;
|
|
|
|
#define acg cgun->cg
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
#define DIP(dp, field) \
|
|
|
|
((sblock.fs_magic == FS_UFS1_MAGIC) ? \
|
|
|
|
(dp)->dp1.di_##field : (dp)->dp2.di_##field)
|
|
|
|
|
2012-02-07 18:14:45 +04:00
|
|
|
#define EXT2FS_SBOFF 1024 /* XXX: SBOFF in <ufs/ext2fs/ext2fs.h> */
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
char *iobuf;
|
2004-03-18 23:35:55 +03:00
|
|
|
int iobufsize; /* size to end of 2nd inode block */
|
|
|
|
int iobuf_memsize; /* Actual buffer size */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
int fsi, fso;
|
|
|
|
|
2010-08-09 21:20:57 +04:00
|
|
|
static void
|
|
|
|
fserr(int num)
|
|
|
|
{
|
|
|
|
#ifdef GARBAGE
|
|
|
|
extern int Gflag;
|
|
|
|
|
|
|
|
if (Gflag)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
exit(num);
|
|
|
|
}
|
|
|
|
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2006-08-27 02:03:47 +04:00
|
|
|
mkfs(const char *fsys, int fi, int fo,
|
2002-01-07 15:00:09 +03:00
|
|
|
mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-04-11 11:20:09 +04:00
|
|
|
uint fragsperinodeblk, ncg, u;
|
2003-08-21 18:55:03 +04:00
|
|
|
uint cgzero;
|
|
|
|
uint64_t inodeblks, cgall;
|
2003-04-02 14:39:19 +04:00
|
|
|
int32_t cylno, i, csfrags;
|
2006-01-12 01:03:51 +03:00
|
|
|
int inodes_per_cg;
|
2003-05-02 07:26:11 +04:00
|
|
|
struct timeval tv;
|
2001-08-25 05:42:46 +04:00
|
|
|
long long sizepb;
|
2006-01-15 22:49:25 +03:00
|
|
|
int len, col, delta, fld_width, max_cols;
|
|
|
|
struct winsize winsize;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifndef STANDALONE
|
2003-05-02 07:26:11 +04:00
|
|
|
gettimeofday(&tv, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
2002-01-18 11:59:18 +03:00
|
|
|
#ifdef MFS
|
2004-03-07 15:26:38 +03:00
|
|
|
if (mfs && !Nflag) {
|
2007-12-09 00:40:23 +03:00
|
|
|
if ((membase = mkfs_malloc(fssize * sectorsize)) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(12);
|
|
|
|
}
|
2002-01-18 11:59:18 +03:00
|
|
|
#endif
|
2015-04-29 04:49:25 +03:00
|
|
|
if ((fsun = calloc(1, sizeof(*fsun))) == NULL)
|
2015-04-28 18:15:53 +03:00
|
|
|
exit(12);
|
2015-04-29 04:49:25 +03:00
|
|
|
if ((cgun = calloc(1, sizeof(*cgun))) == NULL)
|
2015-04-28 18:15:53 +03:00
|
|
|
exit(12);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
fsi = fi;
|
|
|
|
fso = fo;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag == 0) {
|
|
|
|
sblock.fs_old_inodefmt = FS_42INODEFMT;
|
1994-06-08 23:27:32 +04:00
|
|
|
sblock.fs_maxsymlinklen = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_old_flags = 0;
|
1994-06-08 23:27:32 +04:00
|
|
|
} else {
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_old_inodefmt = FS_44INODEFMT;
|
2013-01-22 13:39:11 +04:00
|
|
|
sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN :
|
|
|
|
UFS2_MAXSYMLINKLEN);
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_old_flags = FS_FLAGS_UPDATED;
|
2004-04-15 02:06:33 +04:00
|
|
|
if (isappleufs)
|
|
|
|
sblock.fs_old_flags = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_flags = 0;
|
1994-06-08 23:27:32 +04:00
|
|
|
}
|
2002-09-29 00:11:05 +04:00
|
|
|
|
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in
FreeBSD (three commits; the initial work, man page updates, and a fix
to ffs_reload()), with the following differences:
- Be consistent between newfs(8) and tunefs(8) as to the options which
set and control the tuning parameters for this work (avgfilesize & avgfpdir)
- Use u_int16_t instead of u_int8_t to keep track of the number of
contiguous directories (suggested by Chuck Silvers)
- Work within our FFS_EI framework
- Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to
the same area of memory
The new algorithm has a marked performance increase, especially when
performing tasks such as untarring pkgsrc.tar.gz, etc.
The original FreeBSD commit messages are attached:
=====
mckusick 2001/04/10 01:39:00 PDT
Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>.
His description of the problem and solution follow. My own tests show
speedups on typical filesystem intensive workloads of 5% to 12% which
is very impressive considering the small amount of code change involved.
------
One day I noticed that some file operations run much faster on
small file systems then on big ones. I've looked at the ffs
algorithms, thought about them, and redesigned the dirpref algorithm.
First I want to describe the results of my tests. These results are old
and I have improved the algorithm after these tests were done. Nevertheless
they show how big the perfomance speedup may be. I have done two file/directory
intensive tests on a two OpenBSD systems with old and new dirpref algorithm.
The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports".
The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release.
It contains 6596 directories and 13868 files. The test systems are:
1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for
test is at wd1. Size of test file system is 8 Gb, number of cg=991,
size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current
from Dec 2000 with BUFCACHEPERCENT=35
2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system
at wd0, file system for test is at wd1. Size of test file system is 40 Gb,
number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k
OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50
You can get more info about the test systems and methods at:
http://www.ptci.ru/gluk/dirpref/old/dirpref.html
Test Results
tar -xzf ports.tar.gz rm -rf ports
mode old dirpref new dirpref speedup old dirprefnew dirpref speedup
First system
normal 667 472 1.41 477 331 1.44
async 285 144 1.98 130 14 9.29
sync 768 616 1.25 477 334 1.43
softdep 413 252 1.64 241 38 6.34
Second system
normal 329 81 4.06 263.5 93.5 2.81
async 302 25.7 11.75 112 2.26 49.56
sync 281 57.0 4.93 263 90.5 2.9
softdep 341 40.6 8.4 284 4.76 59.66
"old dirpref" and "new dirpref" columns give a test time in seconds.
speedup - speed increasement in times, ie. old dirpref / new dirpref.
------
Algorithm description
The old dirpref algorithm is described in comments:
/*
* Find a cylinder to place a directory.
*
* The policy implemented by this algorithm is to select from
* among those cylinder groups with above the average number of
* free inodes, the one with the smallest number of directories.
*/
A new directory is allocated in a different cylinder groups than its
parent directory resulting in a directory tree that is spreaded across
all the cylinder groups. This spreading out results in a non-optimal
access to the directories and files. When we have a small filesystem
it is not a problem but when the filesystem is big then perfomance
degradation becomes very apparent.
What I mean by a big file system ?
1. A big filesystem is a filesystem which occupy 20-30 or more percent
of total drive space, i.e. first and last cylinder are physically
located relatively far from each other.
2. It has a relatively large number of cylinder groups, for example
more cylinder groups than 50% of the buffers in the buffer cache.
The first results in long access times, while the second results in
many buffers being used by metadata operations. Such operations use
cylinder group blocks and on-disk inode blocks. The cylinder group
block (fs->fs_cblkno) contains struct cg, inode and block bit maps.
It is 2k in size for the default filesystem parameters. If new and
parent directories are located in different cylinder groups then the
system performs more input/output operations and uses more buffers.
On filesystems with many cylinder groups, lots of cache buffers are
used for metadata operations.
My solution for this problem is very simple. I allocate many directories
in one cylinder group. I also do some things, so that the new allocation
method does not cause excessive fragmentation and all directory inodes
will not be located at a location far from its file's inodes and data.
The algorithm is:
/*
* Find a cylinder group to place a directory.
*
* The policy implemented by this algorithm is to allocate a
* directory inode in the same cylinder group as its parent
* directory, but also to reserve space for its files inodes
* and data. Restrict the number of directories which may be
* allocated one after another in the same cylinder group
* without intervening allocation of files.
*
* If we allocate a first level directory then force allocation
* in another cylinder group.
*/
My early versions of dirpref give me a good results for a wide range of
file operations and different filesystem capacities except one case:
those applications that create their entire directory structure first
and only later fill this structure with files.
My solution for such and similar cases is to limit a number of
directories which may be created one after another in the same cylinder
group without intervening file creations. For this purpose, I allocate
an array of counters at mount time. This array is linked to the superblock
fs->fs_contigdirs[cg]. Each time a directory is created the counter
increases and each time a file is created the counter decreases. A 60Gb
filesystem with 8mb/cg requires 10kb of memory for the counters array.
The maxcontigdirs is a maximum number of directories which may be created
without an intervening file creation. I found in my tests that the best
performance occurs when I restrict the number of directories in one cylinder
group such that all its files may be located in the same cylinder group.
There may be some deterioration in performance if all the file inodes
are in the same cylinder group as its containing directory, but their
data partially resides in a different cylinder group. The maxcontigdirs
value is calculated to try to prevent this condition. Since there is
no way to know how many files and directories will be allocated later
I added two optimization parameters in superblock/tunefs. They are:
int32_t fs_avgfilesize; /* expected average file size */
int32_t fs_avgfpdir; /* expected # of files per directory */
These parameters have reasonable defaults but may be tweeked for special
uses of a filesystem. They are only necessary in rare cases like better
tuning a filesystem being used to store a squid cache.
I have been using this algorithm for about 3 months. I have done
a lot of testing on filesystems with different capacities, average
filesize, average number of files per directory, and so on. I think
this algorithm has no negative impact on filesystem perfomance. It
works better than the default one in all cases. The new dirpref
will greatly improve untarring/removing/coping of big directories,
decrease load on cvs servers and much more. The new dirpref doesn't
speedup a compilation process, but also doesn't slow it down.
Obtained from: Grigoriy Orlov <gluk@ptci.ru>
=====
=====
iedowse 2001/04/23 17:37:17 PDT
Pre-dirpref versions of fsck may zero out the new superblock fields
fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause
panics if these fields were zeroed while a filesystem was mounted
read-only, and then remounted read-write.
Add code to ffs_reload() which copies the fs_contigdirs pointer
from the previous superblock, and reinitialises fs_avgf* if necessary.
Reviewed by: mckusick
=====
=====
nik 2001/04/10 03:36:44 PDT
Add information about the new options to newfs and tunefs which set the
expected average file size and number of files per directory. Could do
with some fleshing out.
=====
2001-09-06 06:16:00 +04:00
|
|
|
/*
|
|
|
|
* collect and verify the filesystem density info
|
|
|
|
*/
|
|
|
|
sblock.fs_avgfilesize = avgfilesize;
|
|
|
|
sblock.fs_avgfpdir = avgfpdir;
|
2003-08-15 19:07:16 +04:00
|
|
|
if (sblock.fs_avgfilesize <= 0) {
|
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in
FreeBSD (three commits; the initial work, man page updates, and a fix
to ffs_reload()), with the following differences:
- Be consistent between newfs(8) and tunefs(8) as to the options which
set and control the tuning parameters for this work (avgfilesize & avgfpdir)
- Use u_int16_t instead of u_int8_t to keep track of the number of
contiguous directories (suggested by Chuck Silvers)
- Work within our FFS_EI framework
- Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to
the same area of memory
The new algorithm has a marked performance increase, especially when
performing tasks such as untarring pkgsrc.tar.gz, etc.
The original FreeBSD commit messages are attached:
=====
mckusick 2001/04/10 01:39:00 PDT
Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>.
His description of the problem and solution follow. My own tests show
speedups on typical filesystem intensive workloads of 5% to 12% which
is very impressive considering the small amount of code change involved.
------
One day I noticed that some file operations run much faster on
small file systems then on big ones. I've looked at the ffs
algorithms, thought about them, and redesigned the dirpref algorithm.
First I want to describe the results of my tests. These results are old
and I have improved the algorithm after these tests were done. Nevertheless
they show how big the perfomance speedup may be. I have done two file/directory
intensive tests on a two OpenBSD systems with old and new dirpref algorithm.
The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports".
The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release.
It contains 6596 directories and 13868 files. The test systems are:
1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for
test is at wd1. Size of test file system is 8 Gb, number of cg=991,
size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current
from Dec 2000 with BUFCACHEPERCENT=35
2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system
at wd0, file system for test is at wd1. Size of test file system is 40 Gb,
number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k
OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50
You can get more info about the test systems and methods at:
http://www.ptci.ru/gluk/dirpref/old/dirpref.html
Test Results
tar -xzf ports.tar.gz rm -rf ports
mode old dirpref new dirpref speedup old dirprefnew dirpref speedup
First system
normal 667 472 1.41 477 331 1.44
async 285 144 1.98 130 14 9.29
sync 768 616 1.25 477 334 1.43
softdep 413 252 1.64 241 38 6.34
Second system
normal 329 81 4.06 263.5 93.5 2.81
async 302 25.7 11.75 112 2.26 49.56
sync 281 57.0 4.93 263 90.5 2.9
softdep 341 40.6 8.4 284 4.76 59.66
"old dirpref" and "new dirpref" columns give a test time in seconds.
speedup - speed increasement in times, ie. old dirpref / new dirpref.
------
Algorithm description
The old dirpref algorithm is described in comments:
/*
* Find a cylinder to place a directory.
*
* The policy implemented by this algorithm is to select from
* among those cylinder groups with above the average number of
* free inodes, the one with the smallest number of directories.
*/
A new directory is allocated in a different cylinder groups than its
parent directory resulting in a directory tree that is spreaded across
all the cylinder groups. This spreading out results in a non-optimal
access to the directories and files. When we have a small filesystem
it is not a problem but when the filesystem is big then perfomance
degradation becomes very apparent.
What I mean by a big file system ?
1. A big filesystem is a filesystem which occupy 20-30 or more percent
of total drive space, i.e. first and last cylinder are physically
located relatively far from each other.
2. It has a relatively large number of cylinder groups, for example
more cylinder groups than 50% of the buffers in the buffer cache.
The first results in long access times, while the second results in
many buffers being used by metadata operations. Such operations use
cylinder group blocks and on-disk inode blocks. The cylinder group
block (fs->fs_cblkno) contains struct cg, inode and block bit maps.
It is 2k in size for the default filesystem parameters. If new and
parent directories are located in different cylinder groups then the
system performs more input/output operations and uses more buffers.
On filesystems with many cylinder groups, lots of cache buffers are
used for metadata operations.
My solution for this problem is very simple. I allocate many directories
in one cylinder group. I also do some things, so that the new allocation
method does not cause excessive fragmentation and all directory inodes
will not be located at a location far from its file's inodes and data.
The algorithm is:
/*
* Find a cylinder group to place a directory.
*
* The policy implemented by this algorithm is to allocate a
* directory inode in the same cylinder group as its parent
* directory, but also to reserve space for its files inodes
* and data. Restrict the number of directories which may be
* allocated one after another in the same cylinder group
* without intervening allocation of files.
*
* If we allocate a first level directory then force allocation
* in another cylinder group.
*/
My early versions of dirpref give me a good results for a wide range of
file operations and different filesystem capacities except one case:
those applications that create their entire directory structure first
and only later fill this structure with files.
My solution for such and similar cases is to limit a number of
directories which may be created one after another in the same cylinder
group without intervening file creations. For this purpose, I allocate
an array of counters at mount time. This array is linked to the superblock
fs->fs_contigdirs[cg]. Each time a directory is created the counter
increases and each time a file is created the counter decreases. A 60Gb
filesystem with 8mb/cg requires 10kb of memory for the counters array.
The maxcontigdirs is a maximum number of directories which may be created
without an intervening file creation. I found in my tests that the best
performance occurs when I restrict the number of directories in one cylinder
group such that all its files may be located in the same cylinder group.
There may be some deterioration in performance if all the file inodes
are in the same cylinder group as its containing directory, but their
data partially resides in a different cylinder group. The maxcontigdirs
value is calculated to try to prevent this condition. Since there is
no way to know how many files and directories will be allocated later
I added two optimization parameters in superblock/tunefs. They are:
int32_t fs_avgfilesize; /* expected average file size */
int32_t fs_avgfpdir; /* expected # of files per directory */
These parameters have reasonable defaults but may be tweeked for special
uses of a filesystem. They are only necessary in rare cases like better
tuning a filesystem being used to store a squid cache.
I have been using this algorithm for about 3 months. I have done
a lot of testing on filesystems with different capacities, average
filesize, average number of files per directory, and so on. I think
this algorithm has no negative impact on filesystem perfomance. It
works better than the default one in all cases. The new dirpref
will greatly improve untarring/removing/coping of big directories,
decrease load on cvs servers and much more. The new dirpref doesn't
speedup a compilation process, but also doesn't slow it down.
Obtained from: Grigoriy Orlov <gluk@ptci.ru>
=====
=====
iedowse 2001/04/23 17:37:17 PDT
Pre-dirpref versions of fsck may zero out the new superblock fields
fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause
panics if these fields were zeroed while a filesystem was mounted
read-only, and then remounted read-write.
Add code to ffs_reload() which copies the fs_contigdirs pointer
from the previous superblock, and reinitialises fs_avgf* if necessary.
Reviewed by: mckusick
=====
=====
nik 2001/04/10 03:36:44 PDT
Add information about the new options to newfs and tunefs which set the
expected average file size and number of files per directory. Could do
with some fleshing out.
=====
2001-09-06 06:16:00 +04:00
|
|
|
printf("illegal expected average file size %d\n",
|
2003-08-15 19:07:16 +04:00
|
|
|
sblock.fs_avgfilesize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(14);
|
2003-08-15 19:07:16 +04:00
|
|
|
}
|
|
|
|
if (sblock.fs_avgfpdir <= 0) {
|
Incorporate the enhanced ffs_dirpref() by Grigoriy Orlov, as found in
FreeBSD (three commits; the initial work, man page updates, and a fix
to ffs_reload()), with the following differences:
- Be consistent between newfs(8) and tunefs(8) as to the options which
set and control the tuning parameters for this work (avgfilesize & avgfpdir)
- Use u_int16_t instead of u_int8_t to keep track of the number of
contiguous directories (suggested by Chuck Silvers)
- Work within our FFS_EI framework
- Ensure that fs->fs_maxclusters and fs->fs_contigdirs don't point to
the same area of memory
The new algorithm has a marked performance increase, especially when
performing tasks such as untarring pkgsrc.tar.gz, etc.
The original FreeBSD commit messages are attached:
=====
mckusick 2001/04/10 01:39:00 PDT
Directory layout preference improvements from Grigoriy Orlov <gluk@ptci.ru>.
His description of the problem and solution follow. My own tests show
speedups on typical filesystem intensive workloads of 5% to 12% which
is very impressive considering the small amount of code change involved.
------
One day I noticed that some file operations run much faster on
small file systems then on big ones. I've looked at the ffs
algorithms, thought about them, and redesigned the dirpref algorithm.
First I want to describe the results of my tests. These results are old
and I have improved the algorithm after these tests were done. Nevertheless
they show how big the perfomance speedup may be. I have done two file/directory
intensive tests on a two OpenBSD systems with old and new dirpref algorithm.
The first test is "tar -xzf ports.tar.gz", the second is "rm -rf ports".
The ports.tar.gz file is the ports collection from the OpenBSD 2.8 release.
It contains 6596 directories and 13868 files. The test systems are:
1. Celeron-450, 128Mb, two IDE drives, the system at wd0, file system for
test is at wd1. Size of test file system is 8 Gb, number of cg=991,
size of cg is 8m, block size = 8k, fragment size = 1k OpenBSD-current
from Dec 2000 with BUFCACHEPERCENT=35
2. PIII-600, 128Mb, two IBM DTLA-307045 IDE drives at i815e, the system
at wd0, file system for test is at wd1. Size of test file system is 40 Gb,
number of cg=5324, size of cg is 8m, block size = 8k, fragment size = 1k
OpenBSD-current from Dec 2000 with BUFCACHEPERCENT=50
You can get more info about the test systems and methods at:
http://www.ptci.ru/gluk/dirpref/old/dirpref.html
Test Results
tar -xzf ports.tar.gz rm -rf ports
mode old dirpref new dirpref speedup old dirprefnew dirpref speedup
First system
normal 667 472 1.41 477 331 1.44
async 285 144 1.98 130 14 9.29
sync 768 616 1.25 477 334 1.43
softdep 413 252 1.64 241 38 6.34
Second system
normal 329 81 4.06 263.5 93.5 2.81
async 302 25.7 11.75 112 2.26 49.56
sync 281 57.0 4.93 263 90.5 2.9
softdep 341 40.6 8.4 284 4.76 59.66
"old dirpref" and "new dirpref" columns give a test time in seconds.
speedup - speed increasement in times, ie. old dirpref / new dirpref.
------
Algorithm description
The old dirpref algorithm is described in comments:
/*
* Find a cylinder to place a directory.
*
* The policy implemented by this algorithm is to select from
* among those cylinder groups with above the average number of
* free inodes, the one with the smallest number of directories.
*/
A new directory is allocated in a different cylinder groups than its
parent directory resulting in a directory tree that is spreaded across
all the cylinder groups. This spreading out results in a non-optimal
access to the directories and files. When we have a small filesystem
it is not a problem but when the filesystem is big then perfomance
degradation becomes very apparent.
What I mean by a big file system ?
1. A big filesystem is a filesystem which occupy 20-30 or more percent
of total drive space, i.e. first and last cylinder are physically
located relatively far from each other.
2. It has a relatively large number of cylinder groups, for example
more cylinder groups than 50% of the buffers in the buffer cache.
The first results in long access times, while the second results in
many buffers being used by metadata operations. Such operations use
cylinder group blocks and on-disk inode blocks. The cylinder group
block (fs->fs_cblkno) contains struct cg, inode and block bit maps.
It is 2k in size for the default filesystem parameters. If new and
parent directories are located in different cylinder groups then the
system performs more input/output operations and uses more buffers.
On filesystems with many cylinder groups, lots of cache buffers are
used for metadata operations.
My solution for this problem is very simple. I allocate many directories
in one cylinder group. I also do some things, so that the new allocation
method does not cause excessive fragmentation and all directory inodes
will not be located at a location far from its file's inodes and data.
The algorithm is:
/*
* Find a cylinder group to place a directory.
*
* The policy implemented by this algorithm is to allocate a
* directory inode in the same cylinder group as its parent
* directory, but also to reserve space for its files inodes
* and data. Restrict the number of directories which may be
* allocated one after another in the same cylinder group
* without intervening allocation of files.
*
* If we allocate a first level directory then force allocation
* in another cylinder group.
*/
My early versions of dirpref give me a good results for a wide range of
file operations and different filesystem capacities except one case:
those applications that create their entire directory structure first
and only later fill this structure with files.
My solution for such and similar cases is to limit a number of
directories which may be created one after another in the same cylinder
group without intervening file creations. For this purpose, I allocate
an array of counters at mount time. This array is linked to the superblock
fs->fs_contigdirs[cg]. Each time a directory is created the counter
increases and each time a file is created the counter decreases. A 60Gb
filesystem with 8mb/cg requires 10kb of memory for the counters array.
The maxcontigdirs is a maximum number of directories which may be created
without an intervening file creation. I found in my tests that the best
performance occurs when I restrict the number of directories in one cylinder
group such that all its files may be located in the same cylinder group.
There may be some deterioration in performance if all the file inodes
are in the same cylinder group as its containing directory, but their
data partially resides in a different cylinder group. The maxcontigdirs
value is calculated to try to prevent this condition. Since there is
no way to know how many files and directories will be allocated later
I added two optimization parameters in superblock/tunefs. They are:
int32_t fs_avgfilesize; /* expected average file size */
int32_t fs_avgfpdir; /* expected # of files per directory */
These parameters have reasonable defaults but may be tweeked for special
uses of a filesystem. They are only necessary in rare cases like better
tuning a filesystem being used to store a squid cache.
I have been using this algorithm for about 3 months. I have done
a lot of testing on filesystems with different capacities, average
filesize, average number of files per directory, and so on. I think
this algorithm has no negative impact on filesystem perfomance. It
works better than the default one in all cases. The new dirpref
will greatly improve untarring/removing/coping of big directories,
decrease load on cvs servers and much more. The new dirpref doesn't
speedup a compilation process, but also doesn't slow it down.
Obtained from: Grigoriy Orlov <gluk@ptci.ru>
=====
=====
iedowse 2001/04/23 17:37:17 PDT
Pre-dirpref versions of fsck may zero out the new superblock fields
fs_contigdirs, fs_avgfilesize and fs_avgfpdir. This could cause
panics if these fields were zeroed while a filesystem was mounted
read-only, and then remounted read-write.
Add code to ffs_reload() which copies the fs_contigdirs pointer
from the previous superblock, and reinitialises fs_avgf* if necessary.
Reviewed by: mckusick
=====
=====
nik 2001/04/10 03:36:44 PDT
Add information about the new options to newfs and tunefs which set the
expected average file size and number of files per directory. Could do
with some fleshing out.
=====
2001-09-06 06:16:00 +04:00
|
|
|
printf("illegal expected number of files per directory %d\n",
|
2003-08-15 19:07:16 +04:00
|
|
|
sblock.fs_avgfpdir);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(15);
|
2003-08-15 19:07:16 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* collect and verify the block and fragment sizes
|
|
|
|
*/
|
|
|
|
sblock.fs_bsize = bsize;
|
|
|
|
sblock.fs_fsize = fsize;
|
2007-11-27 16:31:10 +03:00
|
|
|
if (!powerof2(sblock.fs_bsize)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("block size must be a power of 2, not %d\n",
|
|
|
|
sblock.fs_bsize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(16);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2007-11-27 16:31:10 +03:00
|
|
|
if (!powerof2(sblock.fs_fsize)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("fragment size must be a power of 2, not %d\n",
|
|
|
|
sblock.fs_fsize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(17);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (sblock.fs_fsize < sectorsize) {
|
|
|
|
printf("fragment size %d is too small, minimum is %d\n",
|
|
|
|
sblock.fs_fsize, sectorsize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(18);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (sblock.fs_bsize < MINBSIZE) {
|
|
|
|
printf("block size %d is too small, minimum is %d\n",
|
|
|
|
sblock.fs_bsize, MINBSIZE);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(19);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2001-12-13 09:31:31 +03:00
|
|
|
if (sblock.fs_bsize > MAXBSIZE) {
|
|
|
|
printf("block size %d is too large, maximum is %d\n",
|
|
|
|
sblock.fs_bsize, MAXBSIZE);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(19);
|
2001-12-13 09:31:31 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (sblock.fs_bsize < sblock.fs_fsize) {
|
|
|
|
printf("block size (%d) cannot be smaller than fragment size (%d)\n",
|
|
|
|
sblock.fs_bsize, sblock.fs_fsize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(20);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2007-11-27 16:31:10 +03:00
|
|
|
if (maxbsize < bsize || !powerof2(maxbsize)) {
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_maxbsize = sblock.fs_bsize;
|
|
|
|
} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
|
|
|
|
sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
|
|
|
|
} else {
|
|
|
|
sblock.fs_maxbsize = maxbsize;
|
|
|
|
}
|
|
|
|
sblock.fs_maxcontig = maxcontig;
|
|
|
|
if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
|
|
|
|
sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
|
2006-01-15 22:49:25 +03:00
|
|
|
if (verbosity > 0)
|
|
|
|
printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
|
|
|
if (sblock.fs_maxcontig > 1)
|
|
|
|
sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_bmask = ~(sblock.fs_bsize - 1);
|
|
|
|
sblock.fs_fmask = ~(sblock.fs_fsize - 1);
|
1994-06-08 23:27:32 +04:00
|
|
|
sblock.fs_qbmask = ~sblock.fs_bmask;
|
|
|
|
sblock.fs_qfmask = ~sblock.fs_fmask;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
|
|
|
|
sblock.fs_bshift++;
|
|
|
|
for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
|
|
|
|
sblock.fs_fshift++;
|
2013-06-23 11:28:36 +04:00
|
|
|
sblock.fs_frag = ffs_numfrags(&sblock, sblock.fs_bsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
|
|
|
|
sblock.fs_fragshift++;
|
|
|
|
if (sblock.fs_frag > MAXFRAG) {
|
1998-03-18 20:10:15 +03:00
|
|
|
printf("fragment size %d is too small, "
|
|
|
|
"minimum with block size %d is %d\n",
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_fsize, sblock.fs_bsize,
|
|
|
|
sblock.fs_bsize / MAXFRAG);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(21);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
|
2013-06-23 06:06:04 +04:00
|
|
|
sblock.fs_size = FFS_DBTOFSB(&sblock, fssize);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag <= 1) {
|
2009-04-11 11:20:09 +04:00
|
|
|
if ((uint64_t)sblock.fs_size >= 1ull << 31) {
|
2003-08-15 19:07:16 +04:00
|
|
|
printf("Too many fragments (0x%" PRIx64
|
2009-05-07 10:56:56 +04:00
|
|
|
") for a FFSv1 filesystem\n", sblock.fs_size);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(22);
|
2003-08-15 19:07:16 +04:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_magic = FS_UFS1_MAGIC;
|
|
|
|
sblock.fs_sblockloc = SBLOCK_UFS1;
|
|
|
|
sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
|
|
|
|
sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
|
|
|
|
sblock.fs_old_cgoffset = 0;
|
|
|
|
sblock.fs_old_cgmask = 0xffffffff;
|
|
|
|
sblock.fs_old_size = sblock.fs_size;
|
|
|
|
sblock.fs_old_rotdelay = 0;
|
|
|
|
sblock.fs_old_rps = 60;
|
|
|
|
sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
|
|
|
|
sblock.fs_old_cpg = 1;
|
|
|
|
sblock.fs_old_interleave = 1;
|
|
|
|
sblock.fs_old_trackskew = 0;
|
|
|
|
sblock.fs_old_cpc = 0;
|
2003-08-15 19:07:16 +04:00
|
|
|
sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_old_nrpos = 1;
|
|
|
|
} else {
|
|
|
|
sblock.fs_magic = FS_UFS2_MAGIC;
|
|
|
|
sblock.fs_sblockloc = SBLOCK_UFS2;
|
|
|
|
sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
|
|
|
|
sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_sblkno =
|
2003-04-02 14:39:19 +04:00
|
|
|
roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
|
|
|
|
sblock.fs_frag);
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
|
2003-04-02 14:39:19 +04:00
|
|
|
roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
|
2013-01-22 13:39:11 +04:00
|
|
|
sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
|
|
|
|
for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; 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);
|
1994-06-08 23:27:32 +04:00
|
|
|
sblock.fs_maxfilesize += sizepb;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2003-04-02 14:39:19 +04:00
|
|
|
* Calculate the number of blocks to put into each cylinder group.
|
|
|
|
*
|
2003-08-21 18:55:03 +04:00
|
|
|
* The cylinder group size is limited because the data structure
|
|
|
|
* must fit into a single block.
|
|
|
|
* We try to have as few cylinder groups as possible, with a proviso
|
|
|
|
* that we create at least MINCYLGRPS (==4) except for small
|
|
|
|
* filesystems.
|
2003-04-02 14:39:19 +04:00
|
|
|
*
|
2003-08-21 18:55:03 +04:00
|
|
|
* This algorithm works out how many blocks of inodes would be
|
|
|
|
* needed to fill the entire volume at the specified density.
|
|
|
|
* It then looks at how big the 'cylinder block' would have to
|
|
|
|
* be and, assuming that it is linearly related to the number
|
|
|
|
* of inodes and blocks how many cylinder groups are needed to
|
|
|
|
* keep the cylinder block below the filesystem block size.
|
|
|
|
*
|
|
|
|
* The cylinder groups are then all created with the average size.
|
|
|
|
*
|
|
|
|
* Space taken by the red tape on cylinder groups other than the
|
|
|
|
* first is ignored.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2003-08-21 18:55:03 +04:00
|
|
|
|
|
|
|
/* There must be space for 1 inode block and 2 data blocks */
|
|
|
|
if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
|
|
|
|
printf("Filesystem size %lld < minimum size of %d\n",
|
|
|
|
(long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(23);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-09-11 16:19:44 +04:00
|
|
|
if (num_inodes != 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
|
|
|
inodeblks = howmany(num_inodes, FFS_INOPB(&sblock));
|
2003-09-11 16:19:44 +04:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Calculate 'per inode block' so we can allocate less than
|
|
|
|
* 1 fragment per inode - useful for /dev.
|
|
|
|
*/
|
2013-06-23 11:28:36 +04:00
|
|
|
fragsperinodeblk = MAX(ffs_numfrags(&sblock,
|
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
|
|
|
(uint64_t)density * FFS_INOPB(&sblock)), 1);
|
2003-09-11 16:19:44 +04:00
|
|
|
inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
|
|
|
|
(sblock.fs_frag + fragsperinodeblk);
|
|
|
|
}
|
2003-08-21 18:55:03 +04:00
|
|
|
if (inodeblks == 0)
|
|
|
|
inodeblks = 1;
|
2003-09-11 16:19:44 +04:00
|
|
|
/* Ensure that there are at least 2 data blocks (or we fail below) */
|
2009-04-11 11:20:09 +04:00
|
|
|
if (inodeblks > (uint64_t)(sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
|
2003-09-11 16:19:44 +04:00
|
|
|
inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
|
2003-08-21 18:55:03 +04:00
|
|
|
/* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
|
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 (inodeblks * FFS_INOPB(&sblock) >= 1ull << 31)
|
|
|
|
inodeblks = ((1ull << 31) - NBBY) / FFS_INOPB(&sblock);
|
2003-08-21 18:55:03 +04:00
|
|
|
/*
|
|
|
|
* See what would happen if we tried to use 1 cylinder group.
|
|
|
|
* Assume space linear, so work out number of cylinder groups needed.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2003-08-21 18:55:03 +04:00
|
|
|
cgzero = CGSIZE_IF(&sblock, 0, 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
|
|
|
cgall = CGSIZE_IF(&sblock, inodeblks * FFS_INOPB(&sblock), sblock.fs_size);
|
2006-01-12 01:03:51 +03:00
|
|
|
ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero);
|
2003-08-21 18:55:03 +04:00
|
|
|
if (ncg < MINCYLGRPS) {
|
|
|
|
/*
|
|
|
|
* We would like to allocate MINCLYGRPS cylinder groups,
|
|
|
|
* but for small file sytems (especially ones with a lot
|
|
|
|
* of inodes) this is not desirable (or possible).
|
|
|
|
*/
|
2009-04-11 11:20:09 +04:00
|
|
|
u = sblock.fs_size / 2 / (sblock.fs_iblkno +
|
2003-08-21 18:55:03 +04:00
|
|
|
inodeblks * sblock.fs_frag);
|
2009-04-11 11:20:09 +04:00
|
|
|
if (u > ncg)
|
|
|
|
ncg = u;
|
2003-08-21 18:55:03 +04:00
|
|
|
if (ncg > MINCYLGRPS)
|
|
|
|
ncg = MINCYLGRPS;
|
|
|
|
if (ncg > inodeblks)
|
|
|
|
ncg = inodeblks;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
/*
|
2003-08-21 18:55:03 +04:00
|
|
|
* Put an equal number of blocks in each cylinder group.
|
|
|
|
* Round up so we don't have more fragments in the last CG than
|
|
|
|
* the earlier ones (does that matter?), but kill a block if the
|
|
|
|
* CGSIZE becomes too big (only happens if there are a lot of CGs).
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2003-08-21 18:55:03 +04:00
|
|
|
sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
|
2006-01-12 01:03:51 +03:00
|
|
|
/* Round up the fragments/group so the bitmap bytes are full */
|
|
|
|
sblock.fs_fpg = roundup(sblock.fs_fpg, NBBY);
|
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
|
|
|
inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
|
2006-01-12 01:03:51 +03:00
|
|
|
|
|
|
|
i = CGSIZE_IF(&sblock, inodes_per_cg, sblock.fs_fpg);
|
|
|
|
if (i > sblock.fs_bsize) {
|
2003-08-21 18:55:03 +04:00
|
|
|
sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
|
2006-01-12 01:03:51 +03:00
|
|
|
/* ... and recalculate how many cylinder groups we now need */
|
|
|
|
ncg = howmany(sblock.fs_size, sblock.fs_fpg);
|
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
|
|
|
inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
|
2006-01-12 01:03:51 +03:00
|
|
|
}
|
|
|
|
sblock.fs_ipg = inodes_per_cg;
|
2003-08-21 18:55:03 +04:00
|
|
|
/* Sanity check on our sums... */
|
2009-04-11 11:20:09 +04:00
|
|
|
if ((int)CGSIZE(&sblock) > sblock.fs_bsize) {
|
2003-08-21 18:55:03 +04:00
|
|
|
printf("CGSIZE miscalculated %d > %d\n",
|
|
|
|
(int)CGSIZE(&sblock), sblock.fs_bsize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(24);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2006-01-12 01:03:51 +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
|
|
|
sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / FFS_INOPF(&sblock);
|
2003-08-21 18:55:03 +04:00
|
|
|
/* Check that the last cylinder group has enough space for the inodes */
|
|
|
|
i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
|
2006-01-12 01:03:51 +03:00
|
|
|
if (i < sblock.fs_dblkno) {
|
2003-08-21 18:55:03 +04:00
|
|
|
/*
|
|
|
|
* Since we make all the cylinder groups the same size, the
|
|
|
|
* last will only be small if there are a large number of
|
|
|
|
* cylinder groups. If we pull even a fragment from each
|
|
|
|
* of the other groups then the last CG will be overfull.
|
|
|
|
* So we just kill the last CG.
|
|
|
|
*/
|
|
|
|
ncg--;
|
|
|
|
sblock.fs_size -= i;
|
|
|
|
}
|
|
|
|
sblock.fs_ncg = ncg;
|
|
|
|
|
2013-06-23 11:28:36 +04:00
|
|
|
sblock.fs_cgsize = ffs_fragroundup(&sblock, CGSIZE(&sblock));
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
|
|
|
|
sblock.fs_old_nsect = sblock.fs_old_spc;
|
|
|
|
sblock.fs_old_npsect = sblock.fs_old_spc;
|
|
|
|
sblock.fs_old_ncyl = sblock.fs_ncg;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2003-08-15 19:24:21 +04:00
|
|
|
* Cylinder group summary information for each cylinder is written
|
|
|
|
* into the first cylinder group.
|
|
|
|
* Write this fragment by fragment, but doing the first CG last
|
|
|
|
* (after we've taken stuff off for the structure itself and the
|
|
|
|
* root directory.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
sblock.fs_csaddr = cgdmin(&sblock, 0);
|
|
|
|
sblock.fs_cssize =
|
2013-06-23 11:28:36 +04:00
|
|
|
ffs_fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
|
2003-08-15 19:24:21 +04:00
|
|
|
if (512 % sizeof *fscs_0)
|
|
|
|
errx(1, "cylinder group summary doesn't fit in sectors");
|
2004-03-18 23:35:55 +03:00
|
|
|
fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
2007-12-09 00:40:23 +03:00
|
|
|
if (fscs_0 == MAP_FAILED)
|
2001-07-26 20:53:39 +04:00
|
|
|
exit(39);
|
2004-03-18 23:35:55 +03:00
|
|
|
memset(fscs_0, 0, 2 * sblock.fs_fsize);
|
2003-08-15 19:24:21 +04:00
|
|
|
fs_csaddr = sblock.fs_csaddr;
|
|
|
|
fscs_next = fscs_0;
|
|
|
|
fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
|
|
|
|
fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
|
|
|
|
/*
|
|
|
|
* fill in remaining fields of the super block
|
|
|
|
*/
|
2013-06-23 11:28:36 +04:00
|
|
|
sblock.fs_sbsize = ffs_fragroundup(&sblock, sizeof(struct fs));
|
2003-04-02 14:39:19 +04:00
|
|
|
if (sblock.fs_sbsize > SBLOCKSIZE)
|
|
|
|
sblock.fs_sbsize = SBLOCKSIZE;
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_minfree = minfree;
|
|
|
|
sblock.fs_maxcontig = maxcontig;
|
|
|
|
sblock.fs_maxbpg = maxbpg;
|
|
|
|
sblock.fs_optim = opt;
|
|
|
|
sblock.fs_cgrotor = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_pendingblocks = 0;
|
|
|
|
sblock.fs_pendinginodes = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_cstotal.cs_ndir = 0;
|
|
|
|
sblock.fs_cstotal.cs_nbfree = 0;
|
|
|
|
sblock.fs_cstotal.cs_nifree = 0;
|
|
|
|
sblock.fs_cstotal.cs_nffree = 0;
|
|
|
|
sblock.fs_fmod = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_ronly = 0;
|
|
|
|
sblock.fs_state = 0;
|
1995-04-13 01:24:31 +04:00
|
|
|
sblock.fs_clean = FS_ISCLEAN;
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_ronly = 0;
|
2003-05-02 07:26:11 +04:00
|
|
|
sblock.fs_id[0] = (long)tv.tv_sec; /* XXXfvdl huh? */
|
2003-09-04 19:31:58 +04:00
|
|
|
sblock.fs_id[1] = arc4random() & INT32_MAX;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_fsmnt[0] = '\0';
|
|
|
|
csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
|
|
|
|
sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
|
|
|
|
sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
|
|
|
|
sblock.fs_cstotal.cs_nbfree =
|
2013-06-24 02:03:34 +04:00
|
|
|
ffs_fragstoblks(&sblock, sblock.fs_dsize) -
|
2003-04-02 14:39:19 +04:00
|
|
|
howmany(csfrags, sblock.fs_frag);
|
|
|
|
sblock.fs_cstotal.cs_nffree =
|
2013-06-24 02:03:34 +04:00
|
|
|
ffs_fragnum(&sblock, sblock.fs_size) +
|
|
|
|
(ffs_fragnum(&sblock, csfrags) > 0 ?
|
|
|
|
sblock.fs_frag - ffs_fragnum(&sblock, csfrags) : 0);
|
2013-01-22 13:39:11 +04:00
|
|
|
sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_cstotal.cs_ndir = 0;
|
|
|
|
sblock.fs_dsize -= csfrags;
|
2003-05-02 07:26:11 +04:00
|
|
|
sblock.fs_time = tv.tv_sec;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag <= 1) {
|
2003-05-02 07:26:11 +04:00
|
|
|
sblock.fs_old_time = tv.tv_sec;
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_old_dsize = sblock.fs_dsize;
|
|
|
|
sblock.fs_old_csaddr = sblock.fs_csaddr;
|
|
|
|
sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
|
|
|
|
sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
|
|
|
|
sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
|
|
|
|
sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
|
|
|
|
}
|
2011-03-06 20:08:10 +03:00
|
|
|
/* add quota data in superblock */
|
|
|
|
if (quotas) {
|
|
|
|
sblock.fs_flags |= FS_DOQUOTA2;
|
|
|
|
sblock.fs_quota_magic = Q2_HEAD_MAGIC;
|
|
|
|
sblock.fs_quota_flags = quotas;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Dump out summary information about file system.
|
|
|
|
*/
|
2006-01-15 22:49:25 +03:00
|
|
|
if (verbosity > 0) {
|
2002-01-07 15:00:09 +03:00
|
|
|
#define B2MBFACTOR (1 / (1024.0 * 1024.0))
|
2003-04-02 14:39:19 +04:00
|
|
|
printf("%s: %.1fMB (%lld sectors) block size %d, "
|
|
|
|
"fragment size %d\n",
|
|
|
|
fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
|
2013-06-23 06:06:04 +04:00
|
|
|
(long long)FFS_FSBTODB(&sblock, sblock.fs_size),
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_bsize, sblock.fs_fsize);
|
|
|
|
printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
|
|
|
|
"%d inodes.\n",
|
|
|
|
sblock.fs_ncg,
|
1994-06-08 23:27:32 +04:00
|
|
|
(float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
|
2003-04-02 14:39:19 +04:00
|
|
|
sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
|
1994-06-08 23:27:32 +04:00
|
|
|
#undef B2MBFACTOR
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2003-04-02 14:39:19 +04:00
|
|
|
* allocate space for superblock, cylinder group map, and
|
|
|
|
* two sets of inode blocks.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
if (sblock.fs_bsize < SBLOCKSIZE)
|
|
|
|
iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
|
|
|
|
else
|
|
|
|
iobufsize = 4 * sblock.fs_bsize;
|
2004-03-18 23:35:55 +03:00
|
|
|
iobuf_memsize = iobufsize;
|
|
|
|
if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
/* A larger buffer so we can write multiple inode blks */
|
|
|
|
iobuf_memsize += 14 * sblock.fs_bsize;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
2007-12-09 00:40:23 +03:00
|
|
|
if (iobuf != MAP_FAILED)
|
2004-03-18 23:35:55 +03:00
|
|
|
break;
|
|
|
|
if (iobuf_memsize != iobufsize) {
|
|
|
|
/* Try again with the smaller size */
|
|
|
|
iobuf_memsize = iobufsize;
|
|
|
|
continue;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
printf("Cannot allocate I/O buffer\n");
|
|
|
|
exit(38);
|
|
|
|
}
|
2004-03-18 23:35:55 +03:00
|
|
|
memset(iobuf, 0, iobuf_memsize);
|
2003-09-10 21:25:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We now start writing to the filesystem
|
|
|
|
*/
|
|
|
|
|
2006-01-12 01:33:03 +03:00
|
|
|
if (!Nflag) {
|
|
|
|
/*
|
|
|
|
* Validate the given file system size.
|
|
|
|
* Verify that its last block can actually be accessed.
|
|
|
|
* Convert to file system fragment sized units.
|
|
|
|
*/
|
|
|
|
if (fssize <= 0) {
|
|
|
|
printf("preposterous size %lld\n", (long long)fssize);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(13);
|
2006-01-12 01:33:03 +03:00
|
|
|
}
|
|
|
|
wtfs(fssize - 1, sectorsize, iobuf);
|
2003-09-10 21:25:14 +04:00
|
|
|
|
2006-01-12 01:33:03 +03:00
|
|
|
/*
|
|
|
|
* Ensure there is nothing that looks like a filesystem
|
|
|
|
* superbock anywhere other than where ours will be.
|
|
|
|
* If fsck finds the wrong one all hell breaks loose!
|
|
|
|
*/
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
static const int sblocklist[] = SBLOCKSEARCH;
|
|
|
|
int sblkoff = sblocklist[i];
|
|
|
|
int sz;
|
|
|
|
if (sblkoff == -1)
|
|
|
|
break;
|
|
|
|
/* Remove main superblock */
|
|
|
|
zap_old_sblock(sblkoff);
|
|
|
|
/* and all possible locations for the first alternate */
|
|
|
|
sblkoff += SBLOCKSIZE;
|
|
|
|
for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
|
|
|
|
zap_old_sblock(roundup(sblkoff, sz));
|
|
|
|
}
|
2012-02-07 18:14:45 +04:00
|
|
|
/*
|
|
|
|
* Also zap possible Ext2fs magic leftover to prevent
|
|
|
|
* kernel vfs_mountroot() and bootloaders from mis-recognizing
|
|
|
|
* this file system as Ext2fs.
|
|
|
|
*/
|
|
|
|
zap_old_sblock(EXT2FS_SBOFF);
|
2003-09-10 21:25:14 +04:00
|
|
|
|
2006-01-12 01:33:03 +03:00
|
|
|
if (isappleufs) {
|
|
|
|
struct appleufslabel appleufs;
|
|
|
|
ffs_appleufs_set(&appleufs, appleufs_volname,
|
|
|
|
tv.tv_sec, 0);
|
|
|
|
wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
|
|
|
|
APPLEUFS_LABEL_SIZE, &appleufs);
|
2010-01-31 19:04:34 +03:00
|
|
|
} else if (APPLEUFS_LABEL_SIZE % sectorsize == 0) {
|
2006-01-12 01:33:03 +03:00
|
|
|
struct appleufslabel appleufs;
|
|
|
|
/* Look for & zap any existing valid apple ufs labels */
|
|
|
|
rdfs(APPLEUFS_LABEL_OFFSET/sectorsize,
|
|
|
|
APPLEUFS_LABEL_SIZE, &appleufs);
|
|
|
|
if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
|
|
|
|
memset(&appleufs, 0, sizeof(appleufs));
|
|
|
|
wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
|
|
|
|
APPLEUFS_LABEL_SIZE, &appleufs);
|
|
|
|
}
|
2004-01-02 08:08:57 +03:00
|
|
|
}
|
2003-09-10 21:25:14 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
/*
|
|
|
|
* Make a copy of the superblock into the buffer that we will be
|
|
|
|
* writing out in each cylinder group.
|
|
|
|
*/
|
2003-09-03 23:29:12 +04:00
|
|
|
memcpy(iobuf, &sblock, sizeof sblock);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (needswap)
|
2003-09-03 23:29:12 +04:00
|
|
|
ffs_sb_swap(&sblock, (struct fs *)iobuf);
|
2004-04-15 02:06:33 +04:00
|
|
|
if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
|
|
|
|
memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
|
|
|
|
0xff, 256);
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2006-01-15 22:49:25 +03:00
|
|
|
if (verbosity >= 3)
|
2006-01-09 23:59:25 +03:00
|
|
|
printf("super-block backups (for fsck_ffs -b #) at:\n");
|
2006-01-15 22:49:25 +03:00
|
|
|
/* If we are printing more than one line of numbers, line up columns */
|
|
|
|
fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
|
2013-06-23 06:06:04 +04:00
|
|
|
(uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, sblock.fs_ncg-1)));
|
2006-01-15 22:49:25 +03:00
|
|
|
/* Get terminal width */
|
|
|
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0)
|
|
|
|
max_cols = winsize.ws_col;
|
|
|
|
else
|
|
|
|
max_cols = 80;
|
|
|
|
if (Nflag && verbosity == 3)
|
2006-01-17 00:34:41 +03:00
|
|
|
/* Leave space to add " ..." after one row of numbers */
|
2006-01-15 22:49:25 +03:00
|
|
|
max_cols -= 4;
|
2006-01-11 15:17:42 +03:00
|
|
|
#define BASE 0x10000 /* For some fixed-point maths */
|
2006-01-09 23:59:25 +03:00
|
|
|
col = 0;
|
2006-01-15 22:49:25 +03:00
|
|
|
delta = verbosity > 2 ? 0 : max_cols * BASE / sblock.fs_ncg;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
|
2006-01-15 22:49:25 +03:00
|
|
|
fflush(stdout);
|
2003-05-02 07:26:11 +04:00
|
|
|
initcg(cylno, &tv);
|
2006-01-15 22:49:25 +03:00
|
|
|
if (verbosity < 2)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
2006-01-15 22:49:25 +03:00
|
|
|
if (delta > 0) {
|
|
|
|
if (Nflag)
|
|
|
|
/* No point doing dots for -N */
|
|
|
|
break;
|
|
|
|
/* Print dots scaled to end near RH margin */
|
|
|
|
for (col += delta; col > BASE; col -= BASE)
|
|
|
|
printf(".");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Print superblock numbers */
|
2011-08-25 20:17:58 +04:00
|
|
|
len = printf("%s%*" PRIu64 ",", col ? " " : "", fld_width,
|
2013-06-23 06:06:04 +04:00
|
|
|
(uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)));
|
2006-01-15 22:49:25 +03:00
|
|
|
col += len;
|
|
|
|
if (col + len < max_cols)
|
|
|
|
/* Next number fits */
|
|
|
|
continue;
|
|
|
|
/* Next number won't fit, need a newline */
|
|
|
|
if (verbosity <= 3) {
|
|
|
|
/* Print dots for subsequent cylinder groups */
|
|
|
|
delta = sblock.fs_ncg - cylno - 1;
|
|
|
|
if (delta != 0) {
|
|
|
|
if (Nflag) {
|
|
|
|
printf(" ...");
|
|
|
|
break;
|
2006-01-11 15:17:42 +03:00
|
|
|
}
|
2006-01-15 22:49:25 +03:00
|
|
|
delta = max_cols * BASE / delta;
|
2006-01-09 23:59:25 +03:00
|
|
|
}
|
|
|
|
}
|
2006-01-15 22:49:25 +03:00
|
|
|
col = 0;
|
|
|
|
printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2006-01-11 15:17:42 +03:00
|
|
|
#undef BASE
|
2006-01-15 22:49:25 +03:00
|
|
|
if (col > 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\n");
|
2004-03-07 15:26:38 +03:00
|
|
|
if (Nflag)
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Now construct the initial file system,
|
|
|
|
*/
|
2003-05-02 07:26:11 +04:00
|
|
|
if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
|
2002-01-07 15:00:09 +03:00
|
|
|
errx(1, "Error making filesystem");
|
2003-05-02 07:26:11 +04:00
|
|
|
sblock.fs_time = tv.tv_sec;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
|
|
|
|
sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
|
|
|
|
sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
|
|
|
|
sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
|
|
|
|
}
|
2003-09-03 23:29:12 +04:00
|
|
|
/*
|
|
|
|
* Write out the super-block and zeros until the first cg info
|
|
|
|
*/
|
2004-03-18 23:35:55 +03:00
|
|
|
i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
|
|
|
|
memset(iobuf, 0, i);
|
|
|
|
memcpy(iobuf, &sblock, sizeof sblock);
|
1998-03-18 20:10:15 +03:00
|
|
|
if (needswap)
|
2003-09-03 23:29:12 +04:00
|
|
|
ffs_sb_swap(&sblock, (struct fs *)iobuf);
|
2004-04-15 02:06:33 +04:00
|
|
|
if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
|
|
|
|
memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
|
|
|
|
0xff, 256);
|
2004-03-18 23:35:55 +03:00
|
|
|
wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
|
1999-03-11 23:23:58 +03:00
|
|
|
|
2003-08-15 19:24:21 +04:00
|
|
|
/* Write out first and last cylinder summary sectors */
|
|
|
|
if (needswap)
|
|
|
|
ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
|
1999-03-11 23:23:58 +03:00
|
|
|
|
2003-08-15 19:24:21 +04:00
|
|
|
if (fscs_next > fscs_reset) {
|
|
|
|
if (needswap)
|
|
|
|
ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
|
|
|
|
fs_csaddr++;
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
|
2003-08-15 19:24:21 +04:00
|
|
|
}
|
1999-03-11 23:23:58 +03:00
|
|
|
|
2004-03-18 23:35:55 +03:00
|
|
|
/* mfs doesn't need these permanently allocated */
|
|
|
|
munmap(iobuf, iobuf_memsize);
|
|
|
|
munmap(fscs_0, 2 * sblock.fs_fsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a cylinder group.
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2003-05-02 07:26:11 +04:00
|
|
|
initcg(int cylno, const struct timeval *tv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
daddr_t cbase, dmax;
|
2004-03-18 23:35:55 +03:00
|
|
|
int32_t i, d, dlower, dupper, blkno;
|
2009-04-11 11:20:09 +04:00
|
|
|
uint32_t u;
|
2003-04-02 14:39:19 +04:00
|
|
|
struct ufs1_dinode *dp1;
|
|
|
|
struct ufs2_dinode *dp2;
|
|
|
|
int start;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine block bounds for cylinder group.
|
|
|
|
* Allow space for super block summary information in first
|
|
|
|
* cylinder group.
|
|
|
|
*/
|
|
|
|
cbase = cgbase(&sblock, cylno);
|
|
|
|
dmax = cbase + sblock.fs_fpg;
|
|
|
|
if (dmax > sblock.fs_size)
|
|
|
|
dmax = sblock.fs_size;
|
|
|
|
dlower = cgsblock(&sblock, cylno) - cbase;
|
|
|
|
dupper = cgdmin(&sblock, cylno) - cbase;
|
2003-08-15 19:07:16 +04:00
|
|
|
if (cylno == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
|
2003-08-15 19:07:16 +04:00
|
|
|
if (dupper >= cgstart(&sblock, cylno + 1)) {
|
|
|
|
printf("\rToo many cylinder groups to fit summary "
|
|
|
|
"information into first cylinder group\n");
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(40);
|
2003-08-15 19:07:16 +04:00
|
|
|
}
|
|
|
|
}
|
1994-09-23 18:26:58 +04:00
|
|
|
memset(&acg, 0, sblock.fs_cgsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_magic = CG_MAGIC;
|
|
|
|
acg.cg_cgx = cylno;
|
|
|
|
acg.cg_ndblk = dmax - cbase;
|
1994-06-08 23:27:32 +04:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
2002-04-10 12:27:23 +04:00
|
|
|
acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
|
2003-04-02 14:39:19 +04:00
|
|
|
start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
|
|
|
|
if (Oflag == 2) {
|
2003-09-03 21:08:58 +04:00
|
|
|
acg.cg_time = tv->tv_sec;
|
|
|
|
acg.cg_niblk = sblock.fs_ipg;
|
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
|
|
|
acg.cg_initediblk = sblock.fs_ipg < 2 * FFS_INOPB(&sblock) ?
|
|
|
|
sblock.fs_ipg : 2 * FFS_INOPB(&sblock);
|
2003-04-02 14:39:19 +04:00
|
|
|
acg.cg_iusedoff = start;
|
|
|
|
} else {
|
|
|
|
acg.cg_old_ncyl = sblock.fs_old_cpg;
|
2004-04-15 02:06:33 +04:00
|
|
|
if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
|
|
|
|
(cylno == sblock.fs_ncg - 1))
|
|
|
|
acg.cg_old_ncyl =
|
|
|
|
sblock.fs_old_ncyl % sblock.fs_old_cpg;
|
2003-09-03 21:08:58 +04:00
|
|
|
acg.cg_old_time = tv->tv_sec;
|
|
|
|
acg.cg_old_niblk = sblock.fs_ipg;
|
2003-04-02 14:39:19 +04:00
|
|
|
acg.cg_old_btotoff = start;
|
|
|
|
acg.cg_old_boff = acg.cg_old_btotoff +
|
|
|
|
sblock.fs_old_cpg * sizeof(int32_t);
|
|
|
|
acg.cg_iusedoff = acg.cg_old_boff +
|
|
|
|
sblock.fs_old_cpg * sizeof(u_int16_t);
|
|
|
|
}
|
|
|
|
acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (sblock.fs_contigsumsize <= 0) {
|
|
|
|
acg.cg_nextfreeoff = acg.cg_freeoff +
|
2003-04-02 14:39:19 +04:00
|
|
|
howmany(sblock.fs_fpg, CHAR_BIT);
|
1994-06-08 23:27:32 +04:00
|
|
|
} else {
|
2002-04-10 12:27:23 +04:00
|
|
|
acg.cg_clustersumoff = acg.cg_freeoff +
|
2003-04-02 14:39:19 +04:00
|
|
|
howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
|
2002-09-29 00:11:05 +04:00
|
|
|
if (isappleufs) {
|
|
|
|
/* Apple PR2216969 gives rationale for this change.
|
|
|
|
* I believe they were mistaken, but we need to
|
2003-02-14 19:21:47 +03:00
|
|
|
* duplicate it for compatibility. -- dbj@NetBSD.org
|
2002-09-29 00:11:05 +04:00
|
|
|
*/
|
|
|
|
acg.cg_clustersumoff += sizeof(int32_t);
|
|
|
|
}
|
1994-06-08 23:27:32 +04:00
|
|
|
acg.cg_clustersumoff =
|
1994-12-18 08:09:39 +03:00
|
|
|
roundup(acg.cg_clustersumoff, sizeof(int32_t));
|
1994-06-08 23:27:32 +04:00
|
|
|
acg.cg_clusteroff = acg.cg_clustersumoff +
|
1994-12-18 08:09:39 +03:00
|
|
|
(sblock.fs_contigsumsize + 1) * sizeof(int32_t);
|
2002-04-10 12:27:23 +04:00
|
|
|
acg.cg_nextfreeoff = acg.cg_clusteroff +
|
2013-06-24 02:03:34 +04:00
|
|
|
howmany(ffs_fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
|
1994-06-08 23:27:32 +04:00
|
|
|
}
|
2000-12-04 12:59:48 +03:00
|
|
|
if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
|
1994-06-08 23:27:32 +04:00
|
|
|
printf("Panic: cylinder group too big\n");
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(37);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
acg.cg_cs.cs_nifree += sblock.fs_ipg;
|
|
|
|
if (cylno == 0)
|
2013-01-22 13:39:11 +04:00
|
|
|
for (u = 0; u < UFS_ROOTINO; u++) {
|
2009-04-11 11:20:09 +04:00
|
|
|
setbit(cg_inosused(&acg, 0), u);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nifree--;
|
|
|
|
}
|
|
|
|
if (cylno > 0) {
|
|
|
|
/*
|
|
|
|
* In cylno 0, beginning space is reserved
|
|
|
|
* for boot and super blocks.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
for (d = 0, blkno = 0; d < dlower;) {
|
1998-03-18 20:10:15 +03:00
|
|
|
setblock(&sblock, cg_blksfree(&acg, 0), blkno);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_clustersfree(&acg, 0), blkno);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nbfree++;
|
2004-04-15 02:06:33 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
int cn = old_cbtocylno(&sblock, d);
|
|
|
|
old_cg_blktot(&acg, 0)[cn]++;
|
|
|
|
old_cg_blks(&sblock, &acg,
|
|
|
|
cn, 0)[old_cbtorpos(&sblock, d)]++;
|
|
|
|
}
|
2002-04-10 21:28:13 +04:00
|
|
|
d += sblock.fs_frag;
|
|
|
|
blkno++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-04-10 12:27:23 +04:00
|
|
|
if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_frsum[sblock.fs_frag - i]++;
|
|
|
|
for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_blksfree(&acg, 0), dupper);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
}
|
|
|
|
}
|
2002-04-10 21:28:13 +04:00
|
|
|
for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
|
2003-04-02 14:39:19 +04:00
|
|
|
d + sblock.fs_frag <= acg.cg_ndblk; ) {
|
1998-03-18 20:10:15 +03:00
|
|
|
setblock(&sblock, cg_blksfree(&acg, 0), blkno);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_clustersfree(&acg, 0), blkno);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nbfree++;
|
2004-04-15 02:06:33 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
int cn = old_cbtocylno(&sblock, d);
|
|
|
|
old_cg_blktot(&acg, 0)[cn]++;
|
|
|
|
old_cg_blks(&sblock, &acg,
|
|
|
|
cn, 0)[old_cbtorpos(&sblock, d)]++;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
d += sblock.fs_frag;
|
2002-04-10 21:28:13 +04:00
|
|
|
blkno++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
if (d < acg.cg_ndblk) {
|
|
|
|
acg.cg_frsum[acg.cg_ndblk - d]++;
|
|
|
|
for (; d < acg.cg_ndblk; d++) {
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_blksfree(&acg, 0), d);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
}
|
|
|
|
}
|
1994-06-08 23:27:32 +04:00
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
1998-03-18 20:10:15 +03:00
|
|
|
int32_t *sump = cg_clustersum(&acg, 0);
|
|
|
|
u_char *mapp = cg_clustersfree(&acg, 0);
|
1994-06-08 23:27:32 +04:00
|
|
|
int map = *mapp++;
|
|
|
|
int bit = 1;
|
|
|
|
int run = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < acg.cg_nclusterblks; i++) {
|
|
|
|
if ((map & bit) != 0) {
|
|
|
|
run++;
|
|
|
|
} else if (run != 0) {
|
|
|
|
if (run > sblock.fs_contigsumsize)
|
|
|
|
run = sblock.fs_contigsumsize;
|
|
|
|
sump[run]++;
|
|
|
|
run = 0;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
|
1994-06-08 23:27:32 +04:00
|
|
|
bit <<= 1;
|
|
|
|
} else {
|
|
|
|
map = *mapp++;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (run != 0) {
|
|
|
|
if (run > sblock.fs_contigsumsize)
|
|
|
|
run = sblock.fs_contigsumsize;
|
|
|
|
sump[run]++;
|
|
|
|
}
|
|
|
|
}
|
2003-08-15 19:24:21 +04:00
|
|
|
*fscs_next++ = acg.cg_cs;
|
|
|
|
if (fscs_next == fscs_end) {
|
2004-03-18 23:35:55 +03:00
|
|
|
/* write block of cylinder group summary info into cyl 0 */
|
2003-08-15 19:24:21 +04:00
|
|
|
if (needswap)
|
|
|
|
ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
|
|
|
|
fs_csaddr++;
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
|
2003-08-15 19:24:21 +04:00
|
|
|
fscs_next = fscs_reset;
|
|
|
|
memset(fscs_next, 0, sblock.fs_fsize);
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
/*
|
|
|
|
* Write out the duplicate super block, the cylinder group map
|
|
|
|
* and two blocks worth of inodes in a single write.
|
|
|
|
*/
|
|
|
|
start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
|
|
|
|
memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
|
1998-03-18 20:10:15 +03:00
|
|
|
if (needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
|
|
|
|
start += sblock.fs_bsize;
|
|
|
|
dp1 = (struct ufs1_dinode *)(&iobuf[start]);
|
|
|
|
dp2 = (struct ufs2_dinode *)(&iobuf[start]);
|
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
|
|
|
for (i = MIN(sblock.fs_ipg, 2) * FFS_INOPB(&sblock); i != 0; i--) {
|
2003-04-02 14:39:19 +04:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
/* No need to swap, it'll stay random */
|
2003-09-04 19:31:58 +04:00
|
|
|
dp1->di_gen = arc4random() & INT32_MAX;
|
2003-04-02 14:39:19 +04:00
|
|
|
dp1++;
|
|
|
|
} else {
|
2003-09-04 19:31:58 +04:00
|
|
|
dp2->di_gen = arc4random() & INT32_MAX;
|
2003-04-02 14:39:19 +04:00
|
|
|
dp2++;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
|
2003-04-02 14:39:19 +04:00
|
|
|
/*
|
|
|
|
* For the old file system, we have to initialize all the inodes.
|
|
|
|
*/
|
2004-03-18 23:35:55 +03:00
|
|
|
if (sblock.fs_magic != FS_UFS1_MAGIC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
|
|
|
|
d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
|
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
|
|
|
dupper = sblock.fs_ipg / FFS_INOPF(&sblock);
|
2004-03-18 23:35:55 +03:00
|
|
|
for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
|
|
|
|
if (d > dupper - i)
|
|
|
|
d = dupper - i;
|
|
|
|
dp1 = (struct ufs1_dinode *)(&iobuf[start]);
|
|
|
|
do
|
|
|
|
dp1->di_gen = arc4random() & INT32_MAX;
|
|
|
|
while ((char *)++dp1 < &iobuf[iobuf_memsize]);
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, cgimin(&sblock, cylno) + i),
|
2004-03-18 23:35:55 +03:00
|
|
|
d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the file system
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef LOSTDIR
|
2002-01-07 15:00:09 +03:00
|
|
|
#define PREDEFDIR 3
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2002-01-07 15:00:09 +03:00
|
|
|
#define PREDEFDIR 2
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct direct root_dir[] = {
|
2013-01-22 13:39:11 +04:00
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
|
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
|
1994-06-08 23:27:32 +04:00
|
|
|
#ifdef LOSTDIR
|
|
|
|
{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
struct odirect {
|
1994-12-18 08:09:39 +03:00
|
|
|
u_int32_t d_ino;
|
|
|
|
u_int16_t d_reclen;
|
|
|
|
u_int16_t d_namlen;
|
2005-08-23 15:10:23 +04:00
|
|
|
u_char d_name[FFS_MAXNAMLEN + 1];
|
1994-06-08 23:27:32 +04:00
|
|
|
} oroot_dir[] = {
|
2013-01-22 13:39:11 +04:00
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), 1, "." },
|
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), 2, ".." },
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef LOSTDIR
|
|
|
|
{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#ifdef LOSTDIR
|
|
|
|
struct direct lost_found_dir[] = {
|
1994-06-08 23:27:32 +04:00
|
|
|
{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
|
2013-01-22 13:39:11 +04:00
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
|
1994-06-08 23:27:32 +04:00
|
|
|
{ 0, DIRBLKSIZ, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
struct odirect olost_found_dir[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
|
2013-01-22 13:39:11 +04:00
|
|
|
{ UFS_ROOTINO, sizeof(struct direct), 2, ".." },
|
1993-03-21 12:45:37 +03:00
|
|
|
{ 0, DIRBLKSIZ, 0, 0 },
|
|
|
|
};
|
|
|
|
#endif
|
2014-04-26 18:15:08 +04:00
|
|
|
|
2000-12-01 14:52:54 +03:00
|
|
|
static void copy_dir(struct direct *, struct direct *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-01-07 15:00:09 +03:00
|
|
|
int
|
2003-05-02 07:26:11 +04:00
|
|
|
fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-09-03 21:08:58 +04:00
|
|
|
union dinode node;
|
2015-04-28 18:15:53 +03:00
|
|
|
union Buffer buf;
|
1993-03-21 12:45:37 +03:00
|
|
|
int i;
|
2011-03-06 20:08:10 +03:00
|
|
|
int qblocks = 0;
|
|
|
|
int qinos = 0;
|
|
|
|
uint8_t q2h_hash_shift;
|
|
|
|
uint16_t q2h_hash_mask;
|
|
|
|
#ifdef LOSTDIR
|
2002-09-29 00:11:05 +04:00
|
|
|
int dirblksiz = DIRBLKSIZ;
|
|
|
|
if (isappleufs)
|
|
|
|
dirblksiz = APPLEUFS_DIRBLKSIZ;
|
2011-03-06 20:08:10 +03:00
|
|
|
int nextino = LOSTFOUNDINO+1;
|
|
|
|
#else
|
2013-01-22 13:39:11 +04:00
|
|
|
int nextino = UFS_ROOTINO+1;
|
1997-07-01 02:20:30 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the node
|
|
|
|
*/
|
1998-03-18 20:10:15 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef LOSTDIR
|
|
|
|
/*
|
|
|
|
* create the lost+found directory
|
|
|
|
*/
|
2003-09-03 21:08:58 +04:00
|
|
|
memset(&node, 0, sizeof(node));
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag == 0) {
|
2015-04-29 04:49:25 +03:00
|
|
|
(void)makedir(&buf, (struct direct *)olost_found_dir, 2);
|
2002-09-29 00:11:05 +04:00
|
|
|
for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
|
1998-03-18 20:10:15 +03:00
|
|
|
copy_dir((struct direct*)&olost_found_dir[2],
|
|
|
|
(struct direct*)&buf[i]);
|
1994-06-08 23:27:32 +04:00
|
|
|
} else {
|
2015-04-29 04:49:25 +03:00
|
|
|
(void)makedir(&buf, lost_found_dir, 2);
|
2002-09-29 00:11:05 +04:00
|
|
|
for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
|
1998-03-18 20:10:15 +03:00
|
|
|
copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
|
1994-06-08 23:27:32 +04:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
2003-05-02 07:26:11 +04:00
|
|
|
node.dp1.di_atime = tv->tv_sec;
|
|
|
|
node.dp1.di_atimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp1.di_mtime = tv->tv_sec;
|
|
|
|
node.dp1.di_mtimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp1.di_ctime = tv->tv_sec;
|
|
|
|
node.dp1.di_ctimensec = tv->tv_usec * 1000;
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp1.di_mode = IFDIR | UMASK;
|
|
|
|
node.dp1.di_nlink = 2;
|
|
|
|
node.dp1.di_size = sblock.fs_bsize;
|
|
|
|
node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
|
2003-04-20 23:55:33 +04:00
|
|
|
if (node.dp1.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp1.di_size));
|
2011-03-06 20:08:10 +03:00
|
|
|
qblocks += node.dp1.di_blocks;
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp1.di_uid = geteuid();
|
|
|
|
node.dp1.di_gid = getegid();
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
|
2003-04-02 14:39:19 +04:00
|
|
|
buf);
|
|
|
|
} else {
|
2003-05-02 07:26:11 +04:00
|
|
|
node.dp2.di_atime = tv->tv_sec;
|
|
|
|
node.dp2.di_atimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_mtime = tv->tv_sec;
|
|
|
|
node.dp2.di_mtimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_ctime = tv->tv_sec;
|
|
|
|
node.dp2.di_ctimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_birthtime = tv->tv_sec;
|
|
|
|
node.dp2.di_birthnsec = tv->tv_usec * 1000;
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_mode = IFDIR | UMASK;
|
|
|
|
node.dp2.di_nlink = 2;
|
|
|
|
node.dp2.di_size = sblock.fs_bsize;
|
|
|
|
node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
|
2003-04-20 23:55:33 +04:00
|
|
|
if (node.dp2.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_size));
|
2011-03-06 20:08:10 +03:00
|
|
|
qblocks += node.dp2.di_blocks;
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_uid = geteuid();
|
|
|
|
node.dp2.di_gid = getegid();
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
|
2003-04-02 14:39:19 +04:00
|
|
|
buf);
|
|
|
|
}
|
2011-03-06 20:08:10 +03:00
|
|
|
qinos++;
|
1993-03-21 12:45:37 +03:00
|
|
|
iput(&node, LOSTFOUNDINO);
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* create the root directory
|
|
|
|
*/
|
2003-09-03 21:08:58 +04:00
|
|
|
memset(&node, 0, sizeof(node));
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
if (mfs) {
|
|
|
|
node.dp1.di_mode = IFDIR | mfsmode;
|
|
|
|
node.dp1.di_uid = mfsuid;
|
|
|
|
node.dp1.di_gid = mfsgid;
|
|
|
|
} else {
|
|
|
|
node.dp1.di_mode = IFDIR | UMASK;
|
|
|
|
node.dp1.di_uid = geteuid();
|
|
|
|
node.dp1.di_gid = getegid();
|
|
|
|
}
|
|
|
|
node.dp1.di_nlink = PREDEFDIR;
|
|
|
|
if (Oflag == 0)
|
2015-04-29 04:49:25 +03:00
|
|
|
node.dp1.di_size = makedir(&buf,
|
|
|
|
(struct direct *)oroot_dir, PREDEFDIR);
|
2003-04-02 14:39:19 +04:00
|
|
|
else
|
2015-04-29 04:49:25 +03:00
|
|
|
node.dp1.di_size = makedir(&buf, root_dir, PREDEFDIR);
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
|
|
|
|
if (node.dp1.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp1.di_size));
|
2011-03-06 20:08:10 +03:00
|
|
|
qblocks += node.dp1.di_blocks;
|
2014-04-26 18:15:08 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, &buf);
|
2002-01-07 15:00:09 +03:00
|
|
|
} else {
|
2003-04-02 14:39:19 +04:00
|
|
|
if (mfs) {
|
|
|
|
node.dp2.di_mode = IFDIR | mfsmode;
|
|
|
|
node.dp2.di_uid = mfsuid;
|
|
|
|
node.dp2.di_gid = mfsgid;
|
|
|
|
} else {
|
|
|
|
node.dp2.di_mode = IFDIR | UMASK;
|
|
|
|
node.dp2.di_uid = geteuid();
|
|
|
|
node.dp2.di_gid = getegid();
|
|
|
|
}
|
2003-05-02 07:26:11 +04:00
|
|
|
node.dp2.di_atime = tv->tv_sec;
|
|
|
|
node.dp2.di_atimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_mtime = tv->tv_sec;
|
|
|
|
node.dp2.di_mtimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_ctime = tv->tv_sec;
|
|
|
|
node.dp2.di_ctimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_birthtime = tv->tv_sec;
|
|
|
|
node.dp2.di_birthnsec = tv->tv_usec * 1000;
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_nlink = PREDEFDIR;
|
2015-04-29 04:49:25 +03:00
|
|
|
node.dp2.di_size = makedir(&buf, root_dir, PREDEFDIR);
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
|
|
|
|
if (node.dp2.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2003-04-02 14:39:19 +04:00
|
|
|
node.dp2.di_size));
|
2011-03-06 20:08:10 +03:00
|
|
|
qblocks += node.dp2.di_blocks;
|
2014-04-26 18:15:08 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, &buf);
|
2002-01-07 15:00:09 +03:00
|
|
|
}
|
2011-03-06 20:08:10 +03:00
|
|
|
qinos++;
|
2013-01-22 13:39:11 +04:00
|
|
|
iput(&node, UFS_ROOTINO);
|
2011-03-06 20:08:10 +03:00
|
|
|
/*
|
|
|
|
* compute the size of the hash table
|
|
|
|
* We know the smallest block size is 4k, so we can use 2k
|
|
|
|
* for the hash table; as an entry is 8 bytes we can store
|
|
|
|
* 256 entries. So let start q2h_hash_shift at 8
|
|
|
|
*/
|
|
|
|
for (q2h_hash_shift = 8;
|
|
|
|
q2h_hash_shift < 15;
|
|
|
|
q2h_hash_shift++) {
|
|
|
|
if ((sizeof(uint64_t) << (q2h_hash_shift + 1)) +
|
|
|
|
sizeof(struct quota2_header) > (u_int)sblock.fs_bsize)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
q2h_hash_mask = (1 << q2h_hash_shift) - 1;
|
|
|
|
for (i = 0; i < MAXQUOTAS; i++) {
|
|
|
|
struct quota2_header *q2h;
|
|
|
|
struct quota2_entry *q2e;
|
|
|
|
uint64_t offset;
|
|
|
|
uid_t uid = (i == USRQUOTA ? geteuid() : getegid());
|
|
|
|
|
|
|
|
if ((quotas & FS_Q2_DO_TYPE(i)) == 0)
|
|
|
|
continue;
|
2014-04-26 18:15:08 +04:00
|
|
|
quota2_create_blk0(sblock.fs_bsize, &buf, q2h_hash_shift,
|
2011-03-06 20:08:10 +03:00
|
|
|
i, needswap);
|
|
|
|
/* grab an entry from header for root dir */
|
2014-04-26 18:15:08 +04:00
|
|
|
q2h = &buf.q2h;
|
2011-03-06 20:08:10 +03:00
|
|
|
offset = ufs_rw64(q2h->q2h_free, needswap);
|
2014-04-26 18:15:08 +04:00
|
|
|
q2e = (void *)((char *)&buf + offset);
|
2011-03-06 20:08:10 +03:00
|
|
|
q2h->q2h_free = q2e->q2e_next;
|
|
|
|
memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e));
|
|
|
|
q2e->q2e_uid = ufs_rw32(uid, needswap);
|
|
|
|
q2e->q2e_val[QL_BLOCK].q2v_cur = ufs_rw64(qblocks, needswap);
|
|
|
|
q2e->q2e_val[QL_FILE].q2v_cur = ufs_rw64(qinos, needswap);
|
|
|
|
/* add to the hash entry */
|
|
|
|
q2e->q2e_next = q2h->q2h_entries[uid & q2h_hash_mask];
|
|
|
|
q2h->q2h_entries[uid & q2h_hash_mask] =
|
|
|
|
ufs_rw64(offset, needswap);
|
|
|
|
|
|
|
|
memset(&node, 0, sizeof(node));
|
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
node.dp1.di_atime = tv->tv_sec;
|
|
|
|
node.dp1.di_atimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp1.di_mtime = tv->tv_sec;
|
|
|
|
node.dp1.di_mtimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp1.di_ctime = tv->tv_sec;
|
|
|
|
node.dp1.di_ctimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp1.di_mode = IFREG;
|
|
|
|
node.dp1.di_nlink = 1;
|
|
|
|
node.dp1.di_size = sblock.fs_bsize;
|
|
|
|
node.dp1.di_db[0] =
|
|
|
|
alloc(node.dp1.di_size, node.dp1.di_mode);
|
|
|
|
if (node.dp1.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2011-03-06 20:08:10 +03:00
|
|
|
node.dp1.di_size));
|
|
|
|
node.dp1.di_uid = geteuid();
|
|
|
|
node.dp1.di_gid = getegid();
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]),
|
2014-04-26 18:15:08 +04:00
|
|
|
node.dp1.di_size, &buf);
|
2011-03-06 20:08:10 +03:00
|
|
|
} else {
|
|
|
|
node.dp2.di_atime = tv->tv_sec;
|
|
|
|
node.dp2.di_atimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_mtime = tv->tv_sec;
|
|
|
|
node.dp2.di_mtimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_ctime = tv->tv_sec;
|
|
|
|
node.dp2.di_ctimensec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_birthtime = tv->tv_sec;
|
|
|
|
node.dp2.di_birthnsec = tv->tv_usec * 1000;
|
|
|
|
node.dp2.di_mode = IFREG;
|
|
|
|
node.dp2.di_nlink = 1;
|
|
|
|
node.dp2.di_size = sblock.fs_bsize;
|
|
|
|
node.dp2.di_db[0] =
|
|
|
|
alloc(node.dp2.di_size, node.dp2.di_mode);
|
|
|
|
if (node.dp2.di_db[0] == 0)
|
|
|
|
return (0);
|
2013-06-23 11:28:36 +04:00
|
|
|
node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
|
2011-03-06 20:08:10 +03:00
|
|
|
node.dp2.di_size));
|
|
|
|
node.dp2.di_uid = geteuid();
|
|
|
|
node.dp2.di_gid = getegid();
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]),
|
2014-04-26 18:15:08 +04:00
|
|
|
node.dp2.di_size, &buf);
|
2011-03-06 20:08:10 +03:00
|
|
|
}
|
|
|
|
iput(&node, nextino);
|
|
|
|
sblock.fs_quotafile[i] = nextino;
|
|
|
|
nextino++;
|
|
|
|
}
|
2002-01-07 15:00:09 +03:00
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* construct a set of directory entries in "buf".
|
|
|
|
* return size of directory.
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
int
|
2015-04-29 04:49:25 +03:00
|
|
|
makedir(union Buffer *buf, struct direct *protodir, int entries)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int i, spcleft;
|
2013-06-09 22:29:25 +04:00
|
|
|
int dirblksiz = UFS_DIRBLKSIZ;
|
2002-09-29 00:11:05 +04:00
|
|
|
if (isappleufs)
|
|
|
|
dirblksiz = APPLEUFS_DIRBLKSIZ;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2015-04-29 04:49:25 +03:00
|
|
|
memset(buf, 0, dirblksiz);
|
2002-09-29 00:11:05 +04:00
|
|
|
spcleft = dirblksiz;
|
2015-04-29 04:49:25 +03:00
|
|
|
for (cp = buf->data, i = 0; i < entries - 1; i++) {
|
2013-06-09 22:29:25 +04:00
|
|
|
protodir[i].d_reclen = UFS_DIRSIZ(Oflag == 0, &protodir[i], 0);
|
1998-03-18 20:10:15 +03:00
|
|
|
copy_dir(&protodir[i], (struct direct*)cp);
|
1993-03-21 12:45:37 +03:00
|
|
|
cp += protodir[i].d_reclen;
|
|
|
|
spcleft -= protodir[i].d_reclen;
|
|
|
|
}
|
|
|
|
protodir[i].d_reclen = spcleft;
|
1998-03-18 20:10:15 +03:00
|
|
|
copy_dir(&protodir[i], (struct direct*)cp);
|
2002-09-29 00:11:05 +04:00
|
|
|
return (dirblksiz);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a block or frag
|
|
|
|
*/
|
|
|
|
daddr_t
|
2000-12-01 14:52:54 +03:00
|
|
|
alloc(int size, int mode)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int i, frag;
|
1994-06-08 23:27:32 +04:00
|
|
|
daddr_t d, blkno;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2013-06-23 06:06:04 +04:00
|
|
|
rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
|
1998-03-18 20:10:15 +03:00
|
|
|
/* fs -> host byte order */
|
|
|
|
if (needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_cg_swap(&acg, &acg, &sblock);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (acg.cg_magic != CG_MAGIC) {
|
|
|
|
printf("cg 0: bad magic number\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (acg.cg_cs.cs_nbfree == 0) {
|
|
|
|
printf("first cylinder group ran out of space\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
|
2002-04-10 12:27:23 +04:00
|
|
|
if (isblock(&sblock, cg_blksfree(&acg, 0),
|
|
|
|
d >> sblock.fs_fragshift))
|
1993-03-21 12:45:37 +03:00
|
|
|
goto goth;
|
|
|
|
printf("internal error: can't find block in cyl 0\n");
|
|
|
|
return (0);
|
|
|
|
goth:
|
2013-06-24 02:03:34 +04:00
|
|
|
blkno = ffs_fragstoblks(&sblock, d);
|
1998-03-18 20:10:15 +03:00
|
|
|
clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
|
1994-07-21 00:06:52 +04:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
1998-03-18 20:10:15 +03:00
|
|
|
clrbit(cg_clustersfree(&acg, 0), blkno);
|
1993-03-21 12:45:37 +03:00
|
|
|
acg.cg_cs.cs_nbfree--;
|
|
|
|
sblock.fs_cstotal.cs_nbfree--;
|
2003-08-15 19:24:21 +04:00
|
|
|
fscs_0->cs_nbfree--;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (mode & IFDIR) {
|
|
|
|
acg.cg_cs.cs_ndir++;
|
|
|
|
sblock.fs_cstotal.cs_ndir++;
|
2003-08-15 19:24:21 +04:00
|
|
|
fscs_0->cs_ndir++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-04-15 02:06:33 +04:00
|
|
|
if (Oflag <= 1) {
|
|
|
|
int cn = old_cbtocylno(&sblock, d);
|
|
|
|
old_cg_blktot(&acg, 0)[cn]--;
|
|
|
|
old_cg_blks(&sblock, &acg,
|
|
|
|
cn, 0)[old_cbtorpos(&sblock, d)]--;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (size != sblock.fs_bsize) {
|
|
|
|
frag = howmany(size, sblock.fs_fsize);
|
2003-08-15 19:24:21 +04:00
|
|
|
fscs_0->cs_nffree += sblock.fs_frag - frag;
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
|
|
|
|
acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
|
|
|
|
acg.cg_frsum[sblock.fs_frag - frag]++;
|
|
|
|
for (i = frag; i < sblock.fs_frag; i++)
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_blksfree(&acg, 0), d + i);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-03-18 20:10:15 +03:00
|
|
|
/* host -> fs byte order */
|
|
|
|
if (needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_cg_swap(&acg, &acg, &sblock);
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an inode on the disk
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
static void
|
2003-04-02 14:39:19 +04:00
|
|
|
iput(union dinode *ip, ino_t ino)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
daddr_t d;
|
2012-02-13 16:59:56 +04:00
|
|
|
int i;
|
2003-04-02 14:39:19 +04:00
|
|
|
struct ufs1_dinode *dp1;
|
|
|
|
struct ufs2_dinode *dp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2013-06-23 06:06:04 +04:00
|
|
|
rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
|
1998-03-18 20:10:15 +03:00
|
|
|
/* fs -> host byte order */
|
|
|
|
if (needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_cg_swap(&acg, &acg, &sblock);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (acg.cg_magic != CG_MAGIC) {
|
|
|
|
printf("cg 0: bad magic number\n");
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(31);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
acg.cg_cs.cs_nifree--;
|
1998-03-18 20:10:15 +03:00
|
|
|
setbit(cg_inosused(&acg, 0), ino);
|
|
|
|
/* host -> fs byte order */
|
|
|
|
if (needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_cg_swap(&acg, &acg, &sblock);
|
2013-06-23 06:06:04 +04:00
|
|
|
wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
|
1993-03-21 12:45:37 +03:00
|
|
|
sblock.fs_cstotal.cs_nifree--;
|
2003-08-15 19:24:21 +04:00
|
|
|
fscs_0->cs_nifree--;
|
2009-04-11 11:20:09 +04:00
|
|
|
if (ino >= (ino_t)(sblock.fs_ipg * sblock.fs_ncg)) {
|
2005-08-19 06:07:18 +04:00
|
|
|
printf("fsinit: inode value out of range (%llu).\n",
|
|
|
|
(unsigned long long)ino);
|
2010-08-09 21:20:57 +04:00
|
|
|
fserr(32);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2013-06-23 06:06:04 +04:00
|
|
|
d = FFS_FSBTODB(&sblock, ino_to_fsba(&sblock, ino));
|
2003-04-02 14:39:19 +04:00
|
|
|
rdfs(d, sblock.fs_bsize, (char *)iobuf);
|
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
dp1 = (struct ufs1_dinode *)iobuf;
|
2003-09-03 21:08:58 +04:00
|
|
|
dp1 += ino_to_fsbo(&sblock, ino);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (needswap) {
|
2003-09-03 21:08:58 +04:00
|
|
|
ffs_dinode1_swap(&ip->dp1, dp1);
|
2003-04-02 14:39:19 +04:00
|
|
|
/* ffs_dinode1_swap() doesn't swap blocks addrs */
|
2014-04-05 16:32:27 +04:00
|
|
|
for (i=0; i<UFS_NDADDR; i++)
|
2003-09-03 21:08:58 +04:00
|
|
|
dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
|
2014-04-05 16:32:27 +04:00
|
|
|
for (i=0; i<UFS_NIADDR; i++)
|
|
|
|
dp1->di_ib[i] = bswap32(ip->dp1.di_ib[i]);
|
2003-04-02 14:39:19 +04:00
|
|
|
} else
|
2003-09-03 21:08:58 +04:00
|
|
|
*dp1 = ip->dp1;
|
2003-09-06 16:42:00 +04:00
|
|
|
dp1->di_gen = arc4random() & INT32_MAX;
|
2003-04-02 14:39:19 +04:00
|
|
|
} else {
|
|
|
|
dp2 = (struct ufs2_dinode *)iobuf;
|
2003-09-03 21:08:58 +04:00
|
|
|
dp2 += ino_to_fsbo(&sblock, ino);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (needswap) {
|
2003-09-03 21:08:58 +04:00
|
|
|
ffs_dinode2_swap(&ip->dp2, dp2);
|
2014-04-05 16:32:27 +04:00
|
|
|
for (i=0; i<UFS_NDADDR; i++)
|
2005-06-03 05:10:50 +04:00
|
|
|
dp2->di_db[i] = bswap64(ip->dp2.di_db[i]);
|
2014-04-05 16:32:27 +04:00
|
|
|
for (i=0; i<UFS_NIADDR; i++)
|
|
|
|
dp2->di_ib[i] = bswap64(ip->dp2.di_ib[i]);
|
2003-04-02 14:39:19 +04:00
|
|
|
} else
|
2003-09-03 21:08:58 +04:00
|
|
|
*dp2 = ip->dp2;
|
2003-09-06 16:42:00 +04:00
|
|
|
dp2->di_gen = arc4random() & INT32_MAX;
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
|
|
|
wtfs(d, sblock.fs_bsize, iobuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read a block from the file system
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2000-12-01 14:52:54 +03:00
|
|
|
rdfs(daddr_t bno, int size, void *bf)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int n;
|
1995-03-18 10:02:29 +03:00
|
|
|
off_t offset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-01-18 11:59:18 +03:00
|
|
|
#ifdef MFS
|
1993-03-21 12:45:37 +03:00
|
|
|
if (mfs) {
|
2004-03-07 15:26:38 +03:00
|
|
|
if (Nflag)
|
|
|
|
memset(bf, 0, size);
|
|
|
|
else
|
|
|
|
memmove(bf, membase + bno * sectorsize, size);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2002-01-18 11:59:18 +03:00
|
|
|
#endif
|
1995-03-18 10:02:29 +03:00
|
|
|
offset = bno;
|
2003-08-15 19:07:16 +04:00
|
|
|
n = pread(fsi, bf, size, offset * sectorsize);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (n != size) {
|
2003-01-25 00:55:02 +03:00
|
|
|
printf("rdfs: read error for sector %lld: %s\n",
|
|
|
|
(long long)bno, strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(34);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a block to the file system
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2000-12-01 14:52:54 +03:00
|
|
|
wtfs(daddr_t bno, int size, void *bf)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int n;
|
1995-03-18 10:02:29 +03:00
|
|
|
off_t offset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-03-07 15:26:38 +03:00
|
|
|
if (Nflag)
|
|
|
|
return;
|
2002-01-18 11:59:18 +03:00
|
|
|
#ifdef MFS
|
1993-03-21 12:45:37 +03:00
|
|
|
if (mfs) {
|
1997-09-16 18:05:39 +04:00
|
|
|
memmove(membase + bno * sectorsize, bf, size);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2002-01-18 11:59:18 +03:00
|
|
|
#endif
|
1995-03-18 10:02:29 +03:00
|
|
|
offset = bno;
|
2003-08-15 19:07:16 +04:00
|
|
|
n = pwrite(fso, bf, size, offset * sectorsize);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (n != size) {
|
2003-01-25 00:55:02 +03:00
|
|
|
printf("wtfs: write error for sector %lld: %s\n",
|
|
|
|
(long long)bno, strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(36);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if a block is available
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
int
|
2000-12-01 14:52:54 +03:00
|
|
|
isblock(struct fs *fs, unsigned char *cp, int h)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
unsigned char mask;
|
|
|
|
|
2002-04-10 12:27:23 +04:00
|
|
|
switch (fs->fs_fragshift) {
|
|
|
|
case 3:
|
1993-03-21 12:45:37 +03:00
|
|
|
return (cp[h] == 0xff);
|
2002-04-10 12:27:23 +04:00
|
|
|
case 2:
|
1993-03-21 12:45:37 +03:00
|
|
|
mask = 0x0f << ((h & 0x1) << 2);
|
|
|
|
return ((cp[h >> 1] & mask) == mask);
|
2002-04-10 12:27:23 +04:00
|
|
|
case 1:
|
1993-03-21 12:45:37 +03:00
|
|
|
mask = 0x03 << ((h & 0x3) << 1);
|
|
|
|
return ((cp[h >> 2] & mask) == mask);
|
2002-04-10 12:27:23 +04:00
|
|
|
case 0:
|
1993-03-21 12:45:37 +03:00
|
|
|
mask = 0x01 << (h & 0x7);
|
|
|
|
return ((cp[h >> 3] & mask) == mask);
|
|
|
|
default:
|
|
|
|
#ifdef STANDALONE
|
2002-04-10 12:27:23 +04:00
|
|
|
printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2002-04-10 12:27:23 +04:00
|
|
|
fprintf(stderr, "isblock bad fs_fragshift %d\n",
|
|
|
|
fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take a block out of the map
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2000-12-01 14:52:54 +03:00
|
|
|
clrblock(struct fs *fs, unsigned char *cp, int h)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-04-10 12:27:23 +04:00
|
|
|
switch ((fs)->fs_fragshift) {
|
|
|
|
case 3:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h] = 0;
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 2:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 1:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 0:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 3] &= ~(0x01 << (h & 0x7));
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
#ifdef STANDALONE
|
2002-04-10 12:27:23 +04:00
|
|
|
printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2002-04-10 12:27:23 +04:00
|
|
|
fprintf(stderr, "clrblock bad fs_fragshift %d\n",
|
|
|
|
fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* put a block into the map
|
|
|
|
*/
|
1997-07-01 02:20:30 +04:00
|
|
|
void
|
2000-12-01 14:52:54 +03:00
|
|
|
setblock(struct fs *fs, unsigned char *cp, int h)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-04-10 12:27:23 +04:00
|
|
|
switch (fs->fs_fragshift) {
|
|
|
|
case 3:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h] = 0xff;
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 2:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 1:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
|
|
|
|
return;
|
2002-04-10 12:27:23 +04:00
|
|
|
case 0:
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[h >> 3] |= (0x01 << (h & 0x7));
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
#ifdef STANDALONE
|
2002-04-10 12:27:23 +04:00
|
|
|
printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2002-04-10 12:27:23 +04:00
|
|
|
fprintf(stderr, "setblock bad fs_fragshift %d\n",
|
|
|
|
fs->fs_fragshift);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1998-03-18 20:10:15 +03:00
|
|
|
|
|
|
|
/* copy a direntry to a buffer, in fs byte order */
|
|
|
|
static void
|
2000-12-01 14:52:54 +03:00
|
|
|
copy_dir(struct direct *dir, struct direct *dbuf)
|
1998-03-18 20:10:15 +03:00
|
|
|
{
|
2013-06-09 22:29:25 +04:00
|
|
|
memcpy(dbuf, dir, UFS_DIRSIZ(Oflag == 0, dir, 0));
|
1998-03-18 20:10:15 +03:00
|
|
|
if (needswap) {
|
|
|
|
dbuf->d_ino = bswap32(dir->d_ino);
|
|
|
|
dbuf->d_reclen = bswap16(dir->d_reclen);
|
2003-04-02 14:39:19 +04:00
|
|
|
if (Oflag == 0)
|
1998-03-18 20:10:15 +03:00
|
|
|
((struct odirect*)dbuf)->d_namlen =
|
|
|
|
bswap16(((struct odirect*)dir)->d_namlen);
|
|
|
|
}
|
|
|
|
}
|
Make newfs's spare superblock output nicer. Figure out how wide the
largest block number will be, and make the columns wide enough for it. Then
make enough columns to fit in an 80 character window.
Samples: small end & faked up values:
super-block backups (for fsck -b #) at:
32, 176, 272, 416, 512, 656, 752, 896, 992, 1136, 1232, 1376, 1472,
1616, 1712, 1856, 1952, 2096, 2192, 2336, 2432, 2576, 2672, 2816, 2912, 3056,
3152, 3296, 3392, 3536, 3632, 3776, 3872, 4016, 4112, 4256, 4352, 4496, 4592,
For a 155 GB fs we have:
super-block backups (for fsck -b #) at:
128, 65792, 131456, 197120, 262784, 328448, 394112,
459776, 525440, 591104, 656768, 722432, 788096, 853760,
919424, 985088, 1050752, 1116416, 1182080, 1247744, 1313408,
1379072, 1444736, 1510400, 1576064, 1641728, 1707392, 1773056,
...
317462144, 317527808, 317593472, 317659136, 317724800, 317790464, 317856128,
317921792, 317987456, 318053120, 318118784, 318184448, 318250112, 318315776,
318381440, 318447104, 318512768, 318578432,
Now it actually looks like someone's thought about making large fs's. :-)
1999-05-15 02:36:50 +04:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
static int
|
|
|
|
ilog2(int val)
|
|
|
|
{
|
|
|
|
u_int n;
|
|
|
|
|
|
|
|
for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
|
|
|
|
if (1 << n == val)
|
|
|
|
return (n);
|
|
|
|
errx(1, "ilog2: %d is not a power of 2\n", val);
|
|
|
|
}
|
|
|
|
|
2003-09-10 21:25:14 +04:00
|
|
|
static void
|
|
|
|
zap_old_sblock(int sblkoff)
|
|
|
|
{
|
|
|
|
static int cg0_data;
|
|
|
|
uint32_t oldfs[SBLOCKSIZE / 4];
|
|
|
|
static const struct fsm {
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t mask;
|
|
|
|
} fs_magics[] = {
|
|
|
|
{offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
|
|
|
|
{offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
|
|
|
|
{0, 0x70162, ~0u}, /* LFS_MAGIC */
|
|
|
|
{14, 0xef53, 0xffff}, /* EXT2FS (little) */
|
|
|
|
{14, 0xef530000, 0xffff0000}, /* EXT2FS (big) */
|
2006-10-16 07:04:45 +04:00
|
|
|
{.offset = ~0u},
|
2003-09-10 21:25:14 +04:00
|
|
|
};
|
|
|
|
const struct fsm *fsm;
|
2003-09-11 16:19:44 +04:00
|
|
|
|
|
|
|
if (Nflag)
|
|
|
|
return;
|
2003-09-10 21:25:14 +04:00
|
|
|
|
2003-10-29 11:14:13 +03:00
|
|
|
if (sblkoff == 0) /* Why did UFS2 add support for this? sigh. */
|
|
|
|
return;
|
|
|
|
|
2003-09-10 21:25:14 +04:00
|
|
|
if (cg0_data == 0)
|
|
|
|
/* For FFSv1 this could include all the inodes. */
|
|
|
|
cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
|
|
|
|
|
|
|
|
/* Ignore anything that is beyond our filesystem */
|
|
|
|
if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
|
|
|
|
return;
|
|
|
|
/* Zero anything inside our filesystem... */
|
|
|
|
if (sblkoff >= sblock.fs_sblockloc) {
|
|
|
|
/* ...unless we will write that area anyway */
|
|
|
|
if (sblkoff >= cg0_data)
|
2003-09-18 01:09:18 +04:00
|
|
|
wtfs(sblkoff / sectorsize,
|
|
|
|
roundup(sizeof sblock, sectorsize), iobuf);
|
2003-09-10 21:25:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The sector might contain boot code, so we must validate it */
|
|
|
|
rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
|
|
|
|
for (fsm = fs_magics; ; fsm++) {
|
|
|
|
uint32_t v;
|
|
|
|
if (fsm->mask == 0)
|
|
|
|
return;
|
|
|
|
v = oldfs[fsm->offset];
|
|
|
|
if ((v & fsm->mask) == fsm->magic ||
|
|
|
|
(bswap32(v) & fsm->mask) == fsm->magic)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just zap the magic number */
|
|
|
|
oldfs[fsm->offset] = 0;
|
|
|
|
wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
|
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2002-01-18 11:59:18 +03:00
|
|
|
#ifdef MFS
|
2002-01-07 15:00:09 +03:00
|
|
|
/*
|
|
|
|
* Internal version of malloc that trims the requested size if not enough
|
|
|
|
* memory is available.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
mkfs_malloc(size_t size)
|
|
|
|
{
|
|
|
|
u_long pgsz;
|
2013-03-17 16:25:36 +04:00
|
|
|
caddr_t *memory, *extra;
|
|
|
|
size_t exsize = 128 * 1024;
|
2002-01-07 15:00:09 +03:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
pgsz = getpagesize() - 1;
|
|
|
|
size = (size + pgsz) &~ pgsz;
|
2013-03-17 16:25:36 +04:00
|
|
|
|
|
|
|
/* try to map requested size */
|
2007-12-09 00:40:23 +03:00
|
|
|
memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
|
|
|
|
-1, 0);
|
2013-03-17 16:25:36 +04:00
|
|
|
if (memory == MAP_FAILED)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* try to map something extra */
|
|
|
|
extra = mmap(0, exsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
|
|
|
|
-1, 0);
|
|
|
|
munmap(extra, exsize);
|
|
|
|
|
|
|
|
/* if extra memory couldn't be mapped, reduce original request accordingly */
|
|
|
|
if (extra == MAP_FAILED) {
|
|
|
|
munmap(memory, size);
|
|
|
|
size -= exsize;
|
|
|
|
memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
|
|
|
|
-1, 0);
|
|
|
|
if (memory == MAP_FAILED)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory;
|
2002-01-07 15:00:09 +03:00
|
|
|
}
|
2002-01-18 11:59:18 +03:00
|
|
|
#endif /* MFS */
|