2017-11-18 14:26:50 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
2000-12-09 15:27:35 +00:00
|
|
|
* Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
|
2012-04-30 16:08:02 +00:00
|
|
|
* Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
|
|
|
|
* Copyright (c) 2012 The FreeBSD Foundation
|
2000-12-09 15:27:35 +00:00
|
|
|
* All rights reserved.
|
2003-04-26 15:22:29 +00:00
|
|
|
*
|
2000-12-09 15:27:35 +00:00
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
|
2003-04-26 15:22:29 +00:00
|
|
|
*
|
2012-04-30 16:08:02 +00:00
|
|
|
* Portions of this software were developed by Edward Tomasz Napierala
|
|
|
|
* under sponsorship from the FreeBSD Foundation.
|
|
|
|
*
|
2000-12-09 15:27:35 +00: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 acknowledgment:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors, as well as Christoph
|
|
|
|
* Herrmann and Thomas-Henning von Kamptz.
|
|
|
|
* 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.
|
2003-04-26 15:22:29 +00:00
|
|
|
*
|
2000-12-09 15:27:35 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2000-12-12 20:03:17 +00:00
|
|
|
* $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
|
2000-12-09 15:27:35 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char copyright[] =
|
|
|
|
"@(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz\n\
|
|
|
|
Copyright (c) 1980, 1989, 1993 The Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2005-01-16 14:37:30 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2003-05-12 05:37:16 +00:00
|
|
|
#include <sys/disk.h>
|
2012-04-30 16:08:02 +00:00
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2018-01-26 00:58:32 +00:00
|
|
|
#include <errno.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
#include <fcntl.h>
|
2012-04-30 16:08:02 +00:00
|
|
|
#include <fstab.h>
|
|
|
|
#include <inttypes.h>
|
2002-09-25 04:06:37 +00:00
|
|
|
#include <limits.h>
|
2012-04-30 16:08:02 +00:00
|
|
|
#include <mntopts.h>
|
2012-11-18 19:01:00 +00:00
|
|
|
#include <paths.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
#include <stdlib.h>
|
2004-04-03 17:40:19 +00:00
|
|
|
#include <stdint.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
#include <string.h>
|
2004-04-04 04:17:07 +00:00
|
|
|
#include <time.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
2012-04-30 16:08:02 +00:00
|
|
|
#include <libutil.h>
|
2017-10-10 16:17:03 +00:00
|
|
|
#include <libufs.h>
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#ifdef FS_DEBUG
|
|
|
|
int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
|
|
|
|
#endif /* FS_DEBUG */
|
|
|
|
|
|
|
|
static union {
|
|
|
|
struct fs fs;
|
2012-04-12 16:55:48 +00:00
|
|
|
char pad[SBLOCKSIZE];
|
2000-12-09 15:27:35 +00:00
|
|
|
} fsun1, fsun2;
|
|
|
|
#define sblock fsun1.fs /* the new superblock */
|
|
|
|
#define osblock fsun2.fs /* the old superblock */
|
|
|
|
|
|
|
|
static union {
|
|
|
|
struct cg cg;
|
2012-04-12 16:55:48 +00:00
|
|
|
char pad[MAXBSIZE];
|
2000-12-09 15:27:35 +00:00
|
|
|
} cgun1, cgun2;
|
|
|
|
#define acg cgun1.cg /* a cylinder cgroup (new) */
|
|
|
|
#define aocg cgun2.cg /* an old cylinder group */
|
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
static struct csum *fscs; /* cylinder summary */
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2001-06-07 20:43:00 +00:00
|
|
|
static void growfs(int, int, unsigned int);
|
2002-06-21 06:18:05 +00:00
|
|
|
static void rdfs(ufs2_daddr_t, size_t, void *, int);
|
|
|
|
static void wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int);
|
2000-12-09 15:27:35 +00:00
|
|
|
static int charsperline(void);
|
2000-12-12 20:03:17 +00:00
|
|
|
static void usage(void);
|
2000-12-09 15:27:35 +00:00
|
|
|
static int isblock(struct fs *, unsigned char *, int);
|
|
|
|
static void clrblock(struct fs *, unsigned char *, int);
|
|
|
|
static void setblock(struct fs *, unsigned char *, int);
|
2001-06-07 20:43:00 +00:00
|
|
|
static void initcg(int, time_t, int, unsigned int);
|
|
|
|
static void updjcg(int, time_t, int, int, unsigned int);
|
|
|
|
static void updcsloc(time_t, int, int, unsigned int);
|
2002-06-21 06:18:05 +00:00
|
|
|
static void frag_adjust(ufs2_daddr_t, int);
|
2000-12-09 15:27:35 +00:00
|
|
|
static void updclst(int);
|
2017-10-10 16:17:03 +00:00
|
|
|
static void cgckhash(struct cg *);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2011-06-28 19:59:46 +00:00
|
|
|
* Here we actually start growing the file system. We basically read the
|
2003-04-26 15:22:29 +00:00
|
|
|
* cylinder summary from the first cylinder group as we want to update
|
|
|
|
* this on the fly during our various operations. First we handle the
|
2000-12-09 15:27:35 +00:00
|
|
|
* changes in the former last cylinder group. Afterwards we create all new
|
2003-04-26 15:22:29 +00:00
|
|
|
* cylinder groups. Now we handle the cylinder group containing the
|
|
|
|
* cylinder summary which might result in a relocation of the whole
|
|
|
|
* structure. In the end we write back the updated cylinder summary, the
|
2000-12-09 15:27:35 +00:00
|
|
|
* new superblock, and slightly patched versions of the super block
|
|
|
|
* copies.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-07 20:43:00 +00:00
|
|
|
growfs(int fsi, int fso, unsigned int Nflag)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("growfs")
|
2012-03-05 16:37:51 +00:00
|
|
|
time_t modtime;
|
|
|
|
uint cylno;
|
|
|
|
int i, j, width;
|
|
|
|
char tmpbuf[100];
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2011-01-22 21:27:17 +00:00
|
|
|
time(&modtime);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the cylinder summary into the memory.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
fscs = (struct csum *)calloc((size_t)1, (size_t)sblock.fs_cssize);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (fscs == NULL)
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "calloc failed");
|
2018-01-26 00:58:32 +00:00
|
|
|
memcpy(fscs, osblock.fs_csp, osblock.fs_cssize);
|
|
|
|
free(osblock.fs_csp);
|
|
|
|
osblock.fs_csp = NULL;
|
|
|
|
sblock.fs_csp = fscs;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
#ifdef FS_DEBUG
|
2012-03-05 16:37:51 +00:00
|
|
|
{
|
|
|
|
struct csum *dbg_csp;
|
2015-04-24 12:48:48 +00:00
|
|
|
u_int32_t dbg_csc;
|
2012-03-05 16:37:51 +00:00
|
|
|
char dbg_line[80];
|
|
|
|
|
|
|
|
dbg_csp = fscs;
|
|
|
|
|
|
|
|
for (dbg_csc = 0; dbg_csc < osblock.fs_ncg; dbg_csc++) {
|
|
|
|
snprintf(dbg_line, sizeof(dbg_line),
|
|
|
|
"%d. old csum in old location", dbg_csc);
|
|
|
|
DBG_DUMP_CSUM(&osblock, dbg_line, dbg_csp++);
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
#endif /* FS_DEBUG */
|
|
|
|
DBG_PRINT0("fscs read\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do all needed changes in the former last cylinder group.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
updjcg(osblock.fs_ncg - 1, modtime, fsi, fso, Nflag);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2011-06-28 19:59:46 +00:00
|
|
|
* Dump out summary information about file system.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
#ifdef FS_DEBUG
|
2012-04-15 15:15:16 +00:00
|
|
|
#define B2MBFACTOR (1 / (1024.0 * 1024.0))
|
2004-04-03 22:56:54 +00:00
|
|
|
printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n",
|
2000-12-09 15:27:35 +00:00
|
|
|
(float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
|
2004-04-03 22:56:54 +00:00
|
|
|
(intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize,
|
|
|
|
sblock.fs_fsize);
|
2002-06-21 06:18:05 +00:00
|
|
|
printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n",
|
|
|
|
sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
|
|
|
|
sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
|
|
|
|
if (sblock.fs_flags & FS_DOSOFTDEP)
|
|
|
|
printf("\twith soft updates\n");
|
2012-04-15 15:15:16 +00:00
|
|
|
#undef B2MBFACTOR
|
2012-04-30 16:08:02 +00:00
|
|
|
#endif /* FS_DEBUG */
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now build the cylinders group blocks and
|
|
|
|
* then print out indices of cylinder groups.
|
|
|
|
*/
|
2014-02-09 14:28:47 +00:00
|
|
|
printf("super-block backups (for fsck_ffs -b #) at:\n");
|
2000-12-09 15:27:35 +00:00
|
|
|
i = 0;
|
|
|
|
width = charsperline();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate for only the new cylinder groups.
|
|
|
|
*/
|
|
|
|
for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
|
2011-01-22 21:27:17 +00:00
|
|
|
initcg(cylno, modtime, fso, Nflag);
|
2007-12-17 08:03:18 +00:00
|
|
|
j = sprintf(tmpbuf, " %jd%s",
|
|
|
|
(intmax_t)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
|
2012-03-05 16:37:51 +00:00
|
|
|
cylno < (sblock.fs_ncg - 1) ? "," : "" );
|
2000-12-09 15:27:35 +00:00
|
|
|
if (i + j >= width) {
|
|
|
|
printf("\n");
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
i += j;
|
|
|
|
printf("%s", tmpbuf);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do all needed changes in the first cylinder group.
|
|
|
|
* allocate blocks in new location
|
|
|
|
*/
|
2011-01-22 21:27:17 +00:00
|
|
|
updcsloc(modtime, fsi, fso, Nflag);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2018-01-26 00:58:32 +00:00
|
|
|
* Clean up the dynamic fields in our superblock.
|
|
|
|
*
|
2000-12-09 15:27:35 +00:00
|
|
|
* XXX
|
2003-04-26 15:22:29 +00:00
|
|
|
* The following fields are currently distributed from the superblock
|
2000-12-09 15:27:35 +00:00
|
|
|
* to the copies:
|
|
|
|
* fs_minfree
|
|
|
|
* fs_rotdelay
|
|
|
|
* fs_maxcontig
|
|
|
|
* fs_maxbpg
|
|
|
|
* fs_minfree,
|
|
|
|
* fs_optim
|
2018-01-26 00:58:32 +00:00
|
|
|
* fs_flags
|
2000-12-09 15:27:35 +00:00
|
|
|
*
|
|
|
|
* We probably should rather change the summary for the cylinder group
|
|
|
|
* statistics here to the value of what would be in there, if the file
|
2003-04-26 15:22:29 +00:00
|
|
|
* system were created initially with the new size. Therefor we still
|
2000-12-09 15:27:35 +00:00
|
|
|
* need to find an easy way of calculating that.
|
|
|
|
* Possibly we can try to read the first superblock copy and apply the
|
2003-04-26 15:22:29 +00:00
|
|
|
* "diffed" stats between the old and new superblock by still copying
|
2000-12-09 15:27:35 +00:00
|
|
|
* certain parameters onto that.
|
|
|
|
*/
|
2018-01-26 00:58:32 +00:00
|
|
|
sblock.fs_time = modtime;
|
|
|
|
sblock.fs_fmod = 0;
|
|
|
|
sblock.fs_clean = 1;
|
|
|
|
sblock.fs_ronly = 0;
|
|
|
|
sblock.fs_cgrotor = 0;
|
|
|
|
sblock.fs_state = 0;
|
|
|
|
memset((void *)&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2018-01-26 00:58:32 +00:00
|
|
|
* Now write the new superblock, its summary information,
|
|
|
|
* and all the alternates back to disk.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2018-01-26 00:58:32 +00:00
|
|
|
if (!Nflag && sbput(fso, &sblock, sblock.fs_ncg) != 0)
|
|
|
|
errc(2, EIO, "could not write updated superblock");
|
|
|
|
DBG_PRINT0("fscs written\n");
|
|
|
|
|
|
|
|
#ifdef FS_DEBUG
|
|
|
|
{
|
|
|
|
struct csum *dbg_csp;
|
|
|
|
u_int32_t dbg_csc;
|
|
|
|
char dbg_line[80];
|
|
|
|
|
|
|
|
dbg_csp = fscs;
|
|
|
|
for (dbg_csc = 0; dbg_csc < sblock.fs_ncg; dbg_csc++) {
|
|
|
|
snprintf(dbg_line, sizeof(dbg_line),
|
|
|
|
"%d. new csum in new location", dbg_csc);
|
|
|
|
DBG_DUMP_CSUM(&sblock, dbg_line, dbg_csp++);
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2018-01-26 00:58:32 +00:00
|
|
|
#endif /* FS_DEBUG */
|
|
|
|
|
|
|
|
DBG_PRINT0("sblock written\n");
|
|
|
|
DBG_DUMP_FS(&sblock, "new initial sblock");
|
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_PRINT0("sblock copies written\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_FS(&sblock, "new other sblocks");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* This creates a new cylinder group structure, for more details please see
|
|
|
|
* the source of newfs(8), as this function is taken over almost unchanged.
|
|
|
|
* As this is never called for the first cylinder group, the special
|
2000-12-09 15:27:35 +00:00
|
|
|
* provisions for that case are removed here.
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-22 21:27:17 +00:00
|
|
|
initcg(int cylno, time_t modtime, int fso, unsigned int Nflag)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("initcg")
|
2010-09-19 08:18:56 +00:00
|
|
|
static caddr_t iobuf;
|
2018-11-13 21:40:56 +00:00
|
|
|
static long iobufsize;
|
2010-02-11 06:42:08 +00:00
|
|
|
long blkno, start;
|
2012-09-27 23:31:06 +00:00
|
|
|
ino_t ino;
|
2004-04-03 17:40:19 +00:00
|
|
|
ufs2_daddr_t i, cbase, dmax;
|
2002-06-21 06:18:05 +00:00
|
|
|
struct ufs1_dinode *dp1;
|
2018-11-13 21:40:56 +00:00
|
|
|
struct ufs2_dinode *dp2;
|
2002-03-20 17:55:10 +00:00
|
|
|
struct csum *cs;
|
2012-04-15 15:13:29 +00:00
|
|
|
uint j, d, dupper, dlower;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2018-11-13 21:40:56 +00:00
|
|
|
if (iobuf == NULL) {
|
|
|
|
iobufsize = 2 * sblock.fs_bsize;
|
|
|
|
if ((iobuf = malloc(iobufsize)) == NULL)
|
|
|
|
errx(37, "panic: cannot allocate I/O buffer");
|
|
|
|
memset(iobuf, '\0', iobufsize);
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* Determine block bounds for cylinder group.
|
2002-06-21 06:18:05 +00:00
|
|
|
* Allow space for super block summary information in first
|
|
|
|
* cylinder group.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
cbase = cgbase(&sblock, cylno);
|
|
|
|
dmax = cbase + sblock.fs_fpg;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (dmax > sblock.fs_size)
|
2000-12-09 15:27:35 +00:00
|
|
|
dmax = sblock.fs_size;
|
|
|
|
dlower = cgsblock(&sblock, cylno) - cbase;
|
|
|
|
dupper = cgdmin(&sblock, cylno) - cbase;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (cylno == 0) /* XXX fscs may be relocated */
|
2000-12-09 15:27:35 +00:00
|
|
|
dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
|
2002-06-21 06:18:05 +00:00
|
|
|
cs = &fscs[cylno];
|
|
|
|
memset(&acg, 0, sblock.fs_cgsize);
|
2011-01-22 21:27:17 +00:00
|
|
|
acg.cg_time = modtime;
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_magic = CG_MAGIC;
|
|
|
|
acg.cg_cgx = cylno;
|
|
|
|
acg.cg_niblk = sblock.fs_ipg;
|
2016-05-02 00:44:19 +00:00
|
|
|
acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_ndblk = dmax - cbase;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
|
2002-06-21 06:18:05 +00:00
|
|
|
start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
|
|
|
|
if (sblock.fs_magic == FS_UFS2_MAGIC) {
|
|
|
|
acg.cg_iusedoff = start;
|
|
|
|
} else {
|
|
|
|
acg.cg_old_ncyl = sblock.fs_old_cpg;
|
|
|
|
acg.cg_old_time = acg.cg_time;
|
|
|
|
acg.cg_time = 0;
|
|
|
|
acg.cg_old_niblk = acg.cg_niblk;
|
|
|
|
acg.cg_niblk = 0;
|
2010-09-19 08:18:56 +00:00
|
|
|
acg.cg_initediblk = 0;
|
2002-06-21 06:18:05 +00: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);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2002-09-25 04:06:37 +00:00
|
|
|
acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
|
|
|
|
acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_clustersumoff =
|
2002-06-21 06:18:05 +00:00
|
|
|
roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
|
|
|
|
acg.cg_clustersumoff -= sizeof(u_int32_t);
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_clusteroff = acg.cg_clustersumoff +
|
|
|
|
(sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
|
2002-06-21 06:18:05 +00:00
|
|
|
acg.cg_nextfreeoff = acg.cg_clusteroff +
|
2002-09-25 04:06:37 +00:00
|
|
|
howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2010-02-11 06:42:08 +00:00
|
|
|
if (acg.cg_nextfreeoff > (unsigned)sblock.fs_cgsize) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2002-06-21 06:18:05 +00:00
|
|
|
* This should never happen as we would have had that panic
|
2011-06-28 19:59:46 +00:00
|
|
|
* already on file system creation
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(37, "panic: cylinder group too big");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
acg.cg_cs.cs_nifree += sblock.fs_ipg;
|
|
|
|
if (cylno == 0)
|
2017-02-15 19:50:26 +00:00
|
|
|
for (ino = 0; ino < UFS_ROOTINO; ino++) {
|
2012-09-27 23:31:06 +00:00
|
|
|
setbit(cg_inosused(&acg), ino);
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_cs.cs_nifree--;
|
|
|
|
}
|
2018-11-13 21:40:56 +00:00
|
|
|
/*
|
|
|
|
* Initialize the initial inode blocks.
|
|
|
|
*/
|
|
|
|
dp1 = (struct ufs1_dinode *)(void *)iobuf;
|
|
|
|
dp2 = (struct ufs2_dinode *)(void *)iobuf;
|
|
|
|
for (i = 0; i < acg.cg_initediblk; i++) {
|
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
dp1->di_gen = arc4random();
|
|
|
|
dp1++;
|
|
|
|
} else {
|
|
|
|
dp2->di_gen = arc4random();
|
|
|
|
dp2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno)), iobufsize, iobuf,
|
|
|
|
fso, Nflag);
|
2004-10-09 02:53:47 +00:00
|
|
|
/*
|
2011-06-28 19:59:46 +00:00
|
|
|
* For the old file system, we have to initialize all the inodes.
|
2004-10-09 02:53:47 +00:00
|
|
|
*/
|
2018-11-13 21:40:56 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC &&
|
|
|
|
sblock.fs_ipg > 2 * INOPB(&sblock)) {
|
|
|
|
for (i = 2 * sblock.fs_frag;
|
|
|
|
i < sblock.fs_ipg / INOPF(&sblock);
|
|
|
|
i += sblock.fs_frag) {
|
2012-01-17 22:19:35 +00:00
|
|
|
dp1 = (struct ufs1_dinode *)(void *)iobuf;
|
2010-01-02 20:18:10 +00:00
|
|
|
for (j = 0; j < INOPB(&sblock); j++) {
|
2013-04-15 05:39:21 +00:00
|
|
|
dp1->di_gen = arc4random();
|
2010-01-02 20:18:10 +00:00
|
|
|
dp1++;
|
|
|
|
}
|
2004-10-09 02:53:47 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
|
|
|
|
sblock.fs_bsize, iobuf, fso, Nflag);
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
if (cylno > 0) {
|
|
|
|
/*
|
|
|
|
* In cylno 0, beginning space is reserved
|
|
|
|
* for boot and super blocks.
|
|
|
|
*/
|
|
|
|
for (d = 0; d < dlower; d += sblock.fs_frag) {
|
|
|
|
blkno = d / sblock.fs_frag;
|
|
|
|
setblock(&sblock, cg_blksfree(&acg), blkno);
|
|
|
|
if (sblock.fs_contigsumsize > 0)
|
|
|
|
setbit(cg_clustersfree(&acg), blkno);
|
|
|
|
acg.cg_cs.cs_nbfree++;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
sblock.fs_dsize += dlower;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
sblock.fs_dsize += acg.cg_ndblk - dupper;
|
2022-06-28 04:46:15 +00:00
|
|
|
sblock.fs_old_dsize = sblock.fs_dsize;
|
2000-12-09 15:27:35 +00:00
|
|
|
if ((i = dupper % sblock.fs_frag)) {
|
|
|
|
acg.cg_frsum[sblock.fs_frag - i]++;
|
|
|
|
for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
|
|
|
|
setbit(cg_blksfree(&acg), dupper);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
}
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
|
2012-03-05 16:37:51 +00:00
|
|
|
d += sblock.fs_frag) {
|
2000-12-09 15:27:35 +00:00
|
|
|
blkno = d / sblock.fs_frag;
|
|
|
|
setblock(&sblock, cg_blksfree(&acg), blkno);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
2000-12-09 15:27:35 +00:00
|
|
|
setbit(cg_clustersfree(&acg), blkno);
|
|
|
|
acg.cg_cs.cs_nbfree++;
|
2002-06-21 06:18:05 +00:00
|
|
|
}
|
|
|
|
if (d < acg.cg_ndblk) {
|
|
|
|
acg.cg_frsum[acg.cg_ndblk - d]++;
|
|
|
|
for (; d < acg.cg_ndblk; d++) {
|
2000-12-09 15:27:35 +00:00
|
|
|
setbit(cg_blksfree(&acg), d);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
2002-06-21 06:18:05 +00:00
|
|
|
int32_t *sump = cg_clustersum(&acg);
|
|
|
|
u_char *mapp = cg_clustersfree(&acg);
|
|
|
|
int map = *mapp++;
|
|
|
|
int bit = 1;
|
|
|
|
int run = 0;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < acg.cg_nclusterblks; i++) {
|
2002-06-21 06:18:05 +00:00
|
|
|
if ((map & bit) != 0)
|
2000-12-09 15:27:35 +00:00
|
|
|
run++;
|
2002-06-21 06:18:05 +00:00
|
|
|
else if (run != 0) {
|
|
|
|
if (run > sblock.fs_contigsumsize)
|
2000-12-09 15:27:35 +00:00
|
|
|
run = sblock.fs_contigsumsize;
|
|
|
|
sump[run]++;
|
|
|
|
run = 0;
|
|
|
|
}
|
2002-09-25 04:06:37 +00:00
|
|
|
if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
|
2000-12-09 15:27:35 +00:00
|
|
|
bit <<= 1;
|
2002-06-21 06:18:05 +00:00
|
|
|
else {
|
2000-12-09 15:27:35 +00:00
|
|
|
map = *mapp++;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (run != 0) {
|
2002-06-21 06:18:05 +00:00
|
|
|
if (run > sblock.fs_contigsumsize)
|
2000-12-09 15:27:35 +00:00
|
|
|
run = sblock.fs_contigsumsize;
|
|
|
|
sump[run]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
|
|
|
|
sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
|
|
|
|
sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
|
|
|
|
sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
|
|
|
|
*cs = acg.cg_cs;
|
2010-09-19 08:18:56 +00:00
|
|
|
|
2017-10-10 16:17:03 +00:00
|
|
|
cgckhash(&acg);
|
2018-11-13 21:40:56 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), sblock.fs_cgsize, &acg,
|
|
|
|
fso, Nflag);
|
2010-09-19 08:18:56 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "new cg", &acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Here we add or subtract (sign +1/-1) the available fragments in a given
|
2000-12-09 15:27:35 +00:00
|
|
|
* block to or from the fragment statistics. By subtracting before and adding
|
2003-04-26 15:22:29 +00:00
|
|
|
* after an operation on the free frag map we can easy update the fragment
|
2002-12-30 21:18:15 +00:00
|
|
|
* statistic, which seems to be otherwise a rather complex operation.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
static void
|
2002-06-21 06:18:05 +00:00
|
|
|
frag_adjust(ufs2_daddr_t frag, int sign)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("frag_adjust")
|
|
|
|
int fragsize;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-04-15 15:15:16 +00:00
|
|
|
fragsize = 0;
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* Here frag only needs to point to any fragment in the block we want
|
|
|
|
* to examine.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
for (f = rounddown(frag, sblock.fs_frag);
|
|
|
|
f < roundup(frag + 1, sblock.fs_frag); f++) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Count contiguous free fragments.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
if (isset(cg_blksfree(&acg), f)) {
|
2000-12-09 15:27:35 +00:00
|
|
|
fragsize++;
|
|
|
|
} else {
|
2012-03-05 16:37:51 +00:00
|
|
|
if (fragsize && fragsize < sblock.fs_frag) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* We found something in between.
|
|
|
|
*/
|
2012-04-12 16:55:48 +00:00
|
|
|
acg.cg_frsum[fragsize] += sign;
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_PRINT2("frag_adjust [%d]+=%d\n",
|
2012-03-05 16:37:51 +00:00
|
|
|
fragsize, sign);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
fragsize = 0;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (fragsize && fragsize < sblock.fs_frag) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* We found something.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
acg.cg_frsum[fragsize] += sign;
|
|
|
|
DBG_PRINT2("frag_adjust [%d]+=%d\n", fragsize, sign);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_PRINT2("frag_adjust [[%d]]+=%d\n", fragsize, sign);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we do all needed work for the former last cylinder group. It has to be
|
2011-06-28 19:59:46 +00:00
|
|
|
* changed in any case, even if the file system ended exactly on the end of
|
2003-04-26 15:22:29 +00:00
|
|
|
* this group, as there is some slightly inconsistent handling of the number
|
|
|
|
* of cylinders in the cylinder group. We start again by reading the cylinder
|
2000-12-09 15:27:35 +00:00
|
|
|
* group from disk. If the last block was not fully available, we first handle
|
2003-04-26 15:22:29 +00:00
|
|
|
* the missing fragments, then we handle all new full blocks in that file
|
|
|
|
* system and finally we handle the new last fragmented block in the file
|
|
|
|
* system. We again have to handle the fragment statistics rotational layout
|
2000-12-09 15:27:35 +00:00
|
|
|
* tables and cluster summary during all those operations.
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-22 21:27:17 +00:00
|
|
|
updjcg(int cylno, time_t modtime, int fsi, int fso, unsigned int Nflag)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("updjcg")
|
2022-06-28 04:37:09 +00:00
|
|
|
ufs2_daddr_t cbase, dmax;
|
2012-03-05 16:37:51 +00:00
|
|
|
struct csum *cs;
|
|
|
|
int i, k;
|
|
|
|
int j = 0;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the former last (joining) cylinder group from disk, and make
|
|
|
|
* a copy.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
|
|
|
|
(size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_PRINT0("jcg read\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "old joining cg", &aocg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* If the cylinder group had already its new final size almost
|
2000-12-09 15:27:35 +00:00
|
|
|
* nothing is to be done ... except:
|
|
|
|
* For some reason the value of cg_ncyl in the last cylinder group has
|
2003-04-26 15:22:29 +00:00
|
|
|
* to be zero instead of fs_cpg. As this is now no longer the last
|
2000-12-09 15:27:35 +00:00
|
|
|
* cylinder group we have to change that value now to fs_cpg.
|
2003-04-26 15:22:29 +00:00
|
|
|
*/
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (cgbase(&osblock, cylno + 1) == osblock.fs_size) {
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC)
|
2012-04-15 15:15:16 +00:00
|
|
|
acg.cg_old_ncyl = sblock.fs_old_cpg;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2020-09-22 03:57:48 +00:00
|
|
|
cgckhash(&acg);
|
2001-06-07 20:43:00 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
|
|
|
|
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_PRINT0("jcg written\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "new joining cg", &acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up some variables needed later.
|
|
|
|
*/
|
|
|
|
cbase = cgbase(&sblock, cylno);
|
|
|
|
dmax = cbase + sblock.fs_fpg;
|
|
|
|
if (dmax > sblock.fs_size)
|
|
|
|
dmax = sblock.fs_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set pointer to the cylinder summary for our cylinder group.
|
|
|
|
*/
|
|
|
|
cs = fscs + cylno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Touch the cylinder group, update all fields in the cylinder group as
|
|
|
|
* needed, update the free space in the superblock.
|
|
|
|
*/
|
2011-01-22 21:27:17 +00:00
|
|
|
acg.cg_time = modtime;
|
2010-02-11 06:42:08 +00:00
|
|
|
if ((unsigned)cylno == sblock.fs_ncg - 1) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* This is still the last cylinder group.
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC)
|
|
|
|
acg.cg_old_ncyl =
|
|
|
|
sblock.fs_old_ncyl % sblock.fs_old_cpg;
|
2000-12-09 15:27:35 +00:00
|
|
|
} else {
|
2002-06-21 06:18:05 +00:00
|
|
|
acg.cg_old_ncyl = sblock.fs_old_cpg;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_PRINT2("jcg dbg: %d %u", cylno, sblock.fs_ncg);
|
2004-04-03 17:40:19 +00:00
|
|
|
#ifdef FS_DEBUG
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC)
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_PRINT2("%d %u", acg.cg_old_ncyl, sblock.fs_old_cpg);
|
2004-04-03 17:40:19 +00:00
|
|
|
#endif
|
2002-06-21 06:18:05 +00:00
|
|
|
DBG_PRINT0("\n");
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_ndblk = dmax - cbase;
|
2012-03-05 16:37:51 +00:00
|
|
|
sblock.fs_dsize += acg.cg_ndblk - aocg.cg_ndblk;
|
2022-06-28 04:46:15 +00:00
|
|
|
sblock.fs_old_dsize = sblock.fs_dsize;
|
2012-03-05 16:37:51 +00:00
|
|
|
if (sblock.fs_contigsumsize > 0)
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Now we have to update the free fragment bitmap for our new free
|
|
|
|
* space. There again we have to handle the fragmentation and also
|
|
|
|
* the rotational layout tables and the cluster summary. This is
|
|
|
|
* also done per fragment for the first new block if the old file
|
|
|
|
* system end was not on a block boundary, per fragment for the new
|
2011-06-28 19:59:46 +00:00
|
|
|
* last block if the new file system end is not on a block boundary,
|
2000-12-09 15:27:35 +00:00
|
|
|
* and per block for all space in between.
|
|
|
|
*
|
|
|
|
* Handle the first new block here if it was partially available
|
|
|
|
* before.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
if (osblock.fs_size % sblock.fs_frag) {
|
|
|
|
if (roundup(osblock.fs_size, sblock.fs_frag) <=
|
|
|
|
sblock.fs_size) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* The new space is enough to fill at least this
|
|
|
|
* block
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
j = 0;
|
|
|
|
for (i = roundup(osblock.fs_size - cbase,
|
|
|
|
sblock.fs_frag) - 1; i >= osblock.fs_size - cbase;
|
2000-12-09 15:27:35 +00:00
|
|
|
i--) {
|
|
|
|
setbit(cg_blksfree(&acg), i);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Check if the fragment just created could join an
|
2000-12-09 15:27:35 +00:00
|
|
|
* already existing fragment at the former end of the
|
2011-06-28 19:59:46 +00:00
|
|
|
* file system.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
if (isblock(&sblock, cg_blksfree(&acg),
|
|
|
|
((osblock.fs_size - cgbase(&sblock, cylno)) /
|
|
|
|
sblock.fs_frag))) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* The block is now completely available.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
DBG_PRINT0("block was\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
acg.cg_frsum[osblock.fs_size % sblock.fs_frag]--;
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_cs.cs_nbfree++;
|
2012-03-05 16:37:51 +00:00
|
|
|
acg.cg_cs.cs_nffree -= sblock.fs_frag;
|
|
|
|
k = rounddown(osblock.fs_size - cbase,
|
|
|
|
sblock.fs_frag);
|
|
|
|
updclst((osblock.fs_size - cbase) /
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_frag);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Lets rejoin a possible partially growed
|
|
|
|
* fragment.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
k = 0;
|
|
|
|
while (isset(cg_blksfree(&acg), i) &&
|
|
|
|
(i >= rounddown(osblock.fs_size - cbase,
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_frag))) {
|
|
|
|
i--;
|
|
|
|
k++;
|
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (k)
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_frsum[k]--;
|
2012-03-05 16:37:51 +00:00
|
|
|
acg.cg_frsum[k + j]++;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We only grow by some fragments within this last
|
|
|
|
* block.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
for (i = sblock.fs_size - cbase - 1;
|
|
|
|
i >= osblock.fs_size - cbase; i--) {
|
2000-12-09 15:27:35 +00:00
|
|
|
setbit(cg_blksfree(&acg), i);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Lets rejoin a possible partially growed fragment.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
k = 0;
|
|
|
|
while (isset(cg_blksfree(&acg), i) &&
|
|
|
|
(i >= rounddown(osblock.fs_size - cbase,
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_frag))) {
|
|
|
|
i--;
|
|
|
|
k++;
|
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (k)
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_frsum[k]--;
|
2012-03-05 16:37:51 +00:00
|
|
|
acg.cg_frsum[k + j]++;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle all new complete blocks here.
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
for (i = roundup(osblock.fs_size - cbase, sblock.fs_frag);
|
|
|
|
i + sblock.fs_frag <= dmax - cbase; /* XXX <= or only < ? */
|
|
|
|
i += sblock.fs_frag) {
|
2000-12-09 15:27:35 +00:00
|
|
|
j = i / sblock.fs_frag;
|
|
|
|
setblock(&sblock, cg_blksfree(&acg), j);
|
|
|
|
updclst(j);
|
|
|
|
acg.cg_cs.cs_nbfree++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle the last new block if there are stll some new fragments left.
|
2003-04-26 15:22:29 +00:00
|
|
|
* Here we don't have to bother about the cluster summary or the even
|
2000-12-09 15:27:35 +00:00
|
|
|
* the rotational layout table.
|
|
|
|
*/
|
|
|
|
if (i < (dmax - cbase)) {
|
|
|
|
acg.cg_frsum[dmax - cbase - i]++;
|
|
|
|
for (; i < dmax - cbase; i++) {
|
|
|
|
setbit(cg_blksfree(&acg), i);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sblock.fs_cstotal.cs_nffree +=
|
|
|
|
(acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
|
|
|
|
sblock.fs_cstotal.cs_nbfree +=
|
|
|
|
(acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
|
|
|
|
/*
|
|
|
|
* The following statistics are not changed here:
|
|
|
|
* sblock.fs_cstotal.cs_ndir
|
|
|
|
* sblock.fs_cstotal.cs_nifree
|
|
|
|
* As the statistics for this cylinder group are ready, copy it to
|
|
|
|
* the summary information array.
|
|
|
|
*/
|
|
|
|
*cs = acg.cg_cs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the updated "joining" cylinder group back to disk.
|
|
|
|
*/
|
2017-10-10 16:17:03 +00:00
|
|
|
cgckhash(&acg);
|
2001-06-07 20:43:00 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
|
|
|
|
(void *)&acg, fso, Nflag);
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_PRINT0("jcg written\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "new joining cg", &acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Here we update the location of the cylinder summary. We have two possible
|
2012-04-30 16:08:02 +00:00
|
|
|
* ways of growing the cylinder summary:
|
2003-04-26 15:22:29 +00:00
|
|
|
* (1) We can try to grow the summary in the current location, and relocate
|
2000-12-09 15:27:35 +00:00
|
|
|
* possibly used blocks within the current cylinder group.
|
|
|
|
* (2) Alternatively we can relocate the whole cylinder summary to the first
|
2003-04-26 15:22:29 +00:00
|
|
|
* new completely empty cylinder group. Once the cylinder summary is no
|
|
|
|
* longer in the beginning of the first cylinder group you should never
|
|
|
|
* use a version of fsck which is not aware of the possibility to have
|
2000-12-09 15:27:35 +00:00
|
|
|
* this structure in a non standard place.
|
2012-04-12 13:43:43 +00:00
|
|
|
* Option (2) is considered to be less intrusive to the structure of the file-
|
|
|
|
* system, so that's the one being used.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
static void
|
2011-01-22 21:27:17 +00:00
|
|
|
updcsloc(time_t modtime, int fsi, int fso, unsigned int Nflag)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("updcsloc")
|
2012-03-05 16:37:51 +00:00
|
|
|
struct csum *cs;
|
|
|
|
int ocscg, ncscg;
|
2012-04-12 13:43:43 +00:00
|
|
|
ufs2_daddr_t d;
|
2012-03-05 16:37:51 +00:00
|
|
|
int lcs = 0;
|
|
|
|
int block;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (howmany(sblock.fs_cssize, sblock.fs_fsize) ==
|
2000-12-09 15:27:35 +00:00
|
|
|
howmany(osblock.fs_cssize, osblock.fs_fsize)) {
|
|
|
|
/*
|
|
|
|
* No new fragment needed.
|
|
|
|
*/
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
2022-06-28 04:46:15 +00:00
|
|
|
/* Adjust fs_dsize by added summary blocks */
|
|
|
|
sblock.fs_dsize -= howmany(sblock.fs_cssize, sblock.fs_fsize) -
|
|
|
|
howmany(osblock.fs_cssize, osblock.fs_fsize);
|
|
|
|
sblock.fs_old_dsize = sblock.fs_dsize;
|
2012-03-05 16:37:51 +00:00
|
|
|
ocscg = dtog(&osblock, osblock.fs_csaddr);
|
|
|
|
cs = fscs + ocscg;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read original cylinder group from disk, and make a copy.
|
2001-08-08 21:45:42 +00:00
|
|
|
* XXX If Nflag is set in some very rare cases we now miss
|
|
|
|
* some changes done in updjcg by reading the unmodified
|
|
|
|
* block from disk.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
|
|
|
|
(size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_PRINT0("oscg read\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "old summary cg", &aocg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Touch the cylinder group, set up local variables needed later
|
|
|
|
* and update the superblock.
|
|
|
|
*/
|
2011-01-22 21:27:17 +00:00
|
|
|
acg.cg_time = modtime;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX In the case of having active snapshots we may need much more
|
2003-04-26 15:22:29 +00:00
|
|
|
* blocks for the copy on write. We need each block twice, and
|
|
|
|
* also up to 8*3 blocks for indirect blocks for all possible
|
2000-12-09 15:27:35 +00:00
|
|
|
* references.
|
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* There is not enough space in the old cylinder group to
|
|
|
|
* relocate all blocks as needed, so we relocate the whole
|
|
|
|
* cylinder group summary to a new group. We try to use the
|
|
|
|
* first complete new cylinder group just created. Within the
|
|
|
|
* cylinder group we align the area immediately after the
|
|
|
|
* cylinder group information location in order to be as
|
|
|
|
* close as possible to the original implementation of ffs.
|
|
|
|
*
|
|
|
|
* First we have to make sure we'll find enough space in the
|
|
|
|
* new cylinder group. If not, then we currently give up.
|
|
|
|
* We start with freeing everything which was used by the
|
|
|
|
* fragments of the old cylinder summary in the current group.
|
|
|
|
* Now we write back the group meta data, read in the needed
|
|
|
|
* meta data from the new cylinder group, and start allocating
|
|
|
|
* within that group. Here we can assume, the group to be
|
|
|
|
* completely empty. Which makes the handling of fragments and
|
|
|
|
* clusters a lot easier.
|
|
|
|
*/
|
|
|
|
DBG_TRC;
|
|
|
|
if (sblock.fs_ncg - osblock.fs_ncg < 2)
|
|
|
|
errx(2, "panic: not enough space");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* Point "d" to the first fragment not used by the cylinder
|
|
|
|
* summary.
|
|
|
|
*/
|
|
|
|
d = osblock.fs_csaddr + (osblock.fs_cssize / osblock.fs_fsize);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* Set up last cluster size ("lcs") already here. Calculate
|
|
|
|
* the size for the trailing cluster just behind where "d"
|
|
|
|
* points to.
|
|
|
|
*/
|
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
|
|
|
for (block = howmany(d % sblock.fs_fpg, sblock.fs_frag),
|
2012-04-12 16:55:48 +00:00
|
|
|
lcs = 0; lcs < sblock.fs_contigsumsize; block++, lcs++) {
|
2012-04-12 13:43:43 +00:00
|
|
|
if (isclr(cg_clustersfree(&acg), block))
|
|
|
|
break;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-04-12 13:43:43 +00:00
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* Point "d" to the last frag used by the cylinder summary.
|
|
|
|
*/
|
|
|
|
d--;
|
|
|
|
|
|
|
|
DBG_PRINT1("d=%jd\n", (intmax_t)d);
|
|
|
|
if ((d + 1) % sblock.fs_frag) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* The end of the cylinder summary is not a complete
|
|
|
|
* block.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
DBG_TRC;
|
|
|
|
frag_adjust(d % sblock.fs_fpg, -1);
|
|
|
|
for (; (d + 1) % sblock.fs_frag; d--) {
|
|
|
|
DBG_PRINT1("d=%jd\n", (intmax_t)d);
|
|
|
|
setbit(cg_blksfree(&acg), d % sblock.fs_fpg);
|
|
|
|
acg.cg_cs.cs_nffree++;
|
|
|
|
sblock.fs_cstotal.cs_nffree++;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* Point "d" to the last fragment of the last
|
|
|
|
* (incomplete) block of the cylinder summary.
|
|
|
|
*/
|
|
|
|
d++;
|
2012-04-12 16:55:48 +00:00
|
|
|
frag_adjust(d % sblock.fs_fpg, 1);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
if (isblock(&sblock, cg_blksfree(&acg),
|
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag)) {
|
2004-04-03 17:40:19 +00:00
|
|
|
DBG_PRINT1("d=%jd\n", (intmax_t)d);
|
2012-04-12 13:43:43 +00:00
|
|
|
acg.cg_cs.cs_nffree -= sblock.fs_frag;
|
2000-12-09 15:27:35 +00:00
|
|
|
acg.cg_cs.cs_nbfree++;
|
2012-04-12 13:43:43 +00:00
|
|
|
sblock.fs_cstotal.cs_nffree -= sblock.fs_frag;
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_cstotal.cs_nbfree++;
|
2012-03-05 16:37:51 +00:00
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
2000-12-09 15:27:35 +00:00
|
|
|
setbit(cg_clustersfree(&acg),
|
2012-04-12 16:55:48 +00:00
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (lcs < sblock.fs_contigsumsize) {
|
|
|
|
if (lcs)
|
2000-12-09 15:27:35 +00:00
|
|
|
cg_clustersum(&acg)[lcs]--;
|
|
|
|
lcs++;
|
|
|
|
cg_clustersum(&acg)[lcs]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Point "d" to the first fragment of the block before
|
|
|
|
* the last incomplete block.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
d--;
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
DBG_PRINT1("d=%jd\n", (intmax_t)d);
|
|
|
|
for (d = rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
|
|
|
|
d -= sblock.fs_frag) {
|
|
|
|
DBG_TRC;
|
|
|
|
DBG_PRINT1("d=%jd\n", (intmax_t)d);
|
|
|
|
setblock(&sblock, cg_blksfree(&acg),
|
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
|
|
|
acg.cg_cs.cs_nbfree++;
|
|
|
|
sblock.fs_cstotal.cs_nbfree++;
|
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
|
|
|
setbit(cg_clustersfree(&acg),
|
2012-03-05 16:37:51 +00:00
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* The last cluster size is already set up.
|
|
|
|
*/
|
|
|
|
if (lcs < sblock.fs_contigsumsize) {
|
|
|
|
if (lcs)
|
|
|
|
cg_clustersum(&acg)[lcs]--;
|
|
|
|
lcs++;
|
|
|
|
cg_clustersum(&acg)[lcs]++;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-12 13:43:43 +00:00
|
|
|
*cs = acg.cg_cs;
|
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Now write the former cylinder group containing the cylinder
|
|
|
|
* summary back to disk.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2020-09-22 03:57:48 +00:00
|
|
|
cgckhash(&acg);
|
2012-04-12 13:43:43 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
|
|
|
|
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
|
|
|
|
DBG_PRINT0("oscg written\n");
|
|
|
|
DBG_DUMP_CG(&sblock, "old summary cg", &acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Find the beginning of the new cylinder group containing the
|
|
|
|
* cylinder summary.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
sblock.fs_csaddr = cgdmin(&sblock, osblock.fs_ncg);
|
|
|
|
ncscg = dtog(&sblock, sblock.fs_csaddr);
|
|
|
|
cs = fscs + ncscg;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* If Nflag is specified, we would now read random data instead
|
|
|
|
* of an empty cg structure from disk. So we can't simulate that
|
|
|
|
* part for now.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
if (Nflag) {
|
|
|
|
DBG_PRINT0("nscg update skipped\n");
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Read the future cylinder group containing the cylinder
|
|
|
|
* summary from disk, and make a copy.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
|
|
|
|
(size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
|
|
|
|
DBG_PRINT0("nscg read\n");
|
|
|
|
DBG_DUMP_CG(&sblock, "new summary cg", &aocg);
|
|
|
|
|
|
|
|
memcpy((void *)&cgun1, (void *)&cgun2, sizeof(cgun2));
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Allocate all complete blocks used by the new cylinder
|
|
|
|
* summary.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 13:43:43 +00:00
|
|
|
for (d = sblock.fs_csaddr; d + sblock.fs_frag <=
|
|
|
|
sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize);
|
|
|
|
d += sblock.fs_frag) {
|
|
|
|
clrblock(&sblock, cg_blksfree(&acg),
|
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
|
|
|
acg.cg_cs.cs_nbfree--;
|
|
|
|
sblock.fs_cstotal.cs_nbfree--;
|
|
|
|
if (sblock.fs_contigsumsize > 0) {
|
|
|
|
clrbit(cg_clustersfree(&acg),
|
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Allocate all fragments used by the cylinder summary in the
|
|
|
|
* last block.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-12 16:55:48 +00:00
|
|
|
if (d < sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize)) {
|
2012-04-12 13:43:43 +00:00
|
|
|
for (; d - sblock.fs_csaddr <
|
|
|
|
sblock.fs_cssize/sblock.fs_fsize; d++) {
|
|
|
|
clrbit(cg_blksfree(&acg), d % sblock.fs_fpg);
|
|
|
|
acg.cg_cs.cs_nffree--;
|
|
|
|
sblock.fs_cstotal.cs_nffree--;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-04-12 13:43:43 +00:00
|
|
|
acg.cg_cs.cs_nbfree--;
|
|
|
|
acg.cg_cs.cs_nffree += sblock.fs_frag;
|
|
|
|
sblock.fs_cstotal.cs_nbfree--;
|
|
|
|
sblock.fs_cstotal.cs_nffree += sblock.fs_frag;
|
|
|
|
if (sblock.fs_contigsumsize > 0)
|
|
|
|
clrbit(cg_clustersfree(&acg),
|
|
|
|
(d % sblock.fs_fpg) / sblock.fs_frag);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
frag_adjust(d % sblock.fs_fpg, 1);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* XXX Handle the cluster statistics here in the case this
|
|
|
|
* cylinder group is now almost full, and the remaining
|
|
|
|
* space is less then the maximum cluster size. This is
|
|
|
|
* probably not needed, as you would hardly find a file
|
|
|
|
* system which has only MAXCSBUFS+FS_MAXCONTIG of free
|
|
|
|
* space right behind the cylinder group information in
|
|
|
|
* any new cylinder group.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-12 13:43:43 +00:00
|
|
|
/*
|
|
|
|
* Update our statistics in the cylinder summary.
|
|
|
|
*/
|
2000-12-09 15:27:35 +00:00
|
|
|
*cs = acg.cg_cs;
|
|
|
|
|
|
|
|
/*
|
2012-04-12 13:43:43 +00:00
|
|
|
* Write the new cylinder group containing the cylinder summary
|
|
|
|
* back to disk.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2020-09-22 03:57:48 +00:00
|
|
|
cgckhash(&acg);
|
2012-04-12 13:43:43 +00:00
|
|
|
wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
|
|
|
|
(size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
|
|
|
|
DBG_PRINT0("nscg written\n");
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_CG(&sblock, "new summary cg", &acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we read some block(s) from disk.
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-21 06:18:05 +00:00
|
|
|
rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("rdfs")
|
2001-06-07 20:43:00 +00:00
|
|
|
ssize_t n;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (bno < 0)
|
2005-01-16 14:37:30 +00:00
|
|
|
err(32, "rdfs: attempting to read negative block number");
|
2012-03-05 16:37:51 +00:00
|
|
|
if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0)
|
2004-04-03 17:40:19 +00:00
|
|
|
err(33, "rdfs: seek error: %jd", (intmax_t)bno);
|
2001-06-07 20:43:00 +00:00
|
|
|
n = read(fsi, bf, size);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (n != (ssize_t)size)
|
2004-04-03 17:40:19 +00:00
|
|
|
err(34, "rdfs: read error: %jd", (intmax_t)bno);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we write some block(s) to disk.
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-21 06:18:05 +00:00
|
|
|
wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("wtfs")
|
2001-06-07 20:43:00 +00:00
|
|
|
ssize_t n;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
if (Nflag) {
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0)
|
2000-12-12 20:03:17 +00:00
|
|
|
err(35, "wtfs: seek error: %ld", (long)bno);
|
2001-06-07 20:43:00 +00:00
|
|
|
n = write(fso, bf, size);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (n != (ssize_t)size)
|
2000-12-12 20:03:17 +00:00
|
|
|
err(36, "wtfs: write error: %ld", (long)bno);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* Here we check if all frags of a block are free. For more details again
|
2000-12-09 15:27:35 +00:00
|
|
|
* please see the source of newfs(8), as this function is taken over almost
|
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
isblock(struct fs *fs, unsigned char *cp, int h)
|
|
|
|
{
|
|
|
|
DBG_FUNC("isblock")
|
2012-03-05 16:37:51 +00:00
|
|
|
unsigned char mask;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
switch (fs->fs_frag) {
|
|
|
|
case 8:
|
|
|
|
DBG_LEAVE;
|
|
|
|
return (cp[h] == 0xff);
|
|
|
|
case 4:
|
|
|
|
mask = 0x0f << ((h & 0x1) << 2);
|
|
|
|
DBG_LEAVE;
|
|
|
|
return ((cp[h >> 1] & mask) == mask);
|
|
|
|
case 2:
|
|
|
|
mask = 0x03 << ((h & 0x3) << 1);
|
|
|
|
DBG_LEAVE;
|
|
|
|
return ((cp[h >> 2] & mask) == mask);
|
|
|
|
case 1:
|
|
|
|
mask = 0x01 << (h & 0x7);
|
|
|
|
DBG_LEAVE;
|
|
|
|
return ((cp[h >> 3] & mask) == mask);
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
|
|
|
|
DBG_LEAVE;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we allocate a complete block in the block map. For more details again
|
2003-04-26 15:22:29 +00:00
|
|
|
* please see the source of newfs(8), as this function is taken over almost
|
2000-12-09 15:27:35 +00:00
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clrblock(struct fs *fs, unsigned char *cp, int h)
|
|
|
|
{
|
|
|
|
DBG_FUNC("clrblock")
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
switch ((fs)->fs_frag) {
|
|
|
|
case 8:
|
|
|
|
cp[h] = 0;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
cp[h >> 3] &= ~(0x01 << (h & 0x7));
|
|
|
|
break;
|
|
|
|
default:
|
2000-12-12 20:03:17 +00:00
|
|
|
warnx("clrblock bad fs_frag %d", fs->fs_frag);
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we free a complete block in the free block map. For more details again
|
2003-04-26 15:22:29 +00:00
|
|
|
* please see the source of newfs(8), as this function is taken over almost
|
2000-12-09 15:27:35 +00:00
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setblock(struct fs *fs, unsigned char *cp, int h)
|
|
|
|
{
|
|
|
|
DBG_FUNC("setblock")
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
switch (fs->fs_frag) {
|
|
|
|
case 8:
|
|
|
|
cp[h] = 0xff;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
cp[h >> 3] |= (0x01 << (h & 0x7));
|
|
|
|
break;
|
|
|
|
default:
|
2000-12-12 20:03:17 +00:00
|
|
|
warnx("setblock bad fs_frag %d", fs->fs_frag);
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out how many lines our current terminal has. For more details again
|
2003-04-26 15:22:29 +00:00
|
|
|
* please see the source of newfs(8), as this function is taken over almost
|
2000-12-09 15:27:35 +00:00
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
charsperline(void)
|
|
|
|
{
|
|
|
|
DBG_FUNC("charsperline")
|
2012-03-05 16:37:51 +00:00
|
|
|
int columns;
|
|
|
|
char *cp;
|
|
|
|
struct winsize ws;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
columns = 0;
|
2012-03-05 16:37:51 +00:00
|
|
|
if (ioctl(0, TIOCGWINSZ, &ws) != -1)
|
2000-12-09 15:27:35 +00:00
|
|
|
columns = ws.ws_col;
|
2012-03-05 16:37:51 +00:00
|
|
|
if (columns == 0 && (cp = getenv("COLUMNS")))
|
2000-12-09 15:27:35 +00:00
|
|
|
columns = atoi(cp);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (columns == 0)
|
2000-12-09 15:27:35 +00:00
|
|
|
columns = 80; /* last resort */
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
2012-04-18 13:50:17 +00:00
|
|
|
return (columns);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
static int
|
|
|
|
is_dev(const char *name)
|
|
|
|
{
|
|
|
|
struct stat devstat;
|
|
|
|
|
|
|
|
if (stat(name, &devstat) != 0)
|
|
|
|
return (0);
|
|
|
|
if (!S_ISCHR(devstat.st_mode))
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2023-01-15 18:20:48 +00:00
|
|
|
getdev(const char *name, struct statfs *statfsp)
|
2012-04-30 16:08:02 +00:00
|
|
|
{
|
|
|
|
static char device[MAXPATHLEN];
|
2023-01-15 18:20:48 +00:00
|
|
|
const char *cp;
|
2003-05-12 05:37:16 +00:00
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
if (is_dev(name))
|
|
|
|
return (name);
|
|
|
|
|
|
|
|
cp = strrchr(name, '/');
|
2016-04-18 14:08:35 +00:00
|
|
|
if (cp == NULL) {
|
2012-04-30 16:08:02 +00:00
|
|
|
snprintf(device, sizeof(device), "%s%s", _PATH_DEV, name);
|
|
|
|
if (is_dev(device))
|
|
|
|
return (device);
|
|
|
|
}
|
|
|
|
|
2023-01-15 18:20:48 +00:00
|
|
|
if (statfsp != NULL)
|
|
|
|
return (statfsp->f_mntfromname);
|
2012-04-30 16:08:02 +00:00
|
|
|
|
|
|
|
return (NULL);
|
2003-05-12 05:37:16 +00:00
|
|
|
}
|
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2012-03-05 16:37:51 +00:00
|
|
|
* growfs(8) is a utility which allows to increase the size of an existing
|
2011-06-28 19:59:46 +00:00
|
|
|
* ufs file system. Currently this can only be done on unmounted file system.
|
2003-04-26 15:22:29 +00:00
|
|
|
* It recognizes some command line options to specify the new desired size,
|
2011-06-28 19:59:46 +00:00
|
|
|
* and it does some basic checkings. The old file system size is determined
|
2003-04-26 15:22:29 +00:00
|
|
|
* and after some more checks like we can really access the new last block
|
2000-12-09 15:27:35 +00:00
|
|
|
* on the disk etc. we calculate the new parameters for the superblock. After
|
2012-03-29 11:20:19 +00:00
|
|
|
* having done this we just call growfs() which will do the work.
|
2000-12-09 15:27:35 +00:00
|
|
|
* We still have to provide support for snapshots. Therefore we first have to
|
2003-04-26 15:22:29 +00:00
|
|
|
* understand what data structures are always replicated in the snapshot on
|
|
|
|
* creation, for all other blocks we touch during our procedure, we have to
|
2000-12-12 20:03:17 +00:00
|
|
|
* keep the old blocks unchanged somewhere available for the snapshots. If we
|
2003-04-26 15:22:29 +00:00
|
|
|
* are lucky, then we only have to handle our blocks to be relocated in that
|
2000-12-09 15:27:35 +00:00
|
|
|
* way.
|
2003-04-26 15:22:29 +00:00
|
|
|
* Also we have to consider in what order we actually update the critical
|
2011-06-28 19:59:46 +00:00
|
|
|
* data structures of the file system to make sure, that in case of a disaster
|
2000-12-09 15:27:35 +00:00
|
|
|
* fsck(8) is still able to restore any lost data.
|
2003-04-26 15:22:29 +00:00
|
|
|
* The foreseen last step then will be to provide for growing even mounted
|
2011-06-28 19:59:46 +00:00
|
|
|
* file systems. There we have to extend the mount() system call to provide
|
|
|
|
* userland access to the file system locking facility.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
DBG_FUNC("main")
|
2018-01-26 00:58:32 +00:00
|
|
|
struct fs *fs;
|
2012-04-30 16:08:02 +00:00
|
|
|
const char *device;
|
2023-01-15 18:20:48 +00:00
|
|
|
struct statfs *statfsp;
|
2012-04-30 16:08:02 +00:00
|
|
|
uint64_t size = 0;
|
|
|
|
off_t mediasize;
|
2018-01-26 00:58:32 +00:00
|
|
|
int error, j, fsi, fso, ch, ret, Nflag = 0, yflag = 0;
|
2012-04-30 16:08:02 +00:00
|
|
|
char *p, reply[5], oldsizebuf[6], newsizebuf[6];
|
|
|
|
void *testbuf;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
while ((ch = getopt(argc, argv, "Ns:vy")) != -1) {
|
2000-12-09 15:27:35 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'N':
|
2012-03-05 16:37:51 +00:00
|
|
|
Nflag = 1;
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2012-04-30 16:08:02 +00:00
|
|
|
size = (off_t)strtoumax(optarg, &p, 0);
|
|
|
|
if (p == NULL || *p == '\0')
|
|
|
|
size *= DEV_BSIZE;
|
|
|
|
else if (*p == 'b' || *p == 'B')
|
|
|
|
; /* do nothing */
|
|
|
|
else if (*p == 'k' || *p == 'K')
|
|
|
|
size <<= 10;
|
|
|
|
else if (*p == 'm' || *p == 'M')
|
|
|
|
size <<= 20;
|
|
|
|
else if (*p == 'g' || *p == 'G')
|
|
|
|
size <<= 30;
|
|
|
|
else if (*p == 't' || *p == 'T') {
|
|
|
|
size <<= 30;
|
|
|
|
size <<= 10;
|
|
|
|
} else
|
|
|
|
errx(1, "unknown suffix on -s argument");
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
case 'v': /* for compatibility to newfs */
|
|
|
|
break;
|
|
|
|
case 'y':
|
2012-04-30 16:08:02 +00:00
|
|
|
yflag = 1;
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (argc != 1)
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2012-03-05 16:37:51 +00:00
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2012-04-30 16:08:02 +00:00
|
|
|
* Now try to guess the device name.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2023-01-15 18:20:48 +00:00
|
|
|
statfsp = getmntpoint(*argv);
|
|
|
|
device = getdev(*argv, statfsp);
|
2012-04-30 16:08:02 +00:00
|
|
|
if (device == NULL)
|
|
|
|
errx(1, "cannot find special device for %s", *argv);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
fsi = open(device, O_RDONLY);
|
2012-03-05 16:37:51 +00:00
|
|
|
if (fsi < 0)
|
2000-12-12 20:03:17 +00:00
|
|
|
err(1, "%s", device);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-04-30 16:08:02 +00:00
|
|
|
* Try to guess the slice size if not specified.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
if (ioctl(fsi, DIOCGMEDIASIZE, &mediasize) == -1)
|
|
|
|
err(1,"DIOCGMEDIASIZE");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2011-06-28 19:59:46 +00:00
|
|
|
* Check if that partition is suitable for growing a file system.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
if (mediasize < 1)
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "partition is unavailable");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the current superblock, and take a backup.
|
|
|
|
*/
|
2022-07-31 05:44:01 +00:00
|
|
|
if ((ret = sbget(fsi, &fs, UFS_STDSB, 0)) != 0) {
|
2018-01-26 00:58:32 +00:00
|
|
|
switch (ret) {
|
|
|
|
case ENOENT:
|
|
|
|
errx(1, "superblock not recognized");
|
|
|
|
default:
|
|
|
|
errc(1, ret, "unable to read superblock");
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
}
|
2020-10-25 01:36:33 +00:00
|
|
|
/*
|
2021-03-02 22:35:48 +00:00
|
|
|
* Check for filesystem that was unclean at mount time.
|
2020-10-25 01:36:33 +00:00
|
|
|
*/
|
2021-03-02 22:35:48 +00:00
|
|
|
if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0)
|
2020-10-25 01:36:33 +00:00
|
|
|
errx(1, "%s is not clean - run fsck.\n", *argv);
|
2018-01-26 00:58:32 +00:00
|
|
|
memcpy(&osblock, fs, fs->fs_sbsize);
|
|
|
|
free(fs);
|
|
|
|
memcpy((void *)&fsun1, (void *)&fsun2, osblock.fs_sbsize);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
|
2012-03-05 16:37:51 +00:00
|
|
|
DBG_DUMP_FS(&sblock, "old sblock");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-29 11:20:19 +00:00
|
|
|
* Determine size to grow to. Default to the device size.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
if (size == 0)
|
|
|
|
size = mediasize;
|
|
|
|
else {
|
|
|
|
if (size > (uint64_t)mediasize) {
|
|
|
|
humanize_number(oldsizebuf, sizeof(oldsizebuf), size,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
humanize_number(newsizebuf, sizeof(newsizebuf),
|
|
|
|
mediasize,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
|
|
|
|
errx(1, "requested size %s is larger "
|
|
|
|
"than the available %s", oldsizebuf, newsizebuf);
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
2012-12-15 08:53:58 +00:00
|
|
|
/*
|
|
|
|
* Make sure the new size is a multiple of fs_fsize; /dev/ufssuspend
|
|
|
|
* only supports fragment-aligned IO requests.
|
|
|
|
*/
|
|
|
|
size -= size % osblock.fs_fsize;
|
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
if (size <= (uint64_t)(osblock.fs_size * osblock.fs_fsize)) {
|
|
|
|
humanize_number(oldsizebuf, sizeof(oldsizebuf),
|
|
|
|
osblock.fs_size * osblock.fs_fsize,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
humanize_number(newsizebuf, sizeof(newsizebuf), size,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
|
2021-11-15 20:37:34 +00:00
|
|
|
if (size == (uint64_t)(osblock.fs_size * osblock.fs_fsize))
|
|
|
|
errx(0, "requested size %s is equal to the current "
|
|
|
|
"filesystem size %s", newsizebuf, oldsizebuf);
|
|
|
|
errx(1, "requested size %s is smaller than the current "
|
2012-04-30 16:08:02 +00:00
|
|
|
"filesystem size %s", newsizebuf, oldsizebuf);
|
|
|
|
}
|
|
|
|
|
2022-06-28 04:46:15 +00:00
|
|
|
sblock.fs_old_size = sblock.fs_size =
|
|
|
|
dbtofsb(&osblock, size / DEV_BSIZE);
|
2012-10-30 21:32:10 +00:00
|
|
|
sblock.fs_providersize = dbtofsb(&osblock, mediasize / DEV_BSIZE);
|
2012-04-30 16:08:02 +00:00
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
2012-04-30 16:08:02 +00:00
|
|
|
* Are we really growing?
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
if (osblock.fs_size >= sblock.fs_size) {
|
2004-04-03 17:40:19 +00:00
|
|
|
errx(1, "we are not growing (%jd->%jd)",
|
|
|
|
(intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we find an active snapshot.
|
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
if (yflag == 0) {
|
2012-03-05 16:37:51 +00:00
|
|
|
for (j = 0; j < FSMAXSNAP; j++) {
|
|
|
|
if (sblock.fs_snapinum[j]) {
|
2011-06-28 19:59:46 +00:00
|
|
|
errx(1, "active snapshot found in file system; "
|
2011-06-22 17:59:53 +00:00
|
|
|
"please remove all snapshots before "
|
2005-01-16 14:37:30 +00:00
|
|
|
"using growfs");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (!sblock.fs_snapinum[j]) /* list is dense */
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
if (yflag == 0 && Nflag == 0) {
|
|
|
|
if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0)
|
2012-11-18 19:01:00 +00:00
|
|
|
printf("Device is mounted read-write; resizing will "
|
|
|
|
"result in temporary write suspension for %s.\n",
|
|
|
|
statfsp->f_mntonname);
|
2012-04-30 16:08:02 +00:00
|
|
|
printf("It's strongly recommended to make a backup "
|
2011-06-28 19:59:46 +00:00
|
|
|
"before growing the file system.\n"
|
2012-04-30 16:08:02 +00:00
|
|
|
"OK to grow filesystem on %s", device);
|
|
|
|
if (statfsp != NULL)
|
|
|
|
printf(", mounted on %s,", statfsp->f_mntonname);
|
|
|
|
humanize_number(oldsizebuf, sizeof(oldsizebuf),
|
|
|
|
osblock.fs_size * osblock.fs_fsize,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
humanize_number(newsizebuf, sizeof(newsizebuf),
|
|
|
|
sblock.fs_size * sblock.fs_fsize,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
2016-05-11 09:26:23 +00:00
|
|
|
printf(" from %s to %s? [yes/no] ", oldsizebuf, newsizebuf);
|
2012-04-30 16:08:02 +00:00
|
|
|
fflush(stdout);
|
2001-06-07 20:43:00 +00:00
|
|
|
fgets(reply, (int)sizeof(reply), stdin);
|
2016-05-11 09:26:23 +00:00
|
|
|
if (strcasecmp(reply, "yes\n")){
|
|
|
|
printf("Response other than \"yes\"; aborting\n");
|
|
|
|
exit(0);
|
2003-04-26 15:22:29 +00:00
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
/*
|
|
|
|
* Try to access our device for writing. If it's not mounted,
|
|
|
|
* or mounted read-only, simply open it; otherwise, use UFS
|
|
|
|
* suspension mechanism.
|
|
|
|
*/
|
|
|
|
if (Nflag) {
|
|
|
|
fso = -1;
|
|
|
|
} else {
|
2012-11-18 19:01:00 +00:00
|
|
|
if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0) {
|
|
|
|
fso = open(_PATH_UFSSUSPEND, O_RDWR);
|
|
|
|
if (fso == -1)
|
|
|
|
err(1, "unable to open %s", _PATH_UFSSUSPEND);
|
|
|
|
error = ioctl(fso, UFSSUSPEND, &statfsp->f_fsid);
|
|
|
|
if (error != 0)
|
|
|
|
err(1, "UFSSUSPEND");
|
|
|
|
} else {
|
|
|
|
fso = open(device, O_WRONLY);
|
|
|
|
if (fso < 0)
|
|
|
|
err(1, "%s", device);
|
|
|
|
}
|
2012-04-30 16:08:02 +00:00
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-04-30 16:08:02 +00:00
|
|
|
* Try to access our new last block in the file system.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2012-04-30 16:08:02 +00:00
|
|
|
testbuf = malloc(sblock.fs_fsize);
|
|
|
|
if (testbuf == NULL)
|
|
|
|
err(1, "malloc");
|
2012-05-06 11:48:37 +00:00
|
|
|
rdfs((ufs2_daddr_t)((size - sblock.fs_fsize) / DEV_BSIZE),
|
2012-04-30 16:08:02 +00:00
|
|
|
sblock.fs_fsize, testbuf, fsi);
|
2012-05-06 11:48:37 +00:00
|
|
|
wtfs((ufs2_daddr_t)((size - sblock.fs_fsize) / DEV_BSIZE),
|
2012-04-30 16:08:02 +00:00
|
|
|
sblock.fs_fsize, testbuf, fso, Nflag);
|
|
|
|
free(testbuf);
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now calculate new superblock values and check for reasonable
|
2011-06-28 19:59:46 +00:00
|
|
|
* bound for new file system size:
|
2012-03-29 11:20:19 +00:00
|
|
|
* fs_size: is derived from user input
|
2000-12-09 15:27:35 +00:00
|
|
|
* fs_dsize: should get updated in the routines creating or
|
|
|
|
* updating the cylinder groups on the fly
|
|
|
|
* fs_cstotal: should get updated in the routines creating or
|
|
|
|
* updating the cylinder groups
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-06-28 19:59:46 +00:00
|
|
|
* Update the number of cylinders and cylinder groups in the file system.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC) {
|
|
|
|
sblock.fs_old_ncyl =
|
|
|
|
sblock.fs_size * sblock.fs_old_nspf / sblock.fs_old_spc;
|
|
|
|
if (sblock.fs_size * sblock.fs_old_nspf >
|
|
|
|
sblock.fs_old_ncyl * sblock.fs_old_spc)
|
|
|
|
sblock.fs_old_ncyl++;
|
|
|
|
}
|
|
|
|
sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
|
|
|
|
|
2012-12-16 14:37:25 +00:00
|
|
|
/*
|
|
|
|
* Allocate last cylinder group only if there is enough room
|
|
|
|
* for at least one data block.
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_size % sblock.fs_fpg != 0 &&
|
2012-12-16 14:37:25 +00:00
|
|
|
sblock.fs_size <= cgdmin(&sblock, sblock.fs_ncg - 1)) {
|
|
|
|
humanize_number(oldsizebuf, sizeof(oldsizebuf),
|
|
|
|
(sblock.fs_size % sblock.fs_fpg) * sblock.fs_fsize,
|
|
|
|
"B", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
warnx("no room to allocate last cylinder group; "
|
|
|
|
"leaving %s unused", oldsizebuf);
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_ncg--;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (sblock.fs_magic == FS_UFS1_MAGIC)
|
|
|
|
sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
|
2022-06-28 04:46:15 +00:00
|
|
|
sblock.fs_old_size = sblock.fs_size =
|
|
|
|
sblock.fs_ncg * sblock.fs_fpg;
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the space for the cylinder group summary information in the
|
|
|
|
* respective cylinder group data area.
|
|
|
|
*/
|
|
|
|
sblock.fs_cssize =
|
|
|
|
fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
|
2003-04-26 15:22:29 +00:00
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (osblock.fs_size >= sblock.fs_size)
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "not enough new space");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_PRINT0("sblock calculated\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, everything prepared, so now let's do the tricks.
|
|
|
|
*/
|
|
|
|
growfs(fsi, fso, Nflag);
|
|
|
|
|
|
|
|
close(fsi);
|
2012-04-30 16:08:02 +00:00
|
|
|
if (fso > -1) {
|
2012-11-18 19:01:00 +00:00
|
|
|
if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) == 0) {
|
|
|
|
error = ioctl(fso, UFSRESUME);
|
|
|
|
if (error != 0)
|
|
|
|
err(1, "UFSRESUME");
|
|
|
|
}
|
2012-04-30 16:08:02 +00:00
|
|
|
error = close(fso);
|
|
|
|
if (error != 0)
|
|
|
|
err(1, "close");
|
2023-01-15 18:20:48 +00:00
|
|
|
if (statfsp != NULL && (statfsp->f_flags & MNT_RDONLY) != 0 &&
|
|
|
|
chkdoreload(statfsp, warn) != 0)
|
|
|
|
exit(9);
|
2012-04-30 16:08:02 +00:00
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_CLOSE;
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
2012-04-18 13:50:17 +00:00
|
|
|
return (0);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump a line of usage.
|
|
|
|
*/
|
|
|
|
static void
|
2000-12-12 20:03:17 +00:00
|
|
|
usage(void)
|
2003-04-26 15:22:29 +00:00
|
|
|
{
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_FUNC("usage")
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-04-30 16:08:02 +00:00
|
|
|
fprintf(stderr, "usage: growfs [-Ny] [-s size] special | filesystem\n");
|
2000-12-12 20:03:17 +00:00
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_LEAVE;
|
2000-12-12 20:03:17 +00:00
|
|
|
exit(1);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-26 15:22:29 +00:00
|
|
|
* This updates most parameters and the bitmap related to cluster. We have to
|
|
|
|
* assume that sblock, osblock, acg are set up.
|
2000-12-09 15:27:35 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
updclst(int block)
|
2003-04-26 15:22:29 +00:00
|
|
|
{
|
2000-12-09 15:27:35 +00:00
|
|
|
DBG_FUNC("updclst")
|
2012-03-05 16:37:51 +00:00
|
|
|
static int lcs = 0;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2012-03-05 16:37:51 +00:00
|
|
|
if (sblock.fs_contigsumsize < 1) /* no clustering */
|
2000-12-09 15:27:35 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* update cluster allocation map
|
|
|
|
*/
|
|
|
|
setbit(cg_clustersfree(&acg), block);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update cluster summary table
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
if (!lcs) {
|
2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* calculate size for the trailing cluster
|
|
|
|
*/
|
2012-03-05 16:37:51 +00:00
|
|
|
for (block--; lcs < sblock.fs_contigsumsize; block--, lcs++ ) {
|
|
|
|
if (isclr(cg_clustersfree(&acg), block))
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-04-26 15:22:29 +00:00
|
|
|
}
|
2012-03-05 16:37:51 +00:00
|
|
|
if (lcs < sblock.fs_contigsumsize) {
|
|
|
|
if (lcs)
|
2000-12-09 15:27:35 +00:00
|
|
|
cg_clustersum(&acg)[lcs]--;
|
|
|
|
lcs++;
|
|
|
|
cg_clustersum(&acg)[lcs]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
2012-04-30 16:08:02 +00:00
|
|
|
|
2017-10-10 16:17:03 +00:00
|
|
|
/*
|
|
|
|
* Calculate the check-hash of the cylinder group.
|
|
|
|
*/
|
|
|
|
static void
|
2017-10-10 21:16:07 +00:00
|
|
|
cgckhash(struct cg *cgp)
|
2017-10-10 16:17:03 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if ((sblock.fs_metackhash & CK_CYLGRP) == 0)
|
|
|
|
return;
|
|
|
|
cgp->cg_ckhash = 0;
|
|
|
|
cgp->cg_ckhash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
|
|
|
|
}
|