View | Details | Raw Unified | Return to bug 57018
Collapse All | Expand All

(-)sbin/growfs/Makefile (+2 lines)
Lines 11-16 Link Here
11
PROG=   growfs
11
PROG=   growfs
12
SRCS=   growfs.c
12
SRCS=   growfs.c
13
MAN=	growfs.8
13
MAN=	growfs.8
14
DPADD=	${LIBUFS}
15
LDADD=	-lufs
14
16
15
WARNS?=	0
17
WARNS?=	0
16
18
(-)sbin/growfs/growfs.c (-129 / +88 lines)
Lines 52-60 Link Here
52
#endif /* not lint */
52
#endif /* not lint */
53
53
54
/* ********************************************************** INCLUDES ***** */
54
/* ********************************************************** INCLUDES ***** */
55
#include <sys/types.h>
55
#include <sys/param.h>
56
#include <sys/param.h>
56
#include <sys/disklabel.h>
57
#include <sys/disklabel.h>
57
#include <sys/ioctl.h>
58
#include <sys/ioctl.h>
59
#include <sys/mount.h>
58
#include <sys/stat.h>
60
#include <sys/stat.h>
59
#include <sys/disk.h>
61
#include <sys/disk.h>
60
62
Lines 67-74 Link Here
67
#include <stdlib.h>
69
#include <stdlib.h>
68
#include <string.h>
70
#include <string.h>
69
#include <unistd.h>
71
#include <unistd.h>
72
#include <ufs/ufs/ufsmount.h>
70
#include <ufs/ufs/dinode.h>
73
#include <ufs/ufs/dinode.h>
71
#include <ufs/ffs/fs.h>
74
#include <ufs/ffs/fs.h>
75
#include <libufs.h>
72
76
73
#include "debug.h"
77
#include "debug.h"
74
78
Lines 77-94 Link Here
77
int	_dbg_lvl_ = (DL_INFO);	/* DL_TRC */
81
int	_dbg_lvl_ = (DL_INFO);	/* DL_TRC */
78
#endif /* FS_DEBUG */
82
#endif /* FS_DEBUG */
79
83
84
struct uufsd disk;
85
80
static union {
86
static union {
81
	struct fs	fs;
87
	struct fs	fs;
82
	char	pad[SBLOCKSIZE];
88
	char	pad[SBLOCKSIZE];
83
} fsun1, fsun2;
89
} fsun1;
84
#define	sblock	fsun1.fs	/* the new superblock */
90
#define fsun2	disk.d_sbunion
85
#define	osblock	fsun2.fs	/* the old superblock */
91
#define	sblock	disk.d_fs	/* the new superblock */
86
92
#define	osblock	fsun1.fs	/* the old superblock */
87
/*
88
 * Possible superblock locations ordered from most to least likely.
89
 */
90
static int sblock_try[] = SBLOCKSEARCH;
91
static ufs2_daddr_t sblockloc;
92
93
93
static union {
94
static union {
94
	struct cg	cg;
95
	struct cg	cg;
Lines 112-117 Link Here
112
static char		inobuf[MAXBSIZE];	/* inode block */
113
static char		inobuf[MAXBSIZE];	/* inode block */
113
static int		maxino;			/* last valid inode */
114
static int		maxino;			/* last valid inode */
114
static int		unlabeled;     /* unlabeled partition, e.g. vinum volume etc. */
115
static int		unlabeled;     /* unlabeled partition, e.g. vinum volume etc. */
116
static int		Nflag;
115
117
116
/*
118
/*
117
 * An array of elements of type struct gfs_bpp describes all blocks to
119
 * An array of elements of type struct gfs_bpp describes all blocks to
Lines 128-155 Link Here
128
};
130
};
129
131
130
/* ******************************************************** PROTOTYPES ***** */
132
/* ******************************************************** PROTOTYPES ***** */
131
static void	growfs(int, int, unsigned int);
133
static void	growfs(void);
132
static void	rdfs(ufs2_daddr_t, size_t, void *, int);
134
static void	rdfs(ufs2_daddr_t, size_t, void *);
133
static void	wtfs(ufs2_daddr_t, size_t, void *, int, unsigned int);
135
static void	wtfs(ufs2_daddr_t, size_t, void *);
134
static ufs2_daddr_t alloc(void);
136
static ufs2_daddr_t alloc(void);
135
static int	charsperline(void);
137
static int	charsperline(void);
136
static void	usage(void);
138
static void	usage(void);
137
static int	isblock(struct fs *, unsigned char *, int);
139
static int	isblock(struct fs *, unsigned char *, int);
138
static void	clrblock(struct fs *, unsigned char *, int);
140
static void	clrblock(struct fs *, unsigned char *, int);
139
static void	setblock(struct fs *, unsigned char *, int);
141
static void	setblock(struct fs *, unsigned char *, int);
140
static void	initcg(int, time_t, int, unsigned int);
142
static void	initcg(int, time_t);
141
static void	updjcg(int, time_t, int, int, unsigned int);
143
static void	updjcg(int, time_t);
142
static void	updcsloc(time_t, int, int, unsigned int);
144
static void	updcsloc(time_t);
143
static struct disklabel	*get_disklabel(int);
145
static struct disklabel	*get_disklabel(int);
144
static void	return_disklabel(int, struct disklabel *, unsigned int);
146
static void	return_disklabel(int, struct disklabel *);
145
static union dinode *ginode(ino_t, int, int);
147
static union dinode *ginode(ino_t, int);
146
static void	frag_adjust(ufs2_daddr_t, int);
148
static void	frag_adjust(ufs2_daddr_t, int);
147
static int	cond_bl_upd(ufs2_daddr_t *, struct gfs_bpp *, int, int,
149
static int	cond_bl_upd(ufs2_daddr_t *, struct gfs_bpp *);
148
		    unsigned int);
149
static void	updclst(int);
150
static void	updclst(int);
150
static void	updrefs(int, ino_t, struct gfs_bpp *, int, int, unsigned int);
151
static void	updrefs(int, ino_t, struct gfs_bpp *);
151
static void	indirchk(ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t, ufs_lbn_t,
152
static void	indirchk(ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t, ufs_lbn_t,
152
		    struct gfs_bpp *, int, int, unsigned int);
153
		    struct gfs_bpp *);
153
static void	get_dev_size(int, int *);
154
static void	get_dev_size(int, int *);
154
155
155
/* ************************************************************ growfs ***** */
156
/* ************************************************************ growfs ***** */
Lines 165-171 Link Here
165
 * copies.
166
 * copies.
166
 */
167
 */
167
static void
168
static void
168
growfs(int fsi, int fso, unsigned int Nflag)
169
growfs(void)
169
{
170
{
170
	DBG_FUNC("growfs")
171
	DBG_FUNC("growfs")
171
	int	i;
172
	int	i;
Lines 199-205 Link Here
199
	for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
200
	for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
200
		rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
201
		rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
201
		    numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i,
202
		    numfrags(&osblock, i)), (size_t)MIN(osblock.fs_cssize - i,
202
		    osblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
203
		    osblock.fs_bsize), (void *)(((char *)fscs)+i));
203
	}
204
	}
204
205
205
#ifdef FS_DEBUG
206
#ifdef FS_DEBUG
Lines 223-229 Link Here
223
	/*
224
	/*
224
	 * Do all needed changes in the former last cylinder group.
225
	 * Do all needed changes in the former last cylinder group.
225
	 */
226
	 */
226
	updjcg(osblock.fs_ncg-1, utime, fsi, fso, Nflag);
227
	updjcg(osblock.fs_ncg-1, utime);
227
228
228
	/*
229
	/*
229
	 * Dump out summary information about file system.
230
	 * Dump out summary information about file system.
Lines 251-257 Link Here
251
	 * Iterate for only the new cylinder groups.
252
	 * Iterate for only the new cylinder groups.
252
	 */
253
	 */
253
	for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
254
	for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
254
		initcg(cylno, utime, fso, Nflag);
255
		initcg(cylno, utime);
255
		j = sprintf(tmpbuf, " %d%s",
256
		j = sprintf(tmpbuf, " %d%s",
256
		    (int)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
257
		    (int)fsbtodb(&sblock, cgsblock(&sblock, cylno)),
257
		    cylno < (sblock.fs_ncg-1) ? "," : "" );
258
		    cylno < (sblock.fs_ncg-1) ? "," : "" );
Lines 269-275 Link Here
269
	 * Do all needed changes in the first cylinder group.
270
	 * Do all needed changes in the first cylinder group.
270
	 * allocate blocks in new location
271
	 * allocate blocks in new location
271
	 */
272
	 */
272
	updcsloc(utime, fsi, fso, Nflag);
273
	updcsloc(utime);
273
274
274
	/*
275
	/*
275
	 * Now write the cylinder summary back to disk.
276
	 * Now write the cylinder summary back to disk.
Lines 277-283 Link Here
277
	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
278
	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
278
		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
279
		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
279
		    (size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize),
280
		    (size_t)MIN(sblock.fs_cssize - i, sblock.fs_bsize),
280
		    (void *)(((char *)fscs) + i), fso, Nflag);
281
		    (void *)(((char *)fscs) + i));
281
	}
282
	}
282
	DBG_PRINT0("fscs written\n");
283
	DBG_PRINT0("fscs written\n");
283
284
Lines 302-308 Link Here
302
	 * Now write the new superblock back to disk.
303
	 * Now write the new superblock back to disk.
303
	 */
304
	 */
304
	sblock.fs_time = utime;
305
	sblock.fs_time = utime;
305
	wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
306
	if (Nflag == 0 && sbwrite(&disk, 0) == -1)
307
		err(1, "sbwrite: %s", disk.d_error);
306
	DBG_PRINT0("sblock written\n");
308
	DBG_PRINT0("sblock written\n");
307
	DBG_DUMP_FS(&sblock,
309
	DBG_DUMP_FS(&sblock,
308
	    "new initial sblock");
310
	    "new initial sblock");
Lines 344-350 Link Here
344
	 */
346
	 */
345
	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
347
	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
346
		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
348
		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
347
		    (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
349
		    (size_t)SBLOCKSIZE, (void *)&sblock);
348
	}
350
	}
349
	DBG_PRINT0("sblock copies written\n");
351
	DBG_PRINT0("sblock copies written\n");
350
	DBG_DUMP_FS(&sblock,
352
	DBG_DUMP_FS(&sblock,
Lines 362-372 Link Here
362
 * provisions for that case are removed here.
364
 * provisions for that case are removed here.
363
 */
365
 */
364
static void
366
static void
365
initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
367
initcg(int cylno, time_t utime)
366
{
368
{
367
	DBG_FUNC("initcg")
369
	DBG_FUNC("initcg")
368
	static caddr_t iobuf;
370
	static caddr_t iobuf;
369
	long i, j, d, dlower, dupper, blkno, start;
371
	long i, d, dlower, dupper, blkno, start;
370
	ufs2_daddr_t cbase, dmax;
372
	ufs2_daddr_t cbase, dmax;
371
	struct ufs1_dinode *dp1;
373
	struct ufs1_dinode *dp1;
372
	struct ufs2_dinode *dp2;
374
	struct ufs2_dinode *dp2;
Lines 453-459 Link Here
453
			}
455
			}
454
#endif
456
#endif
455
		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
457
		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
456
		    sblock.fs_bsize, iobuf, fso, Nflag);
458
		    sblock.fs_bsize, iobuf);
457
	}
459
	}
458
	if (cylno > 0) {
460
	if (cylno > 0) {
459
		/*
461
		/*
Lines 527-533 Link Here
527
	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
529
	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
528
	*cs = acg.cg_cs;
530
	*cs = acg.cg_cs;
529
	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
531
	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
530
		sblock.fs_bsize, (char *)&acg, fso, Nflag);
532
		sblock.fs_bsize, (char *)&acg);
531
	DBG_DUMP_CG(&sblock,
533
	DBG_DUMP_CG(&sblock,
532
	    "new cg",
534
	    "new cg",
533
	    &acg);
535
	    &acg);
Lines 605-612 Link Here
605
 * out if a write back operation is needed.
607
 * out if a write back operation is needed.
606
 */
608
 */
607
static int
609
static int
608
cond_bl_upd(ufs2_daddr_t *block, struct gfs_bpp *field, int fsi, int fso,
610
cond_bl_upd(ufs2_daddr_t *block, struct gfs_bpp *field)
609
    unsigned int Nflag)
610
{
611
{
611
	DBG_FUNC("cond_bl_upd")
612
	DBG_FUNC("cond_bl_upd")
612
	struct gfs_bpp *f;
613
	struct gfs_bpp *f;
Lines 644-651 Link Here
644
		if (!ibuf)
645
		if (!ibuf)
645
			errx(1, "malloc failed");
646
			errx(1, "malloc failed");
646
		src -= fragnum;
647
		src -= fragnum;
647
		rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf, fsi);
648
		rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf);
648
		wtfs(dst, (size_t)sblock.fs_bsize, ibuf, fso, Nflag);
649
		wtfs(dst, (size_t)sblock.fs_bsize, ibuf);
649
		free(ibuf);
650
		free(ibuf);
650
		/*
651
		/*
651
		 * The same block can't be found again in this loop.
652
		 * The same block can't be found again in this loop.
Lines 670-676 Link Here
670
 * tables and cluster summary during all those operations.
671
 * tables and cluster summary during all those operations.
671
 */
672
 */
672
static void
673
static void
673
updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
674
updjcg(int cylno, time_t utime)
674
{
675
{
675
	DBG_FUNC("updjcg")
676
	DBG_FUNC("updjcg")
676
	ufs2_daddr_t	cbase, dmax, dupper;
677
	ufs2_daddr_t	cbase, dmax, dupper;
Lines 685-691 Link Here
685
	 * a copy.
686
	 * a copy.
686
	 */
687
	 */
687
	rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
688
	rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
688
	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
689
	    (size_t)osblock.fs_cgsize, (void *)&aocg);
689
	DBG_PRINT0("jcg read\n");
690
	DBG_PRINT0("jcg read\n");
690
	DBG_DUMP_CG(&sblock,
691
	DBG_DUMP_CG(&sblock,
691
	    "old joining cg",
692
	    "old joining cg",
Lines 706-712 Link Here
706
			acg.cg_old_ncyl=sblock.fs_old_cpg;
707
			acg.cg_old_ncyl=sblock.fs_old_cpg;
707
708
708
		wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
709
		wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
709
		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
710
		    (size_t)sblock.fs_cgsize, (void *)&acg);
710
		DBG_PRINT0("jcg written\n");
711
		DBG_PRINT0("jcg written\n");
711
		DBG_DUMP_CG(&sblock,
712
		DBG_DUMP_CG(&sblock,
712
		    "new joining cg",
713
		    "new joining cg",
Lines 895-901 Link Here
895
	 * Write the updated "joining" cylinder group back to disk.
896
	 * Write the updated "joining" cylinder group back to disk.
896
	 */
897
	 */
897
	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
898
	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
898
	    (void *)&acg, fso, Nflag);
899
	    (void *)&acg);
899
	DBG_PRINT0("jcg written\n");
900
	DBG_PRINT0("jcg written\n");
900
	DBG_DUMP_CG(&sblock,
901
	DBG_DUMP_CG(&sblock,
901
	    "new joining cg",
902
	    "new joining cg",
Lines 923-929 Link Here
923
 * completely avoid implementing copy on write if we stick to method (2) only.
924
 * completely avoid implementing copy on write if we stick to method (2) only.
924
 */
925
 */
925
static void
926
static void
926
updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
927
updcsloc(time_t utime)
927
{
928
{
928
	DBG_FUNC("updcsloc")
929
	DBG_FUNC("updcsloc")
929
	struct csum	*cs;
930
	struct csum	*cs;
Lines 959-965 Link Here
959
	 *	block from disk.
960
	 *	block from disk.
960
	 */
961
	 */
961
	rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
962
	rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
962
	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
963
	    (size_t)osblock.fs_cgsize, (void *)&aocg);
963
	DBG_PRINT0("oscg read\n");
964
	DBG_PRINT0("oscg read\n");
964
	DBG_DUMP_CG(&sblock,
965
	DBG_DUMP_CG(&sblock,
965
	    "old summary cg",
966
	    "old summary cg",
Lines 1111-1117 Link Here
1111
		 * summary back to disk.
1112
		 * summary back to disk.
1112
		 */
1113
		 */
1113
		wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1114
		wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1114
		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1115
		    (size_t)sblock.fs_cgsize, (void *)&acg);
1115
		DBG_PRINT0("oscg written\n");
1116
		DBG_PRINT0("oscg written\n");
1116
		DBG_DUMP_CG(&sblock,
1117
		DBG_DUMP_CG(&sblock,
1117
		    "old summary cg",
1118
		    "old summary cg",
Lines 1142-1148 Link Here
1142
		 * summary from disk, and make a copy.
1143
		 * summary from disk, and make a copy.
1143
		 */
1144
		 */
1144
		rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1145
		rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1145
		    (size_t)sblock.fs_cgsize, (void *)&aocg, fsi);
1146
		    (size_t)sblock.fs_cgsize, (void *)&aocg);
1146
		DBG_PRINT0("nscg read\n");
1147
		DBG_PRINT0("nscg read\n");
1147
		DBG_DUMP_CG(&sblock,
1148
		DBG_DUMP_CG(&sblock,
1148
		    "new summary cg",
1149
		    "new summary cg",
Lines 1210-1216 Link Here
1210
		 * back to disk.
1211
		 * back to disk.
1211
		 */
1212
		 */
1212
		wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1213
		wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1213
		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1214
		    (size_t)sblock.fs_cgsize, (void *)&acg);
1214
		DBG_PRINT0("nscg written\n");
1215
		DBG_PRINT0("nscg written\n");
1215
		DBG_DUMP_CG(&sblock,
1216
		DBG_DUMP_CG(&sblock,
1216
		    "new summary cg",
1217
		    "new summary cg",
Lines 1461-1469 Link Here
1461
			 *	in the case we have any active snapshots.
1462
			 *	in the case we have any active snapshots.
1462
			 */
1463
			 */
1463
			rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag),
1464
			rdfs(fsbtodb(&sblock, bp[i].old*sblock.fs_frag),
1464
			    (size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1465
			    (size_t)sblock.fs_bsize, (void *)&ablk);
1465
			wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag),
1466
			wtfs(fsbtodb(&sblock, bp[i].new*sblock.fs_frag),
1466
			    (size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag);
1467
			    (size_t)sblock.fs_bsize, (void *)&ablk);
1467
			DBG_DUMP_HEX(&sblock,
1468
			DBG_DUMP_HEX(&sblock,
1468
			    "copied full block",
1469
			    "copied full block",
1469
			    (unsigned char *)&ablk);
1470
			    (unsigned char *)&ablk);
Lines 1483-1489 Link Here
1483
			DBG_PRINT1("scg doing cg (%d)\n",
1484
			DBG_PRINT1("scg doing cg (%d)\n",
1484
			    cylno);
1485
			    cylno);
1485
			for(inc=osblock.fs_ipg-1 ; inc>=0 ; inc--) {
1486
			for(inc=osblock.fs_ipg-1 ; inc>=0 ; inc--) {
1486
				updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1487
				updrefs(cylno, (ino_t)inc, bp);
1487
			}
1488
			}
1488
		}
1489
		}
1489
1490
Lines 1493-1499 Link Here
1493
		 */
1494
		 */
1494
		for(i=0; i<ind; i++) {
1495
		for(i=0; i<ind; i++) {
1495
			if(!bp[i].found || (bp[i].found>sblock.fs_frag)) {
1496
			if(!bp[i].found || (bp[i].found>sblock.fs_frag)) {
1496
				warnx("error: %d refs found for block %d.",
1497
				warnx("error: %d refs found for block %jd.",
1497
				    bp[i].found, bp[i].old);
1498
				    bp[i].found, bp[i].old);
1498
			}
1499
			}
1499
1500
Lines 1516-1522 Link Here
1516
	 * Write summary cylinder group back to disk.
1517
	 * Write summary cylinder group back to disk.
1517
	 */
1518
	 */
1518
	wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1519
	wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1519
	    (void *)&acg, fso, Nflag);
1520
	    (void *)&acg);
1520
	DBG_PRINT0("scg written\n");
1521
	DBG_PRINT0("scg written\n");
1521
	DBG_DUMP_CG(&sblock,
1522
	DBG_DUMP_CG(&sblock,
1522
	    "new summary cg",
1523
	    "new summary cg",
Lines 1531-1550 Link Here
1531
 * Here we read some block(s) from disk.
1532
 * Here we read some block(s) from disk.
1532
 */
1533
 */
1533
static void
1534
static void
1534
rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
1535
rdfs(ufs2_daddr_t bno, size_t size, void *bf)
1535
{
1536
{
1536
	DBG_FUNC("rdfs")
1537
	DBG_FUNC("rdfs")
1537
	ssize_t	n;
1538
1538
1539
	DBG_ENTER;
1539
	DBG_ENTER;
1540
1540
1541
	if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
1541
	if (bread(&disk, bno, bf, size) == -1)
1542
		err(33, "rdfs: seek error: %ld", (long)bno);
1542
		err(1, "rdfs: read error: %ld: %s", (long)bno, disk.d_error);
1543
	}
1544
	n = read(fsi, bf, size);
1545
	if (n != (ssize_t)size) {
1546
		err(34, "rdfs: read error: %ld", (long)bno);
1547
	}
1548
1543
1549
	DBG_LEAVE;
1544
	DBG_LEAVE;
1550
	return;
1545
	return;
Lines 1555-1564 Link Here
1555
 * Here we write some block(s) to disk.
1550
 * Here we write some block(s) to disk.
1556
 */
1551
 */
1557
static void
1552
static void
1558
wtfs(ufs2_daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1553
wtfs(ufs2_daddr_t bno, size_t size, void *bf)
1559
{
1554
{
1560
	DBG_FUNC("wtfs")
1555
	DBG_FUNC("wtfs")
1561
	ssize_t	n;
1562
1556
1563
	DBG_ENTER;
1557
	DBG_ENTER;
1564
1558
Lines 1566-1578 Link Here
1566
		DBG_LEAVE;
1560
		DBG_LEAVE;
1567
		return;
1561
		return;
1568
	}
1562
	}
1569
	if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1563
1570
		err(35, "wtfs: seek error: %ld", (long)bno);
1564
	if (bwrite(&disk, bno, bf, size) == -1)
1571
	}
1565
		err(35, "wtfs: write error: %ld: %s", (long)bno, disk.d_error);
1572
	n = write(fso, bf, size);
1573
	if (n != (ssize_t)size) {
1574
		err(36, "wtfs: write error: %ld", (long)bno);
1575
	}
1576
1566
1577
	DBG_LEAVE;
1567
	DBG_LEAVE;
1578
	return;
1568
	return;
Lines 1828-1834 Link Here
1828
 * inodes.
1818
 * inodes.
1829
 */
1819
 */
1830
static union dinode *
1820
static union dinode *
1831
ginode(ino_t inumber, int fsi, int cg)
1821
ginode(ino_t inumber, int cg)
1832
{
1822
{
1833
	DBG_FUNC("ginode")
1823
	DBG_FUNC("ginode")
1834
	static ino_t	startinum = 0;	/* first inode in cached block */
1824
	static ino_t	startinum = 0;	/* first inode in cached block */
Lines 1841-1847 Link Here
1841
	if (startinum == 0 ||
1831
	if (startinum == 0 ||
1842
	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1832
	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1843
		inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber));
1833
		inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber));
1844
		rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf, fsi);
1834
		rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf);
1845
		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1835
		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1846
	}
1836
	}
1847
	DBG_LEAVE;
1837
	DBG_LEAVE;
Lines 1936-1947 Link Here
1936
	char	ch;
1926
	char	ch;
1937
	unsigned int	size=0;
1927
	unsigned int	size=0;
1938
	size_t	len;
1928
	size_t	len;
1939
	unsigned int	Nflag=0;
1940
	int	ExpertFlag=0;
1929
	int	ExpertFlag=0;
1941
	struct stat	st;
1930
	struct stat	st;
1942
	struct disklabel	*lp;
1931
	struct disklabel	*lp;
1943
	struct partition	*pp;
1932
	struct partition	*pp;
1944
	int	i,fsi,fso;
1945
    u_int32_t p_size;
1933
    u_int32_t p_size;
1946
	char	reply[5];
1934
	char	reply[5];
1947
#ifdef FSMAXSNAP
1935
#ifdef FSMAXSNAP
Lines 2016-2039 Link Here
2016
	}
2004
	}
2017
2005
2018
	/*
2006
	/*
2019
	 * Try to access our devices for writing ...
2007
	 * Try to access our device read/write.
2020
	 */
2021
	if (Nflag) {
2022
		fso = -1;
2023
	} else {
2024
		fso = open(device, O_WRONLY);
2025
		if (fso < 0) {
2026
			err(1, "%s", device);
2027
		}
2028
	}
2029
2030
	/*
2031
	 * ... and reading.
2032
	 */
2008
	 */
2033
	fsi = open(device, O_RDONLY);
2009
	if (ufs_disk_fillout(&disk, device) == -1)
2034
	if (fsi < 0) {
2010
		err(1, "ufs_disk_fillout: %s", disk.d_error);
2035
		err(1, "%s", device);
2011
	if (ufs_disk_write(&disk) == -1)
2036
	}
2012
		err(1, "ufs_disk_write: %s", disk.d_error);
2037
2013
2038
	/*
2014
	/*
2039
	 * Try to read a label and guess the slice if not specified. This
2015
	 * Try to read a label and guess the slice if not specified. This
Lines 2041-2047 Link Here
2041
	 * with the task of specifying the option -v on vinum volumes.
2017
	 * with the task of specifying the option -v on vinum volumes.
2042
	 */
2018
	 */
2043
	cp=device+strlen(device)-1;
2019
	cp=device+strlen(device)-1;
2044
	lp = get_disklabel(fsi);
2020
	lp = get_disklabel(disk.d_fd);
2045
    if (lp != NULL) {
2021
    if (lp != NULL) {
2046
        if (isdigit(*cp)) {
2022
        if (isdigit(*cp)) {
2047
            pp = &lp->d_partitions[2];
2023
            pp = &lp->d_partitions[2];
Lines 2052-2058 Link Here
2052
        }
2028
        }
2053
        p_size = pp->p_size;
2029
        p_size = pp->p_size;
2054
    } else {
2030
    } else {
2055
        get_dev_size(fsi, &p_size);
2031
        get_dev_size(disk.d_fd, &p_size);
2056
    }
2032
    }
2057
2033
2058
	/*
2034
	/*
Lines 2063-2083 Link Here
2063
	}
2039
	}
2064
2040
2065
	/*
2041
	/*
2066
	 * Read the current superblock, and take a backup.
2042
	 * Take a backup of the superblock.
2067
	 */
2043
	 */
2068
	for (i = 0; sblock_try[i] != -1; i++) {
2069
		sblockloc = sblock_try[i] / DEV_BSIZE;
2070
		rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi);
2071
		if ((osblock.fs_magic == FS_UFS1_MAGIC ||
2072
		     (osblock.fs_magic == FS_UFS2_MAGIC &&
2073
		      osblock.fs_sblockloc == sblock_try[i])) &&
2074
		    osblock.fs_bsize <= MAXBSIZE &&
2075
		    osblock.fs_bsize >= sizeof(struct fs))
2076
			break;
2077
	}
2078
	if (sblock_try[i] == -1) {
2079
		errx(1, "superblock not recognized");
2080
	}
2081
	memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2044
	memcpy((void *)&fsun1, (void *)&fsun2, sizeof(fsun2));
2082
	maxino = sblock.fs_ncg * sblock.fs_ipg;
2045
	maxino = sblock.fs_ncg * sblock.fs_ipg;
2083
2046
Lines 2102-2108 Link Here
2102
	 * Are we really growing ?
2065
	 * Are we really growing ?
2103
	 */
2066
	 */
2104
	if(osblock.fs_size >= sblock.fs_size) {
2067
	if(osblock.fs_size >= sblock.fs_size) {
2105
		errx(1, "we are not growing (%d->%d)", osblock.fs_size,
2068
		errx(1, "we are not growing (%jd->%jd)", osblock.fs_size,
2106
		    sblock.fs_size);
2069
		    sblock.fs_size);
2107
	}
2070
	}
2108
2071
Lines 2136-2150 Link Here
2136
		}
2099
		}
2137
	}
2100
	}
2138
2101
2139
	printf("new file systemsize is: %d frags\n", sblock.fs_size);
2102
	printf("new file system size is: %jd frags\n", sblock.fs_size);
2140
2103
2141
	/*
2104
	/*
2142
	 * Try to access our new last block in the file system. Even if we
2105
	 * Try to access our new last block in the file system. Even if we
2143
	 * later on realize we have to abort our operation, on that block
2106
	 * later on realize we have to abort our operation, on that block
2144
	 * there should be no data, so we can't destroy something yet.
2107
	 * there should be no data, so we can't destroy something yet.
2145
	 */
2108
	 */
2146
	wtfs((ufs2_daddr_t)p_size-1, (size_t)DEV_BSIZE, (void *)&sblock,
2109
	wtfs((ufs2_daddr_t)p_size-1, (size_t)DEV_BSIZE, (void *)&sblock);
2147
	    fso, Nflag);
2148
2110
2149
	/*
2111
	/*
2150
	 * Now calculate new superblock values and check for reasonable
2112
	 * Now calculate new superblock values and check for reasonable
Lines 2178-2184 Link Here
2178
		sblock.fs_ncg--;
2140
		sblock.fs_ncg--;
2179
		if (sblock.fs_magic == FS_UFS1_MAGIC)
2141
		if (sblock.fs_magic == FS_UFS1_MAGIC)
2180
			sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
2142
			sblock.fs_old_ncyl = sblock.fs_ncg * sblock.fs_old_cpg;
2181
		printf("Warning: %d sector(s) cannot be allocated.\n",
2143
		printf("Warning: %jd sector(s) cannot be allocated.\n",
2182
		    fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg));
2144
		    fsbtodb(&sblock, sblock.fs_size % sblock.fs_fpg));
2183
		sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
2145
		sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
2184
	}
2146
	}
Lines 2199-2205 Link Here
2199
	/*
2161
	/*
2200
	 * Ok, everything prepared, so now let's do the tricks.
2162
	 * Ok, everything prepared, so now let's do the tricks.
2201
	 */
2163
	 */
2202
	growfs(fsi, fso, Nflag);
2164
	growfs();
2203
2165
2204
	/*
2166
	/*
2205
	 * Update the disk label.
2167
	 * Update the disk label.
Lines 2209-2220 Link Here
2209
        pp->p_frag = sblock.fs_frag;
2171
        pp->p_frag = sblock.fs_frag;
2210
        pp->p_cpg = sblock.fs_fpg;
2172
        pp->p_cpg = sblock.fs_fpg;
2211
2173
2212
        return_disklabel(fso, lp, Nflag);
2174
        return_disklabel(disk.d_fd, lp);
2213
        DBG_PRINT0("label rewritten\n");
2175
        DBG_PRINT0("label rewritten\n");
2214
    }
2176
    }
2215
2177
2216
	close(fsi);
2178
	ufs_disk_close(&disk);
2217
	if(fso>-1) close(fso);
2218
2179
2219
	DBG_CLOSE;
2180
	DBG_CLOSE;
2220
2181
Lines 2227-2233 Link Here
2227
 * Write the updated disklabel back to disk.
2188
 * Write the updated disklabel back to disk.
2228
 */
2189
 */
2229
static void
2190
static void
2230
return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2191
return_disklabel(int fd, struct disklabel *lp)
2231
{
2192
{
2232
	DBG_FUNC("return_disklabel")
2193
	DBG_FUNC("return_disklabel")
2233
	u_short	sum;
2194
	u_short	sum;
Lines 2358-2371 Link Here
2358
 * cylinder group.
2319
 * cylinder group.
2359
 */
2320
 */
2360
static void
2321
static void
2361
updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2322
updrefs(int cg, ino_t in, struct gfs_bpp *bp)
2362
    Nflag)
2363
{
2323
{
2364
	DBG_FUNC("updrefs")
2324
	DBG_FUNC("updrefs")
2365
	ufs_lbn_t	len, lbn, numblks;
2325
	ufs_lbn_t	len, lbn, numblks;
2366
	ufs2_daddr_t	iptr, blksperindir;
2326
	ufs2_daddr_t	iptr, blksperindir;
2367
	union dinode	*ino;
2327
	union dinode	*ino;
2368
	int		i, mode, remaining_blocks, inodeupdated;
2328
	int		i, mode, inodeupdated;
2369
2329
2370
	DBG_ENTER;
2330
	DBG_ENTER;
2371
2331
Lines 2373-2379 Link Here
2373
	 * XXX We should skip unused inodes even from being read from disk
2333
	 * XXX We should skip unused inodes even from being read from disk
2374
	 *     here by using the bitmap.
2334
	 *     here by using the bitmap.
2375
	 */
2335
	 */
2376
	ino = ginode(in, fsi, cg);
2336
	ino = ginode(in, cg);
2377
	mode = DIP(ino, di_mode) & IFMT;
2337
	mode = DIP(ino, di_mode) & IFMT;
2378
	if (mode != IFDIR && mode != IFREG && mode != IFLNK) {
2338
	if (mode != IFDIR && mode != IFREG && mode != IFLNK) {
2379
		DBG_LEAVE;
2339
		DBG_LEAVE;
Lines 2405-2411 Link Here
2405
		iptr = DIP(ino, di_db[i]);
2365
		iptr = DIP(ino, di_db[i]);
2406
		if (iptr == 0)
2366
		if (iptr == 0)
2407
			continue;
2367
			continue;
2408
		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2368
		if (cond_bl_upd(&iptr, bp)) {
2409
			DIP(ino, di_db[i]) = iptr;
2369
			DIP(ino, di_db[i]) = iptr;
2410
			inodeupdated++;
2370
			inodeupdated++;
2411
		}
2371
		}
Lines 2419-2436 Link Here
2419
		iptr = DIP(ino, di_ib[i]);
2379
		iptr = DIP(ino, di_ib[i]);
2420
		if (iptr == 0)
2380
		if (iptr == 0)
2421
			continue;
2381
			continue;
2422
		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2382
		if (cond_bl_upd(&iptr, bp)) {
2423
			DIP(ino, di_ib[i]) = iptr;
2383
			DIP(ino, di_ib[i]) = iptr;
2424
			inodeupdated++;
2384
			inodeupdated++;
2425
		}
2385
		}
2426
		indirchk(blksperindir, lbn, iptr, numblks, bp, fsi, fso, Nflag);
2386
		indirchk(blksperindir, lbn, iptr, numblks, bp);
2427
		blksperindir *= NINDIR(&sblock);
2387
		blksperindir *= NINDIR(&sblock);
2428
		lbn += blksperindir;
2388
		lbn += blksperindir;
2429
		len -= blksperindir;
2389
		len -= blksperindir;
2430
		DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1);
2390
		DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1);
2431
	}
2391
	}
2432
	if (inodeupdated)
2392
	if (inodeupdated)
2433
		wtfs(inoblk, sblock.fs_bsize, inobuf, fso, Nflag);
2393
		wtfs(inoblk, sblock.fs_bsize, inobuf);
2434
2394
2435
	DBG_LEAVE;
2395
	DBG_LEAVE;
2436
	return;
2396
	return;
Lines 2441-2451 Link Here
2441
 */
2401
 */
2442
static void
2402
static void
2443
indirchk(ufs_lbn_t blksperindir, ufs_lbn_t lbn, ufs2_daddr_t blkno,
2403
indirchk(ufs_lbn_t blksperindir, ufs_lbn_t lbn, ufs2_daddr_t blkno,
2444
    ufs_lbn_t lastlbn, struct gfs_bpp *bp, int fsi, int fso, unsigned int Nflag)
2404
    ufs_lbn_t lastlbn, struct gfs_bpp *bp)
2445
{
2405
{
2446
	DBG_FUNC("indirchk")
2406
	DBG_FUNC("indirchk")
2447
	void *ibuf;
2407
	void *ibuf;
2448
	off_t offset;
2449
	int i, last;
2408
	int i, last;
2450
	ufs2_daddr_t iptr;
2409
	ufs2_daddr_t iptr;
2451
2410
Lines 2455-2461 Link Here
2455
	ibuf = malloc(sblock.fs_bsize);
2414
	ibuf = malloc(sblock.fs_bsize);
2456
	if (!ibuf)
2415
	if (!ibuf)
2457
		errx(1, "malloc failed");
2416
		errx(1, "malloc failed");
2458
	rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf, fsi);
2417
	rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf);
2459
	last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ?
2418
	last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ?
2460
	    howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock);
2419
	    howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock);
2461
	for (i = 0; i < last; i++) {
2420
	for (i = 0; i < last; i++) {
Lines 2465-2471 Link Here
2465
			iptr = ((ufs2_daddr_t *)ibuf)[i];
2424
			iptr = ((ufs2_daddr_t *)ibuf)[i];
2466
		if (iptr == 0)
2425
		if (iptr == 0)
2467
			continue;
2426
			continue;
2468
		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2427
		if (cond_bl_upd(&iptr, bp)) {
2469
			if (sblock.fs_magic == FS_UFS1_MAGIC)
2428
			if (sblock.fs_magic == FS_UFS1_MAGIC)
2470
				((ufs1_daddr_t *)ibuf)[i] = iptr;
2429
				((ufs1_daddr_t *)ibuf)[i] = iptr;
2471
			else
2430
			else
Lines 2474-2480 Link Here
2474
		if (blksperindir == 1)
2433
		if (blksperindir == 1)
2475
			continue;
2434
			continue;
2476
		indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i,
2435
		indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i,
2477
		    iptr, lastlbn, bp, fsi, fso, Nflag);
2436
		    iptr, lastlbn, bp);
2478
	}
2437
	}
2479
	free(ibuf);
2438
	free(ibuf);

Return to bug 57018