growfs.c revision 1.44
1/*	$OpenBSD: growfs.c,v 1.44 2015/11/20 17:31:20 mmcc Exp $	*/
2/*
3 * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
4 * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgment:
20 *      This product includes software developed by the University of
21 *      California, Berkeley and its contributors, as well as Christoph
22 *      Herrmann and Thomas-Henning von Kamptz.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * $TSHeader: src/sbin/growfs/growfs.c,v 1.5 2000/12/12 19:31:00 tomsoft Exp $
40 * $FreeBSD: src/sbin/growfs/growfs.c,v 1.25 2006/07/17 20:48:36 stefanf Exp $
41 *
42 */
43
44#include <sys/param.h>	/* DEV_BSIZE MAXBSIZE setbit isset isclr clrbit */
45#include <sys/types.h>
46#include <sys/disklabel.h>
47#include <sys/ioctl.h>
48#include <sys/dkio.h>
49#include <sys/stat.h>
50
51#include <stdio.h>
52#include <paths.h>
53#include <ctype.h>
54#include <err.h>
55#include <fcntl.h>
56#include <limits.h>
57#include <stdlib.h>
58#include <stdint.h>
59#include <string.h>
60#include <time.h>
61#include <unistd.h>
62#include <util.h>
63
64#include <ufs/ufs/dinode.h>
65#include <ufs/ffs/fs.h>
66
67#define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
68#define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
69
70#include "debug.h"
71
72#define	rounddown(x, y)	(((x)/(y))*(y))
73#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
74
75#ifdef FS_DEBUG
76int	_dbg_lvl_ = (DL_INFO);	/* DL_TRC */
77#endif /* FS_DEBUG */
78
79static int quiet;		/* quiet flag */
80
81static union {
82	struct	fs fs;
83	char	pad[SBLOCKSIZE];
84} fsun1, fsun2;
85#define	sblock	fsun1.fs	/* the new superblock */
86#define	osblock	fsun2.fs	/* the old superblock */
87
88/*
89 * Possible superblock locations ordered from most to least likely.
90 */
91static int sblock_try[] = SBLOCKSEARCH;
92static daddr_t sblockloc;
93
94static union {
95	struct	cg cg;
96	char	pad[MAXBSIZE];
97} cgun1, cgun2;
98#define	acg	cgun1.cg	/* a cylinder cgroup (new) */
99#define	aocg	cgun2.cg	/* an old cylinder group */
100
101static char	ablk[MAXBSIZE];		/* a block */
102
103static struct csum	*fscs;	/* cylinder summary */
104
105union dinode {
106	struct ufs1_dinode dp1;
107	struct ufs2_dinode dp2;
108};
109#define	DIP(dp, field) \
110	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
111	(uint32_t)(dp)->dp1.field : (dp)->dp2.field)
112#define	DIP_SET(dp, field, val) do { \
113	if (sblock.fs_magic == FS_UFS1_MAGIC) \
114		(dp)->dp1.field = (val); \
115	else \
116		(dp)->dp2.field = (val); \
117	} while (0)
118static daddr_t 	inoblk;			/* inode block address */
119static char		inobuf[MAXBSIZE];	/* inode block */
120ino_t			maxino;			/* last valid inode */
121
122/*
123 * An  array of elements of type struct gfs_bpp describes all blocks  to
124 * be relocated in order to free the space needed for the cylinder group
125 * summary for all cylinder groups located in the first cylinder group.
126 */
127struct gfs_bpp {
128	daddr_t		old;		/* old block number */
129	daddr_t		new;		/* new block number */
130#define GFS_FL_FIRST	1
131#define GFS_FL_LAST	2
132	unsigned int	flags;		/* special handling required */
133	int		found;		/* how many references were updated */
134};
135
136static void	growfs(int, int, unsigned int);
137static void	rdfs(daddr_t, size_t, void *, int);
138static void	wtfs(daddr_t, size_t, void *, int, unsigned int);
139static daddr_t alloc(void);
140static int	charsperline(void);
141static void	usage(void);
142static int	isblock(struct fs *, unsigned char *, int);
143static void	clrblock(struct fs *, unsigned char *, int);
144static void	setblock(struct fs *, unsigned char *, int);
145static void	initcg(int, time_t, int, unsigned int);
146static void	updjcg(int, time_t, int, int, unsigned int);
147static void	updcsloc(time_t, int, int, unsigned int);
148static struct disklabel	*get_disklabel(int);
149static void	return_disklabel(int, struct disklabel *, unsigned int);
150static union dinode *ginode(ino_t, int, int);
151static void	frag_adjust(daddr_t, int);
152static int	cond_bl_upd(daddr_t *, struct gfs_bpp *, int, int,
153		    unsigned int);
154static void	updclst(int);
155static void	updrefs(int, ino_t, struct gfs_bpp *, int, int, unsigned int);
156static void	indirchk(daddr_t, daddr_t, daddr_t, daddr_t,
157		    struct gfs_bpp *, int, int, unsigned int);
158static void	ffs1_sb_update(struct fs *, daddr_t);
159
160/*
161 * Here  we actually start growing the filesystem. We basically  read  the
162 * cylinder  summary  from the first cylinder group as we want  to  update
163 * this  on  the fly during our various operations. First  we  handle  the
164 * changes in the former last cylinder group. Afterwards we create all new
165 * cylinder  groups.  Now  we handle the  cylinder  group  containing  the
166 * cylinder  summary  which  might result in a  relocation  of  the  whole
167 * structure.  In the end we write back the updated cylinder summary,  the
168 * new superblock, and slightly patched versions of the super block
169 * copies.
170 */
171static void
172growfs(int fsi, int fso, unsigned int Nflag)
173{
174	DBG_FUNC("growfs")
175	int	i;
176	int	cylno, j;
177	time_t	utime;
178	int	width;
179	char	tmpbuf[100];
180
181	DBG_ENTER;
182
183	time(&utime);
184
185	/*
186	 * Get the cylinder summary into the memory.
187	 */
188	fscs = calloc(1, (size_t)sblock.fs_cssize);
189	if (fscs == NULL)
190		errx(1, "calloc failed");
191	for (i = 0; i < osblock.fs_cssize; i += osblock.fs_bsize) {
192		rdfs(fsbtodb(&osblock, osblock.fs_csaddr +
193		    numfrags(&osblock, i)), (size_t)MINIMUM(osblock.fs_cssize - i,
194		    osblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
195	}
196
197#ifdef FS_DEBUG
198{
199	struct csum	*dbg_csp;
200	int	dbg_csc;
201	char	dbg_line[80];
202
203	dbg_csp = fscs;
204	for (dbg_csc = 0; dbg_csc < osblock.fs_ncg; dbg_csc++) {
205		snprintf(dbg_line, sizeof(dbg_line),
206		    "%d. old csum in old location", dbg_csc);
207		DBG_DUMP_CSUM(&osblock, dbg_line, dbg_csp++);
208	}
209}
210#endif /* FS_DEBUG */
211	DBG_PRINT0("fscs read\n");
212
213	/*
214	 * Do all needed changes in the former last cylinder group.
215	 */
216	updjcg(osblock.fs_ncg - 1, utime, fsi, fso, Nflag);
217
218	/*
219	 * Dump out summary information about filesystem.
220	 */
221#define B2MBFACTOR (1 / (1024.0 * 1024.0))
222	printf("growfs: %.1fMB (%jd sectors) block size %d, fragment size %d\n",
223	    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
224	    (intmax_t)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize,
225	    sblock.fs_fsize);
226	printf("\tusing %d cylinder groups of %.2fMB, %d blks, %d inodes.\n",
227	    sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
228	    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
229	if (sblock.fs_flags & FS_DOSOFTDEP)
230		printf("\twith soft updates\n");
231#undef B2MBFACTOR
232
233	/*
234	 * Now build the cylinders group blocks and
235	 * then print out indices of cylinder groups.
236	 */
237	if (!quiet)
238		printf("super-block backups (for fsck -b #) at:\n");
239	i = 0;
240	width = charsperline();
241
242	/*
243	 * Iterate for only the new cylinder groups.
244	 */
245	for (cylno = osblock.fs_ncg; cylno < sblock.fs_ncg; cylno++) {
246		initcg(cylno, utime, fso, Nflag);
247		if (quiet)
248			continue;
249		j = snprintf(tmpbuf, sizeof(tmpbuf), " %lld%s",
250		    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
251		    cylno < (sblock.fs_ncg - 1) ? "," : "");
252		if (j >= sizeof(tmpbuf))
253			j = sizeof(tmpbuf) - 1;
254		if (j == -1 || i + j >= width) {
255			printf("\n");
256			i = 0;
257		}
258		i += j;
259		printf("%s", tmpbuf);
260		fflush(stdout);
261	}
262	if (!quiet)
263		printf("\n");
264
265	/*
266	 * Do all needed changes in the first cylinder group.
267	 * allocate blocks in new location
268	 */
269	updcsloc(utime, fsi, fso, Nflag);
270
271	/*
272	 * Now write the cylinder summary back to disk.
273	 */
274	for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
275		wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
276		    (size_t)MINIMUM(sblock.fs_cssize - i, sblock.fs_bsize),
277		    (void *)(((char *)fscs) + i), fso, Nflag);
278	}
279	DBG_PRINT0("fscs written\n");
280
281#ifdef FS_DEBUG
282{
283	struct csum	*dbg_csp;
284	int	dbg_csc;
285	char	dbg_line[80];
286
287	dbg_csp = fscs;
288	for (dbg_csc = 0; dbg_csc < sblock.fs_ncg; dbg_csc++) {
289		snprintf(dbg_line, sizeof(dbg_line),
290		    "%d. new csum in new location", dbg_csc);
291		DBG_DUMP_CSUM(&sblock, dbg_line, dbg_csp++);
292	}
293}
294#endif /* FS_DEBUG */
295
296	/*
297	 * Now write the new superblock back to disk.
298	 */
299	sblock.fs_time = utime;
300	sblock.fs_clean = 0;
301	if (sblock.fs_magic == FS_UFS1_MAGIC) {
302		sblock.fs_ffs1_time = (int32_t)sblock.fs_time;
303		sblock.fs_ffs1_size = (int32_t)sblock.fs_size;
304		sblock.fs_ffs1_dsize = (int32_t)sblock.fs_dsize;
305		sblock.fs_ffs1_csaddr = (int32_t)sblock.fs_csaddr;
306		sblock.fs_ffs1_cstotal.cs_ndir =
307		    (int32_t)sblock.fs_cstotal.cs_ndir;
308		sblock.fs_ffs1_cstotal.cs_nbfree =
309		    (int32_t)sblock.fs_cstotal.cs_nbfree;
310		sblock.fs_ffs1_cstotal.cs_nifree =
311		    (int32_t)sblock.fs_cstotal.cs_nifree;
312		sblock.fs_ffs1_cstotal.cs_nffree =
313		    (int32_t)sblock.fs_cstotal.cs_nffree;
314	}
315	wtfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
316	DBG_PRINT0("sblock written\n");
317	DBG_DUMP_FS(&sblock, "new initial sblock");
318
319	/*
320	 * Clean up the dynamic fields in our superblock copies.
321	 */
322	sblock.fs_fmod = 0;
323	sblock.fs_clean = 1;
324	sblock.fs_ronly = 0;
325	sblock.fs_cgrotor = 0;
326	sblock.fs_state = 0;
327	memset(&sblock.fs_fsmnt, 0, sizeof(sblock.fs_fsmnt));
328	sblock.fs_flags &= FS_DOSOFTDEP;
329	if (sblock.fs_magic == FS_UFS1_MAGIC)
330		sblock.fs_ffs1_flags &= FS_DOSOFTDEP;
331
332	/*
333	 * XXX
334	 * The following fields are currently distributed from the  superblock
335	 * to the copies:
336	 *     fs_minfree
337	 *     fs_rotdelay
338	 *     fs_maxcontig
339	 *     fs_maxbpg
340	 *     fs_minfree,
341	 *     fs_optim
342	 *     fs_flags regarding SOFTPDATES
343	 *
344	 * We probably should rather change the summary for the cylinder group
345	 * statistics here to the value of what would be in there, if the file
346	 * system were created initially with the new size. Therefor we  still
347	 * need to find an easy way of calculating that.
348	 * Possibly we can try to read the first superblock copy and apply the
349	 * "diffed" stats between the old and new superblock by still  copying
350	 * certain parameters onto that.
351	 */
352
353	/*
354	 * Write out the duplicate superblocks.
355	 */
356	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
357		wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
358		    (size_t)SBLOCKSIZE, (void *)&sblock, fso, Nflag);
359	}
360	DBG_PRINT0("sblock copies written\n");
361	DBG_DUMP_FS(&sblock, "new other sblocks");
362
363	DBG_LEAVE;
364}
365
366/*
367 * This creates a new cylinder group structure, for more details please  see
368 * the  source of newfs(8), as this function is taken over almost unchanged.
369 * As  this  is  never called for the  first  cylinder  group,  the  special
370 * provisions for that case are removed here.
371 */
372static void
373initcg(int cylno, time_t utime, int fso, unsigned int Nflag)
374{
375	DBG_FUNC("initcg")
376	static char *iobuf;
377	daddr_t d, dlower, dupper, blkno, start;
378	daddr_t i, cbase, dmax;
379	struct ufs1_dinode *dp1;
380	struct ufs2_dinode *dp2;
381	struct csum *cs;
382	ino_t j;
383	size_t iobufsize;
384
385	if (sblock.fs_bsize < SBLOCKSIZE)
386		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
387	else
388		iobufsize = 4 * sblock.fs_bsize;
389
390	if (iobuf == NULL && (iobuf = malloc(iobufsize)) == NULL)
391		errx(37, "panic: cannot allocate I/O buffer");
392	bzero(iobuf, iobufsize);
393
394	/*
395	 * Determine block bounds for cylinder group.
396	 * Allow space for super block summary information in first
397	 * cylinder group.
398	 */
399	cbase = cgbase(&sblock, cylno);
400	dmax = cbase + sblock.fs_fpg;
401	if (dmax > sblock.fs_size)
402		dmax = sblock.fs_size;
403	dlower = cgsblock(&sblock, cylno) - cbase;
404	dupper = cgdmin(&sblock, cylno) - cbase;
405	if (cylno == 0) /* XXX fscs may be relocated */
406		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
407	cs = &fscs[cylno];
408	memset(&acg, 0, sblock.fs_cgsize);
409	acg.cg_ffs2_time = utime;
410	acg.cg_magic = CG_MAGIC;
411	acg.cg_cgx = cylno;
412	acg.cg_ffs2_niblk = sblock.fs_ipg;
413	acg.cg_initediblk = MINIMUM(sblock.fs_ipg, 2 * INOPB(&sblock));
414	acg.cg_ndblk = dmax - cbase;
415	if (sblock.fs_contigsumsize > 0)
416		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
417	start = sizeof(struct cg);
418	if (sblock.fs_magic == FS_UFS2_MAGIC) {
419		acg.cg_iusedoff = start;
420	} else {
421		if (cylno == sblock.fs_ncg - 1)
422			acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
423		else
424			acg.cg_ncyl = sblock.fs_cpg;
425		acg.cg_time = (int32_t)acg.cg_ffs2_time;
426		acg.cg_ffs2_time = 0;
427		acg.cg_niblk = (int16_t)acg.cg_ffs2_niblk;
428		acg.cg_ffs2_niblk = 0;
429		acg.cg_initediblk = 0;
430		acg.cg_btotoff = start;
431		acg.cg_boff = acg.cg_btotoff +
432		    sblock.fs_cpg * sizeof(int32_t);
433		acg.cg_iusedoff = acg.cg_boff +
434		    sblock.fs_cpg * sizeof(u_int16_t);
435	}
436	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
437	acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT);
438	if (sblock.fs_contigsumsize > 0) {
439		acg.cg_clustersumoff =
440		    roundup(acg.cg_nextfreeoff, sizeof(u_int32_t));
441		acg.cg_clustersumoff -= sizeof(u_int32_t);
442		acg.cg_clusteroff = acg.cg_clustersumoff +
443		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
444		acg.cg_nextfreeoff = acg.cg_clusteroff +
445		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
446	}
447	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
448		/*
449		 * This should never happen as we would have had that panic
450		 *     already on filesystem creation
451		 */
452		errx(37, "panic: cylinder group too big");
453	}
454	acg.cg_cs.cs_nifree += sblock.fs_ipg;
455	if (cylno == 0) {
456		for (i = 0; i < ROOTINO; i++) {
457			setbit(cg_inosused(&acg), i);
458			acg.cg_cs.cs_nifree--;
459		}
460	}
461	if (cylno > 0) {
462		/*
463		 * In cylno 0, beginning space is reserved
464		 * for boot and super blocks.
465		 */
466		for (d = 0; d < dlower; d += sblock.fs_frag) {
467			blkno = d / sblock.fs_frag;
468			setblock(&sblock, cg_blksfree(&acg), blkno);
469			if (sblock.fs_contigsumsize > 0)
470				setbit(cg_clustersfree(&acg), blkno);
471			acg.cg_cs.cs_nbfree++;
472		}
473		sblock.fs_dsize += dlower;
474	}
475	sblock.fs_dsize += acg.cg_ndblk - dupper;
476	if ((i = dupper % sblock.fs_frag)) {
477		acg.cg_frsum[sblock.fs_frag - i]++;
478		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
479			setbit(cg_blksfree(&acg), dupper);
480			acg.cg_cs.cs_nffree++;
481		}
482	}
483	for (d = dupper; d + sblock.fs_frag <= acg.cg_ndblk;
484	     d += sblock.fs_frag) {
485		blkno = d / sblock.fs_frag;
486		setblock(&sblock, cg_blksfree(&acg), blkno);
487		if (sblock.fs_contigsumsize > 0)
488			setbit(cg_clustersfree(&acg), blkno);
489		acg.cg_cs.cs_nbfree++;
490	}
491	if (d < acg.cg_ndblk) {
492		acg.cg_frsum[acg.cg_ndblk - d]++;
493		for (; d < acg.cg_ndblk; d++) {
494			setbit(cg_blksfree(&acg), d);
495			acg.cg_cs.cs_nffree++;
496		}
497	}
498	if (sblock.fs_contigsumsize > 0) {
499		int32_t	*sump = cg_clustersum(&acg);
500		u_char	*mapp = cg_clustersfree(&acg);
501		int	map = *mapp++;
502		int	bit = 1;
503		int	run = 0;
504
505		for (i = 0; i < acg.cg_nclusterblks; i++) {
506			if ((map & bit) != 0)
507				run++;
508			else if (run != 0) {
509				if (run > sblock.fs_contigsumsize)
510					run = sblock.fs_contigsumsize;
511				sump[run]++;
512				run = 0;
513			}
514			if ((i & (CHAR_BIT - 1)) != CHAR_BIT - 1)
515				bit <<= 1;
516			else {
517				map = *mapp++;
518				bit = 1;
519			}
520		}
521		if (run != 0) {
522			if (run > sblock.fs_contigsumsize)
523				run = sblock.fs_contigsumsize;
524			sump[run]++;
525		}
526	}
527	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
528	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
529	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
530	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
531	*cs = acg.cg_cs;
532
533	/*
534	 * Write out the duplicate superblock, the cylinder group map
535	 * and two blocks worth of inodes in a single write.
536	 */
537	bcopy(&sblock, iobuf, SBLOCKSIZE);
538	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
539	bcopy(&acg, &iobuf[start], sblock.fs_cgsize);
540	start += sblock.fs_bsize;
541	dp1 = (struct ufs1_dinode *)&iobuf[start];
542	dp2 = (struct ufs2_dinode *)&iobuf[start];
543	for (i = MINIMUM(sblock.fs_ipg, 2 * INOPB(&sblock)); i != 0; i--) {
544		if (sblock.fs_magic == FS_UFS1_MAGIC) {
545			dp1->di_gen = arc4random();
546			dp1++;
547		} else {
548			dp2->di_gen = arc4random();
549			dp2++;
550		}
551	}
552	wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize,
553	    iobuf, fso, Nflag);
554
555	/* Initialize inodes for FFS1. */
556	if (sblock.fs_magic == FS_UFS1_MAGIC) {
557		for (i = 2 * sblock.fs_frag; i < sblock.fs_ipg / INOPF(&sblock);
558		     i += sblock.fs_frag) {
559			dp1 = (struct ufs1_dinode *)&iobuf[start];
560			for (j = 0; j < INOPB(&sblock); j++) {
561				dp1->di_gen = arc4random();
562				dp1++;
563			}
564			wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
565			    (size_t)sblock.fs_bsize, &iobuf[start], fso, Nflag);
566		}
567	}
568
569	DBG_DUMP_CG(&sblock, "new cg", &acg);
570
571	DBG_LEAVE;
572}
573
574/*
575 * Here  we add or subtract (sign +1/-1) the available fragments in  a  given
576 * block to or from the fragment statistics. By subtracting before and adding
577 * after  an operation on the free frag map we can easy update  the  fragment
578 * statistic, which seems to be otherwise a rather complex operation.
579 */
580static void
581frag_adjust(daddr_t frag, int sign)
582{
583	DBG_FUNC("frag_adjust")
584	int fragsize;
585	int f;
586
587	DBG_ENTER;
588
589	fragsize = 0;
590	/*
591	 * Here frag only needs to point to any fragment in the block we want
592	 * to examine.
593	 */
594	for (f = rounddown(frag, sblock.fs_frag);
595	    f < roundup(frag + 1, sblock.fs_frag);
596	    f++) {
597		/*
598		 * Count contiguous free fragments.
599		 */
600		if (isset(cg_blksfree(&acg), f)) {
601			fragsize++;
602		} else {
603			if (fragsize && fragsize < sblock.fs_frag) {
604				/*
605				 * We found something in between.
606				 */
607				acg.cg_frsum[fragsize] += sign;
608				DBG_PRINT2("frag_adjust [%d]+=%d\n", fragsize,
609				    sign);
610			}
611			fragsize = 0;
612		}
613	}
614	if (fragsize && fragsize < sblock.fs_frag) {
615		/*
616		 * We found something.
617		 */
618		acg.cg_frsum[fragsize] += sign;
619		DBG_PRINT2("frag_adjust [%d]+=%d\n", fragsize, sign);
620	}
621	DBG_PRINT2("frag_adjust [[%d]]+=%d\n", fragsize, sign);
622
623	DBG_LEAVE;
624}
625
626/*
627 * Here we conditionally update a pointer to a fragment. We check for all
628 * relocated blocks if any of its fragments is referenced by the current
629 * field,  and update the pointer to the respective fragment in  our  new
630 * block.  If  we find a reference we write back the  block  immediately,
631 * as there is no easy way for our general block reading engine to figure
632 * out if a write back operation is needed.
633 */
634static int
635cond_bl_upd(daddr_t *block, struct gfs_bpp *field, int fsi, int fso,
636    unsigned int Nflag)
637{
638	DBG_FUNC("cond_bl_upd")
639	struct gfs_bpp	*f;
640	daddr_t src, dst;
641	int fragnum;
642	void *ibuf;
643
644	DBG_ENTER;
645
646	for (f = field; f->old != 0; f++) {
647		src = *block;
648		if (fragstoblks(&sblock, src) != f->old)
649			continue;
650		/*
651		 * The fragment is part of the block, so update.
652		 */
653		dst = blkstofrags(&sblock, f->new);
654		fragnum = fragnum(&sblock, src);
655		*block = dst + fragnum;
656		f->found++;
657		DBG_PRINT3("scg (%jd->%jd)[%d] reference updated\n",
658		    (intmax_t)f->old, (intmax_t)f->new, fragnum);
659
660		/*
661		 * Copy the block back immediately.
662		 *
663		 * XXX	If src is from an indirect block we have
664		 *	to implement copy on write here in case of
665		 *	active snapshots.
666		 */
667		ibuf = malloc(sblock.fs_bsize);
668		if (!ibuf)
669			errx(1, "malloc failed");
670		src -= fragnum;
671		rdfs(fsbtodb(&sblock, src), (size_t)sblock.fs_bsize, ibuf, fsi);
672		wtfs(dst, (size_t)sblock.fs_bsize, ibuf, fso, Nflag);
673		free(ibuf);
674		/*
675		 * The same block can't be found again in this loop.
676		 */
677		return (1);
678	}
679
680	DBG_LEAVE;
681	return (0);
682}
683
684/*
685 * Here we do all needed work for the former last cylinder group. It has to be
686 * changed  in  any case, even if the filesystem ended exactly on the  end  of
687 * this  group, as there is some slightly inconsistent handling of the  number
688 * of cylinders in the cylinder group. We start again by reading the  cylinder
689 * group from disk. If the last block was not fully available, we first handle
690 * the  missing  fragments, then we handle all new full blocks  in  that  file
691 * system  and  finally we handle the new last fragmented block  in  the  file
692 * system.  We again have to handle the fragment statistics rotational  layout
693 * tables and cluster summary during all those operations.
694 */
695static void
696updjcg(int cylno, time_t utime, int fsi, int fso, unsigned int Nflag)
697{
698	DBG_FUNC("updjcg")
699	daddr_t	cbase, dmax, dupper;
700	struct csum	*cs;
701	int	i, k;
702	int	j = 0;
703
704	DBG_ENTER;
705
706	/*
707	 * Read the former last (joining) cylinder group from disk, and make
708	 * a copy.
709	 */
710	rdfs(fsbtodb(&osblock, cgtod(&osblock, cylno)),
711	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
712	DBG_PRINT0("jcg read\n");
713	DBG_DUMP_CG(&sblock, "old joining cg", &aocg);
714
715	memcpy(&cgun1, &cgun2, sizeof(cgun2));
716
717	/*
718	 * If the cylinder group had already its new final size almost
719	 * nothing is to be done ... except:
720	 * For some reason the value of cg_ncyl in the last cylinder group has
721	 * to  be  zero instead of fs_cpg. As this is now no longer  the  last
722	 * cylinder group we have to change that value now to fs_cpg.
723	 */
724	if (cgbase(&osblock, cylno+1) == osblock.fs_size) {
725		if (sblock.fs_magic == FS_UFS1_MAGIC)
726			acg.cg_ncyl = sblock.fs_cpg;
727
728		wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
729		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
730		DBG_PRINT0("jcg written\n");
731		DBG_DUMP_CG(&sblock, "new joining cg", &acg);
732
733		DBG_LEAVE;
734		return;
735	}
736
737	/*
738	 * Set up some variables needed later.
739	 */
740	cbase = cgbase(&sblock, cylno);
741	dmax = cbase + sblock.fs_fpg;
742	if (dmax > sblock.fs_size)
743		dmax = sblock.fs_size;
744	dupper = cgdmin(&sblock, cylno) - cbase;
745	if (cylno == 0)	/* XXX fscs may be relocated */
746		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
747
748	/*
749	 * Set pointer to the cylinder summary for our cylinder group.
750	 */
751	cs = fscs + cylno;
752
753	/*
754	 * Touch the cylinder group, update all fields in the cylinder group as
755	 * needed, update the free space in the superblock.
756	 */
757	acg.cg_time = utime;
758	if (sblock.fs_magic == FS_UFS1_MAGIC) {
759		if (cylno == sblock.fs_ncg - 1) {
760			/*
761			 * This is still the last cylinder group.
762			 */
763			acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
764		} else {
765			acg.cg_ncyl = sblock.fs_cpg;
766		}
767	}
768	DBG_PRINT2("jcg dbg: %d %u", cylno, sblock.fs_ncg);
769#ifdef FS_DEBUG
770	if (sblock.fs_magic == FS_UFS1_MAGIC)
771		DBG_PRINT2("%d %u", acg.cg_ncyl, sblock.fs_cpg);
772#endif
773	DBG_PRINT0("\n");
774	acg.cg_ndblk = dmax - cbase;
775	sblock.fs_dsize += acg.cg_ndblk-aocg.cg_ndblk;
776	if (sblock.fs_contigsumsize > 0)
777		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
778
779	/*
780	 * Now  we have to update the free fragment bitmap for our new  free
781	 * space.  There again we have to handle the fragmentation and  also
782	 * the  rotational  layout tables and the cluster summary.  This  is
783	 * also  done per fragment for the first new block if the  old  file
784	 * system end was not on a block boundary, per fragment for the  new
785	 * last block if the new filesystem end is not on a block boundary,
786	 * and per block for all space in between.
787	 *
788	 * Handle the first new block here if it was partially available
789	 * before.
790	 */
791	if (osblock.fs_size % sblock.fs_frag) {
792		if (roundup(osblock.fs_size, sblock.fs_frag) <= sblock.fs_size) {
793			/*
794			 * The new space is enough to fill at least this
795			 * block
796			 */
797			j = 0;
798			for (i = roundup(osblock.fs_size-cbase, sblock.fs_frag) - 1;
799			    i >= osblock.fs_size-cbase; i--) {
800				setbit(cg_blksfree(&acg), i);
801				acg.cg_cs.cs_nffree++;
802				j++;
803			}
804
805			/*
806			 * Check  if the fragment just created could join  an
807			 * already existing fragment at the former end of the
808			 * filesystem.
809			 */
810			if (isblock(&sblock, cg_blksfree(&acg),
811			    ((osblock.fs_size - cgbase(&sblock, cylno))/
812			    sblock.fs_frag))) {
813				/*
814				 * The block is now completely available.
815				 */
816				DBG_PRINT0("block was\n");
817				acg.cg_frsum[osblock.fs_size%sblock.fs_frag]--;
818				acg.cg_cs.cs_nbfree++;
819				acg.cg_cs.cs_nffree-=sblock.fs_frag;
820				k = rounddown(osblock.fs_size-cbase,
821				    sblock.fs_frag);
822				updclst((osblock.fs_size-cbase)/sblock.fs_frag);
823			} else {
824				/*
825				 * Lets rejoin a possible partially growed
826				 * fragment.
827				 */
828				k = 0;
829				while (isset(cg_blksfree(&acg), i) &&
830				    (i >= rounddown(osblock.fs_size - cbase,
831				    sblock.fs_frag))) {
832					i--;
833					k++;
834				}
835				if (k)
836					acg.cg_frsum[k]--;
837				acg.cg_frsum[k + j]++;
838			}
839		} else {
840			/*
841			 * We only grow by some fragments within this last
842			 * block.
843			 */
844			for (i = sblock.fs_size-cbase-1;
845			    i >= osblock.fs_size-cbase; i--) {
846				setbit(cg_blksfree(&acg), i);
847				acg.cg_cs.cs_nffree++;
848				j++;
849			}
850			/*
851			 * Lets rejoin a possible partially growed fragment.
852			 */
853			k = 0;
854			while (isset(cg_blksfree(&acg), i) &&
855			    (i >= rounddown(osblock.fs_size - cbase,
856			    sblock.fs_frag))) {
857				i--;
858				k++;
859			}
860			if (k)
861				acg.cg_frsum[k]--;
862			acg.cg_frsum[k + j]++;
863		}
864	}
865
866	/*
867	 * Handle all new complete blocks here.
868	 */
869	for (i = roundup(osblock.fs_size - cbase, sblock.fs_frag);
870	    i + sblock.fs_frag <= dmax-cbase;	/* XXX <= or only < ? */
871	    i += sblock.fs_frag) {
872		j = i / sblock.fs_frag;
873		setblock(&sblock, cg_blksfree(&acg), j);
874		updclst(j);
875		acg.cg_cs.cs_nbfree++;
876	}
877
878	/*
879	 * Handle the last new block if there are stll some new fragments left.
880	 * Here  we don't have to bother about the cluster summary or the  even
881	 * the rotational layout table.
882	 */
883	if (i < (dmax - cbase)) {
884		acg.cg_frsum[dmax - cbase - i]++;
885		for (; i < dmax - cbase; i++) {
886			setbit(cg_blksfree(&acg), i);
887			acg.cg_cs.cs_nffree++;
888		}
889	}
890
891	sblock.fs_cstotal.cs_nffree +=
892	    (acg.cg_cs.cs_nffree - aocg.cg_cs.cs_nffree);
893	sblock.fs_cstotal.cs_nbfree +=
894	    (acg.cg_cs.cs_nbfree - aocg.cg_cs.cs_nbfree);
895	/*
896	 * The following statistics are not changed here:
897	 *     sblock.fs_cstotal.cs_ndir
898	 *     sblock.fs_cstotal.cs_nifree
899	 * As the statistics for this cylinder group are ready, copy it to
900	 * the summary information array.
901	 */
902	*cs = acg.cg_cs;
903
904	/*
905	 * Write the updated "joining" cylinder group back to disk.
906	 */
907	wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)), (size_t)sblock.fs_cgsize,
908	    (void *)&acg, fso, Nflag);
909	DBG_PRINT0("jcg written\n");
910	DBG_DUMP_CG(&sblock, "new joining cg", &acg);
911
912	DBG_LEAVE;
913}
914
915/*
916 * Here  we update the location of the cylinder summary. We have  two  possible
917 * ways of growing the cylinder summary.
918 * (1)	We can try to grow the summary in the current location, and  relocate
919 *	possibly used blocks within the current cylinder group.
920 * (2)	Alternatively we can relocate the whole cylinder summary to the first
921 *	new completely empty cylinder group. Once the cylinder summary is  no
922 *	longer in the beginning of the first cylinder group you should  never
923 *	use  a version of fsck which is not aware of the possibility to  have
924 *	this structure in a non standard place.
925 * Option (1) is considered to be less intrusive to the structure of the  file-
926 * system. So we try to stick to that whenever possible. If there is not enough
927 * space  in the cylinder group containing the cylinder summary we have to  use
928 * method  (2). In case of active snapshots in the filesystem we  probably  can
929 * completely avoid implementing copy on write if we stick to method (2) only.
930 */
931static void
932updcsloc(time_t utime, int fsi, int fso, unsigned int Nflag)
933{
934	DBG_FUNC("updcsloc")
935	struct csum	*cs;
936	int	ocscg, ncscg;
937	int	blocks;
938	daddr_t	cbase, dupper, odupper, d, f, g;
939	int	ind;
940	int	cylno, inc;
941	struct gfs_bpp	*bp;
942	int	i, l;
943	int	lcs = 0;
944	int	block;
945
946	DBG_ENTER;
947
948	if (howmany(sblock.fs_cssize, sblock.fs_fsize) ==
949	    howmany(osblock.fs_cssize, osblock.fs_fsize)) {
950		/*
951		 * No new fragment needed.
952		 */
953		DBG_LEAVE;
954		return;
955	}
956	ocscg = dtog(&osblock, osblock.fs_csaddr);
957	cs = fscs + ocscg;
958	blocks = 1+howmany(sblock.fs_cssize, sblock.fs_bsize)-
959	    howmany(osblock.fs_cssize, osblock.fs_bsize);
960
961	/*
962	 * Read original cylinder group from disk, and make a copy.
963	 * XXX	If Nflag is set in some very rare cases we now miss
964	 *	some changes done in updjcg by reading the unmodified
965	 *	block from disk.
966	 */
967	rdfs(fsbtodb(&osblock, cgtod(&osblock, ocscg)),
968	    (size_t)osblock.fs_cgsize, (void *)&aocg, fsi);
969	DBG_PRINT0("oscg read\n");
970	DBG_DUMP_CG(&sblock, "old summary cg", &aocg);
971
972	memcpy(&cgun1, &cgun2, sizeof(cgun2));
973
974	/*
975	 * Touch the cylinder group, set up local variables needed later
976	 * and update the superblock.
977	 */
978	acg.cg_time = utime;
979
980	/*
981	 * XXX	In the case of having active snapshots we may need much more
982	 *	blocks for the copy on write. We need each block twice,  and
983	 *	also  up to 8*3 blocks for indirect blocks for all  possible
984	 *	references.
985	 */
986	if (/*((int)sblock.fs_time & 0x3) > 0 || */ cs->cs_nbfree < blocks) {
987		/*
988		 * There  is  not enough space in the old cylinder  group  to
989		 * relocate  all blocks as needed, so we relocate  the  whole
990		 * cylinder  group summary to a new group. We try to use  the
991		 * first complete new cylinder group just created. Within the
992		 * cylinder  group we align the area immediately  after  the
993		 * cylinder  group  information location in order  to  be  as
994		 * close as possible to the original implementation of ffs.
995		 *
996		 * First  we have to make sure we'll find enough space in  the
997		 * new  cylinder  group. If not, then we  currently  give  up.
998		 * We  start  with freeing everything which was  used  by  the
999		 * fragments of the old cylinder summary in the current group.
1000		 * Now  we write back the group meta data, read in the  needed
1001		 * meta data from the new cylinder group, and start allocating
1002		 * within  that  group. Here we can assume, the  group  to  be
1003		 * completely empty. Which makes the handling of fragments and
1004		 * clusters a lot easier.
1005		 */
1006		DBG_TRC;
1007		if (sblock.fs_ncg-osblock.fs_ncg < 2)
1008			errx(2, "panic: not enough space");
1009
1010		/*
1011		 * Point "d" to the first fragment not used by the cylinder
1012		 * summary.
1013		 */
1014		d = osblock.fs_csaddr + (osblock.fs_cssize / osblock.fs_fsize);
1015
1016		/*
1017		 * Set up last cluster size ("lcs") already here. Calculate
1018		 * the size for the trailing cluster just behind where  "d"
1019		 * points to.
1020		 */
1021		if (sblock.fs_contigsumsize > 0) {
1022			for (block = howmany(d % sblock.fs_fpg, sblock.fs_frag),
1023			    lcs = 0; lcs < sblock.fs_contigsumsize;
1024			    block++, lcs++) {
1025				if (isclr(cg_clustersfree(&acg), block))
1026					break;
1027			}
1028		}
1029
1030		/*
1031		 * Point "d" to the last frag used by the cylinder summary.
1032		 */
1033		d--;
1034
1035		DBG_PRINT1("d=%jd\n", (intmax_t)d);
1036		if ((d + 1) % sblock.fs_frag) {
1037			/*
1038			 * The end of the cylinder summary is not a complete
1039			 * block.
1040			 */
1041			DBG_TRC;
1042			frag_adjust(d % sblock.fs_fpg, -1);
1043			for (; (d + 1) % sblock.fs_frag; d--) {
1044				DBG_PRINT1("d=%jd\n", (intmax_t)d);
1045				setbit(cg_blksfree(&acg), d % sblock.fs_fpg);
1046				acg.cg_cs.cs_nffree++;
1047				sblock.fs_cstotal.cs_nffree++;
1048			}
1049			/*
1050			 * Point  "d" to the last fragment of the  last
1051			 * (incomplete) block of the cylinder summary.
1052			 */
1053			d++;
1054			frag_adjust(d % sblock.fs_fpg, 1);
1055
1056			if (isblock(&sblock, cg_blksfree(&acg),
1057			    (d % sblock.fs_fpg) / sblock.fs_frag)) {
1058				DBG_PRINT1("d=%jd\n", (intmax_t)d);
1059				acg.cg_cs.cs_nffree -= sblock.fs_frag;
1060				acg.cg_cs.cs_nbfree++;
1061				sblock.fs_cstotal.cs_nffree -= sblock.fs_frag;
1062				sblock.fs_cstotal.cs_nbfree++;
1063				if (sblock.fs_contigsumsize > 0) {
1064					setbit(cg_clustersfree(&acg),
1065					    (d % sblock.fs_fpg) / sblock.fs_frag);
1066					if (lcs < sblock.fs_contigsumsize) {
1067						if (lcs) {
1068							cg_clustersum(&acg)
1069							    [lcs]--;
1070						}
1071						lcs++;
1072						cg_clustersum(&acg)[lcs]++;
1073					}
1074				}
1075			}
1076			/*
1077			 * Point "d" to the first fragment of the block before
1078			 * the last incomplete block.
1079			 */
1080			d--;
1081		}
1082
1083		DBG_PRINT1("d=%jd\n", (intmax_t)d);
1084		for (d = rounddown(d, sblock.fs_frag); d >= osblock.fs_csaddr;
1085		    d -= sblock.fs_frag) {
1086			DBG_TRC;
1087			DBG_PRINT1("d=%d\n", d);
1088			setblock(&sblock, cg_blksfree(&acg),
1089			    (d % sblock.fs_fpg) / sblock.fs_frag);
1090			acg.cg_cs.cs_nbfree++;
1091			sblock.fs_cstotal.cs_nbfree++;
1092			 if (sblock.fs_contigsumsize > 0) {
1093				setbit(cg_clustersfree(&acg),
1094				    (d % sblock.fs_fpg) / sblock.fs_frag);
1095				/*
1096				 * The last cluster size is already set up.
1097				 */
1098				if (lcs < sblock.fs_contigsumsize) {
1099					if (lcs) {
1100						cg_clustersum(&acg)[lcs]--;
1101					}
1102					lcs++;
1103					cg_clustersum(&acg)[lcs]++;
1104				}
1105			}
1106		}
1107		*cs = acg.cg_cs;
1108
1109		/*
1110		 * Now write the former cylinder group containing the cylinder
1111		 * summary back to disk.
1112		 */
1113		wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)),
1114		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1115		DBG_PRINT0("oscg written\n");
1116		DBG_DUMP_CG(&sblock, "old summary cg", &acg);
1117
1118		/*
1119		 * Find the beginning of the new cylinder group containing the
1120		 * cylinder summary.
1121		 */
1122		sblock.fs_csaddr = cgdmin(&sblock, osblock.fs_ncg);
1123		ncscg = dtog(&sblock, sblock.fs_csaddr);
1124		cs = fscs + ncscg;
1125
1126
1127		/*
1128		 * If Nflag is specified, we would now read random data instead
1129		 * of an empty cg structure from disk. So we can't simulate that
1130		 * part for now.
1131		 */
1132		if (Nflag) {
1133			DBG_PRINT0("nscg update skipped\n");
1134			DBG_LEAVE;
1135			return;
1136		}
1137
1138		/*
1139		 * Read the future cylinder group containing the cylinder
1140		 * summary from disk, and make a copy.
1141		 */
1142		rdfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1143		    (size_t)sblock.fs_cgsize, &aocg, fsi);
1144		DBG_PRINT0("nscg read\n");
1145		DBG_DUMP_CG(&sblock, "new summary cg", &aocg);
1146
1147		memcpy(&cgun1, &cgun2, sizeof(cgun2));
1148
1149		/*
1150		 * Allocate all complete blocks used by the new cylinder
1151		 * summary.
1152		 */
1153		for (d = sblock.fs_csaddr; d + sblock.fs_frag <=
1154		    sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize);
1155		    d += sblock.fs_frag) {
1156			clrblock(&sblock, cg_blksfree(&acg),
1157			    (d%sblock.fs_fpg)/sblock.fs_frag);
1158			acg.cg_cs.cs_nbfree--;
1159			sblock.fs_cstotal.cs_nbfree--;
1160			if (sblock.fs_contigsumsize > 0) {
1161				clrbit(cg_clustersfree(&acg),
1162				    (d % sblock.fs_fpg) / sblock.fs_frag);
1163			}
1164		}
1165
1166		/*
1167		 * Allocate all fragments used by the cylinder summary in the
1168		 * last block.
1169		 */
1170		if (d < sblock.fs_csaddr + (sblock.fs_cssize / sblock.fs_fsize)) {
1171			for (; d - sblock.fs_csaddr <
1172			    sblock.fs_cssize/sblock.fs_fsize;
1173			    d++) {
1174				clrbit(cg_blksfree(&acg), d%sblock.fs_fpg);
1175				acg.cg_cs.cs_nffree--;
1176				sblock.fs_cstotal.cs_nffree--;
1177			}
1178			acg.cg_cs.cs_nbfree--;
1179			acg.cg_cs.cs_nffree += sblock.fs_frag;
1180			sblock.fs_cstotal.cs_nbfree--;
1181			sblock.fs_cstotal.cs_nffree += sblock.fs_frag;
1182			if (sblock.fs_contigsumsize > 0) {
1183				clrbit(cg_clustersfree(&acg),
1184				    (d%sblock.fs_fpg) / sblock.fs_frag);
1185			}
1186
1187			frag_adjust(d % sblock.fs_fpg, 1);
1188		}
1189		/*
1190		 * XXX	Handle the cluster statistics here in the case  this
1191		 *	cylinder group is now almost full, and the remaining
1192		 *	space is less then the maximum cluster size. This is
1193		 *	probably not needed, as you would hardly find a file
1194		 *	system which has only MAXCSBUFS+FS_MAXCONTIG of free
1195		 *	space right behind the cylinder group information in
1196		 *	any new cylinder group.
1197		 */
1198
1199		/*
1200		 * Update our statistics in the cylinder summary.
1201		 */
1202		*cs = acg.cg_cs;
1203
1204		/*
1205		 * Write the new cylinder group containing the cylinder summary
1206		 * back to disk.
1207		 */
1208		wtfs(fsbtodb(&sblock, cgtod(&sblock, ncscg)),
1209		    (size_t)sblock.fs_cgsize, (void *)&acg, fso, Nflag);
1210		DBG_PRINT0("nscg written\n");
1211		DBG_DUMP_CG(&sblock, "new summary cg", &acg);
1212
1213		DBG_LEAVE;
1214		return;
1215	}
1216	/*
1217	 * We have got enough of space in the current cylinder group, so we
1218	 * can relocate just a few blocks, and let the summary  information
1219	 * grow in place where it is right now.
1220	 */
1221	DBG_TRC;
1222
1223	cbase = cgbase(&osblock, ocscg);	/* old and new are equal */
1224	dupper = sblock.fs_csaddr - cbase +
1225	    howmany(sblock.fs_cssize, sblock.fs_fsize);
1226	odupper = osblock.fs_csaddr - cbase +
1227	    howmany(osblock.fs_cssize, osblock.fs_fsize);
1228
1229	sblock.fs_dsize -= dupper-odupper;
1230
1231	/*
1232	 * Allocate the space for the array of blocks to be relocated.
1233	 */
1234 	bp = calloc(((dupper-odupper) / sblock.fs_frag + 2),
1235	    sizeof(struct gfs_bpp));
1236	if (bp == NULL)
1237		errx(1, "calloc failed");
1238
1239	/*
1240	 * Lock all new frags needed for the cylinder group summary. This  is
1241	 * done per fragment in the first and last block of the new  required
1242	 * area, and per block for all other blocks.
1243	 *
1244	 * Handle the first new  block here (but only if some fragments where
1245	 * already used for the cylinder summary).
1246	 */
1247	ind = 0;
1248	frag_adjust(odupper, -1);
1249	for (d = odupper; ((d < dupper) && (d % sblock.fs_frag)); d++) {
1250		DBG_PRINT1("scg first frag check loop d=%jd\n", (intmax_t)d);
1251		if (isclr(cg_blksfree(&acg), d)) {
1252			if (!ind) {
1253				bp[ind].old = d / sblock.fs_frag;
1254				bp[ind].flags|=GFS_FL_FIRST;
1255				if (roundup(d, sblock.fs_frag) >= dupper)
1256					bp[ind].flags |= GFS_FL_LAST;
1257				ind++;
1258			}
1259		} else {
1260			clrbit(cg_blksfree(&acg), d);
1261			acg.cg_cs.cs_nffree--;
1262			sblock.fs_cstotal.cs_nffree--;
1263		}
1264		/*
1265		 * No cluster handling is needed here, as there was at least
1266		 * one  fragment in use by the cylinder summary in  the  old
1267		 * filesystem.
1268		 * No block - free counter handling here as this block was not
1269		 * a free block.
1270		 */
1271	}
1272	frag_adjust(odupper, 1);
1273
1274	/*
1275	 * Handle all needed complete blocks here.
1276	 */
1277	for (; d + sblock.fs_frag <= dupper; d += sblock.fs_frag) {
1278		DBG_PRINT1("scg block check loop d=%jd\n", (intmax_t)d);
1279		if (!isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) {
1280			for (f = d; f < d + sblock.fs_frag; f++) {
1281				if (isset(cg_blksfree(&aocg), f)) {
1282					acg.cg_cs.cs_nffree--;
1283					sblock.fs_cstotal.cs_nffree--;
1284				}
1285			}
1286			clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
1287			bp[ind].old = d / sblock.fs_frag;
1288			ind++;
1289		} else {
1290			clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
1291			acg.cg_cs.cs_nbfree--;
1292			sblock.fs_cstotal.cs_nbfree--;
1293			if (sblock.fs_contigsumsize > 0) {
1294				clrbit(cg_clustersfree(&acg), d / sblock.fs_frag);
1295				for (lcs = 0, l = (d / sblock.fs_frag) + 1;
1296				    lcs < sblock.fs_contigsumsize;
1297				    l++, lcs++) {
1298					if (isclr(cg_clustersfree(&acg), l))
1299						break;
1300				}
1301				if (lcs < sblock.fs_contigsumsize) {
1302					cg_clustersum(&acg)[lcs + 1]--;
1303					if (lcs)
1304						cg_clustersum(&acg)[lcs]++;
1305				}
1306			}
1307		}
1308		/*
1309		 * No fragment counter handling is needed here, as this finally
1310		 * doesn't change after the relocation.
1311		 */
1312	}
1313
1314	/*
1315	 * Handle all fragments needed in the last new affected block.
1316	 */
1317	if (d < dupper) {
1318		frag_adjust(dupper - 1, -1);
1319
1320		if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) {
1321			acg.cg_cs.cs_nbfree--;
1322			sblock.fs_cstotal.cs_nbfree--;
1323			acg.cg_cs.cs_nffree+=sblock.fs_frag;
1324			sblock.fs_cstotal.cs_nffree+=sblock.fs_frag;
1325			if (sblock.fs_contigsumsize > 0) {
1326				clrbit(cg_clustersfree(&acg), d / sblock.fs_frag);
1327				for (lcs = 0, l = (d / sblock.fs_frag) + 1;
1328				    lcs < sblock.fs_contigsumsize;
1329				    l++, lcs++) {
1330					if (isclr(cg_clustersfree(&acg), l))
1331						break;
1332				}
1333				if (lcs < sblock.fs_contigsumsize) {
1334					cg_clustersum(&acg)[lcs + 1]--;
1335					if (lcs)
1336						cg_clustersum(&acg)[lcs]++;
1337				}
1338			}
1339		}
1340
1341		for (; d < dupper; d++) {
1342			DBG_PRINT1("scg second frag check loop d=%jd\n",
1343			    (intmax_t)d);
1344			if (isclr(cg_blksfree(&acg), d)) {
1345				bp[ind].old = d / sblock.fs_frag;
1346				bp[ind].flags |= GFS_FL_LAST;
1347			} else {
1348				clrbit(cg_blksfree(&acg), d);
1349				acg.cg_cs.cs_nffree--;
1350				sblock.fs_cstotal.cs_nffree--;
1351			}
1352		}
1353		if (bp[ind].flags & GFS_FL_LAST) /* we have to advance here */
1354			ind++;
1355		frag_adjust(dupper - 1, 1);
1356	}
1357
1358	/*
1359	 * If we found a block to relocate just do so.
1360	 */
1361	if (ind) {
1362		for (i = 0; i < ind; i++) {
1363			if (!bp[i].old) { /* no more blocks listed */
1364				/*
1365				 * XXX	A relative blocknumber should not be
1366				 *	zero,   which  is   not   explicitly
1367				 *	guaranteed by our code.
1368				 */
1369				break;
1370			}
1371			/*
1372			 * Allocate a complete block in the same (current)
1373			 * cylinder group.
1374			 */
1375			bp[i].new = alloc() / sblock.fs_frag;
1376
1377			/*
1378			 * There is no frag_adjust() needed for the new block
1379			 * as it will have no fragments yet :-).
1380			 */
1381			for (f = bp[i].old * sblock.fs_frag,
1382			    g = bp[i].new * sblock.fs_frag;
1383			    f < (bp[i].old + 1) * sblock.fs_frag;
1384			    f++, g++) {
1385				if (isset(cg_blksfree(&aocg), f)) {
1386					setbit(cg_blksfree(&acg), g);
1387					acg.cg_cs.cs_nffree++;
1388					sblock.fs_cstotal.cs_nffree++;
1389				}
1390			}
1391
1392			/*
1393			 * Special handling is required if this was the  first
1394			 * block. We have to consider the fragments which were
1395			 * used by the cylinder summary in the original  block
1396			 * which  re to be free in the copy of our  block.  We
1397			 * have  to be careful if this first block happens  to
1398			 * be also the last block to be relocated.
1399			 */
1400			if (bp[i].flags & GFS_FL_FIRST) {
1401				for (f = bp[i].old * sblock.fs_frag,
1402				    g = bp[i].new * sblock.fs_frag;
1403				    f < odupper;
1404				    f++, g++) {
1405					setbit(cg_blksfree(&acg), g);
1406					acg.cg_cs.cs_nffree++;
1407					sblock.fs_cstotal.cs_nffree++;
1408				}
1409				if (!(bp[i].flags & GFS_FL_LAST))
1410					frag_adjust(bp[i].new * sblock.fs_frag, 1);
1411			}
1412
1413			/*
1414			 * Special handling is required if this is the last
1415			 * block to be relocated.
1416			 */
1417			if (bp[i].flags & GFS_FL_LAST) {
1418				frag_adjust(bp[i].new * sblock.fs_frag, 1);
1419				frag_adjust(bp[i].old * sblock.fs_frag, -1);
1420				for (f = dupper;
1421				    f < roundup(dupper, sblock.fs_frag);
1422				    f++) {
1423					if (isclr(cg_blksfree(&acg), f)) {
1424						setbit(cg_blksfree(&acg), f);
1425						acg.cg_cs.cs_nffree++;
1426						sblock.fs_cstotal.cs_nffree++;
1427					}
1428				}
1429				frag_adjust(bp[i].old * sblock.fs_frag, 1);
1430			}
1431
1432			/*
1433			 * !!! Attach the cylindergroup offset here.
1434			 */
1435			bp[i].old += cbase / sblock.fs_frag;
1436			bp[i].new += cbase / sblock.fs_frag;
1437
1438			/*
1439			 * Copy the content of the block.
1440			 */
1441			/*
1442			 * XXX	Here we will have to implement a copy on write
1443			 *	in the case we have any active snapshots.
1444			 */
1445			rdfs(fsbtodb(&sblock, bp[i].old * sblock.fs_frag),
1446			    (size_t)sblock.fs_bsize, (void *)&ablk, fsi);
1447			wtfs(fsbtodb(&sblock, bp[i].new * sblock.fs_frag),
1448			    (size_t)sblock.fs_bsize, (void *)&ablk, fso, Nflag);
1449			DBG_DUMP_HEX(&sblock, "copied full block",
1450			    (unsigned char *)&ablk);
1451
1452			DBG_PRINT2("scg (%jd->%jd) block relocated\n",
1453			    (intmax_t)bp[i].old, (intmax_t)bp[i].new);
1454		}
1455
1456		/*
1457		 * Now we have to update all references to any fragment which
1458		 * belongs  to any block relocated. We iterate now  over  all
1459		 * cylinder  groups,  within those over all non  zero  length
1460		 * inodes.
1461		 */
1462		for (cylno = 0; cylno < osblock.fs_ncg; cylno++) {
1463			DBG_PRINT1("scg doing cg (%d)\n", cylno);
1464			for (inc = osblock.fs_ipg - 1; inc > 0; inc--) {
1465				updrefs(cylno, (ino_t)inc, bp, fsi, fso, Nflag);
1466			}
1467		}
1468
1469		/*
1470		 * All inodes are checked, now make sure the number of
1471		 * references found make sense.
1472		 */
1473		for (i = 0; i < ind; i++) {
1474			if (!bp[i].found || (bp[i].found > sblock.fs_frag)) {
1475				warnx("error: %jd refs found for block %jd.",
1476				    (intmax_t)bp[i].found, (intmax_t)bp[i].old);
1477			}
1478
1479		}
1480	}
1481	/*
1482	 * The following statistics are not changed here:
1483	 *     sblock.fs_cstotal.cs_ndir
1484	 *     sblock.fs_cstotal.cs_nifree
1485	 * The following statistics were already updated on the fly:
1486	 *     sblock.fs_cstotal.cs_nffree
1487	 *     sblock.fs_cstotal.cs_nbfree
1488	 * As the statistics for this cylinder group are ready, copy it to
1489	 * the summary information array.
1490	 */
1491
1492	*cs = acg.cg_cs;
1493
1494	/*
1495	 * Write summary cylinder group back to disk.
1496	 */
1497	wtfs(fsbtodb(&sblock, cgtod(&sblock, ocscg)), (size_t)sblock.fs_cgsize,
1498	    (void *)&acg, fso, Nflag);
1499	DBG_PRINT0("scg written\n");
1500	DBG_DUMP_CG(&sblock, "new summary cg", &acg);
1501
1502	DBG_LEAVE;
1503}
1504
1505/*
1506 * Here we read some block(s) from disk.
1507 */
1508static void
1509rdfs(daddr_t bno, size_t size, void *bf, int fsi)
1510{
1511	DBG_FUNC("rdfs")
1512	ssize_t	n;
1513
1514	DBG_ENTER;
1515
1516	if (bno < 0) {
1517		err(32, "rdfs: attempting to read negative block number");
1518	}
1519	if (lseek(fsi, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1520		err(33, "rdfs: seek error: %jd", (intmax_t)bno);
1521	}
1522	n = read(fsi, bf, size);
1523	if (n != (ssize_t)size) {
1524		err(34, "rdfs: read error: %jd", (intmax_t)bno);
1525	}
1526
1527	DBG_LEAVE;
1528}
1529
1530/*
1531 * Here we write some block(s) to disk.
1532 */
1533static void
1534wtfs(daddr_t bno, size_t size, void *bf, int fso, unsigned int Nflag)
1535{
1536	DBG_FUNC("wtfs")
1537	ssize_t	n;
1538
1539	DBG_ENTER;
1540
1541	if (Nflag) {
1542		DBG_LEAVE;
1543		return;
1544	}
1545	if (lseek(fso, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0) {
1546		err(35, "wtfs: seek error: %ld", (long)bno);
1547	}
1548	n = write(fso, bf, size);
1549	if (n != (ssize_t)size) {
1550		err(36, "wtfs: write error: %ld", (long)bno);
1551	}
1552
1553	DBG_LEAVE;
1554}
1555
1556/*
1557 * Here we allocate a free block in the current cylinder group. It is assumed,
1558 * that  acg contains the current cylinder group. As we may take a block  from
1559 * somewhere in the filesystem we have to handle cluster summary here.
1560 */
1561static daddr_t
1562alloc(void)
1563{
1564	DBG_FUNC("alloc")
1565	daddr_t	d, blkno;
1566	int	lcs1, lcs2;
1567	int	l;
1568	int	csmin, csmax;
1569	int	dlower, dupper, dmax;
1570
1571	DBG_ENTER;
1572
1573	if (acg.cg_magic != CG_MAGIC) {
1574		warnx("acg: bad magic number");
1575		DBG_LEAVE;
1576		return (0);
1577	}
1578	if (acg.cg_cs.cs_nbfree == 0) {
1579		warnx("error: cylinder group ran out of space");
1580		DBG_LEAVE;
1581		return (0);
1582	}
1583	/*
1584	 * We start seeking for free blocks only from the space available after
1585	 * the  end of the new grown cylinder summary. Otherwise we allocate  a
1586	 * block here which we have to relocate a couple of seconds later again
1587	 * again, and we are not prepared to to this anyway.
1588	 */
1589	blkno = -1;
1590	dlower = cgsblock(&sblock, acg.cg_cgx) - cgbase(&sblock, acg.cg_cgx);
1591	dupper = cgdmin(&sblock, acg.cg_cgx) - cgbase(&sblock, acg.cg_cgx);
1592	dmax = cgbase(&sblock, acg.cg_cgx) + sblock.fs_fpg;
1593	if (dmax > sblock.fs_size) {
1594		dmax = sblock.fs_size;
1595	}
1596	dmax -= cgbase(&sblock, acg.cg_cgx); /* retransform into cg */
1597	csmin=sblock.fs_csaddr-cgbase(&sblock, acg.cg_cgx);
1598	csmax = csmin + howmany(sblock.fs_cssize, sblock.fs_fsize);
1599	DBG_PRINT3("seek range: dl=%d, du=%d, dm=%d\n", dlower, dupper, dmax);
1600	DBG_PRINT2("range cont: csmin=%d, csmax=%d\n", csmin, csmax);
1601
1602	for (d = 0; (d < dlower && blkno == -1); d += sblock.fs_frag) {
1603		if (d >= csmin && d <= csmax) {
1604			continue;
1605		}
1606		if (isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1607		    d))) {
1608			blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1609			break;
1610		}
1611	}
1612	for (d = dupper; (d < dmax && blkno == -1); d += sblock.fs_frag) {
1613		if (d >= csmin && d <= csmax) {
1614			continue;
1615		}
1616		if (isblock(&sblock, cg_blksfree(&acg), fragstoblks(&sblock,
1617		    d))) {
1618			blkno = fragstoblks(&sblock, d);/* Yeah found a block */
1619			break;
1620		}
1621	}
1622	if (blkno == -1) {
1623		warnx("internal error: couldn't find promised block in cg");
1624		DBG_LEAVE;
1625		return (0);
1626	}
1627
1628	/*
1629	 * This is needed if the block was found already in the first loop.
1630	 */
1631	d = blkstofrags(&sblock, blkno);
1632
1633	clrblock(&sblock, cg_blksfree(&acg), blkno);
1634	if (sblock.fs_contigsumsize > 0) {
1635		/*
1636		 * Handle the cluster allocation bitmap.
1637		 */
1638		clrbit(cg_clustersfree(&acg), blkno);
1639		/*
1640		 * We  possibly have split a cluster here, so we have  to  do
1641		 * recalculate the sizes of the remaining cluster halves now,
1642		 * and use them for updating the cluster summary information.
1643		 *
1644		 * Lets start with the blocks before our allocated block ...
1645		 */
1646		for (lcs1 = 0, l = blkno - 1; lcs1 < sblock.fs_contigsumsize;
1647		    l--, lcs1++) {
1648			if (isclr(cg_clustersfree(&acg), l))
1649				break;
1650		}
1651		/*
1652		 * ... and continue with the blocks right after our allocated
1653		 * block.
1654		 */
1655		for (lcs2 = 0, l = blkno + 1; lcs2 < sblock.fs_contigsumsize;
1656		    l++, lcs2++) {
1657			if (isclr(cg_clustersfree(&acg), l))
1658				break;
1659		}
1660
1661		/*
1662		 * Now update all counters.
1663		 */
1664		cg_clustersum(&acg)[MINIMUM(lcs1 + lcs2 + 1, sblock.fs_contigsumsize)]--;
1665		if (lcs1)
1666			cg_clustersum(&acg)[lcs1]++;
1667		if (lcs2)
1668			cg_clustersum(&acg)[lcs2]++;
1669	}
1670	/*
1671	 * Update all statistics based on blocks.
1672	 */
1673	acg.cg_cs.cs_nbfree--;
1674	sblock.fs_cstotal.cs_nbfree--;
1675
1676	DBG_LEAVE;
1677	return (d);
1678}
1679
1680/*
1681 * Here  we check if all frags of a block are free. For more details  again
1682 * please see the source of newfs(8), as this function is taken over almost
1683 * unchanged.
1684 */
1685static int
1686isblock(struct fs *fs, unsigned char *cp, int h)
1687{
1688	DBG_FUNC("isblock")
1689	unsigned char	mask;
1690
1691	DBG_ENTER;
1692
1693	switch (fs->fs_frag) {
1694	case 8:
1695		DBG_LEAVE;
1696		return (cp[h] == 0xff);
1697	case 4:
1698		mask = 0x0f << ((h & 0x1) << 2);
1699		DBG_LEAVE;
1700		return ((cp[h >> 1] & mask) == mask);
1701	case 2:
1702		mask = 0x03 << ((h & 0x3) << 1);
1703		DBG_LEAVE;
1704		return ((cp[h >> 2] & mask) == mask);
1705	case 1:
1706		mask = 0x01 << (h & 0x7);
1707		DBG_LEAVE;
1708		return ((cp[h >> 3] & mask) == mask);
1709	default:
1710		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
1711		DBG_LEAVE;
1712		return (0);
1713	}
1714}
1715
1716/*
1717 * Here we allocate a complete block in the block map. For more details again
1718 * please  see the source of newfs(8), as this function is taken over  almost
1719 * unchanged.
1720 */
1721static void
1722clrblock(struct fs *fs, unsigned char *cp, int h)
1723{
1724	DBG_FUNC("clrblock")
1725
1726	DBG_ENTER;
1727
1728	switch ((fs)->fs_frag) {
1729	case 8:
1730		cp[h] = 0;
1731		break;
1732	case 4:
1733		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1734		break;
1735	case 2:
1736		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1737		break;
1738	case 1:
1739		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1740		break;
1741	default:
1742		warnx("clrblock bad fs_frag %d", fs->fs_frag);
1743		break;
1744	}
1745
1746	DBG_LEAVE;
1747}
1748
1749/*
1750 * Here we free a complete block in the free block map. For more details again
1751 * please  see the source of newfs(8), as this function is taken  over  almost
1752 * unchanged.
1753 */
1754static void
1755setblock(struct fs *fs, unsigned char *cp, int h)
1756{
1757	DBG_FUNC("setblock")
1758
1759	DBG_ENTER;
1760
1761	switch (fs->fs_frag) {
1762	case 8:
1763		cp[h] = 0xff;
1764		break;
1765	case 4:
1766		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1767		break;
1768	case 2:
1769		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1770		break;
1771	case 1:
1772		cp[h >> 3] |= (0x01 << (h & 0x7));
1773		break;
1774	default:
1775		warnx("setblock bad fs_frag %d", fs->fs_frag);
1776		break;
1777	}
1778
1779	DBG_LEAVE;
1780}
1781
1782/*
1783 * This function provides access to an individual inode. We find out in which
1784 * block  the  requested inode is located, read it from disk if  needed,  and
1785 * return  the pointer into that block. We maintain a cache of one  block  to
1786 * not  read the same block again and again if we iterate linearly  over  all
1787 * inodes.
1788 */
1789static union dinode *
1790ginode(ino_t inumber, int fsi, int cg)
1791{
1792	DBG_FUNC("ginode")
1793	static ino_t	startinum = 0;	/* first inode in cached block */
1794
1795	DBG_ENTER;
1796
1797	/*
1798	 * The inumber passed in is relative to the cg, so use it here to see
1799	 * if the inode has been allocated yet.
1800	 */
1801	if (isclr(cg_inosused(&aocg), inumber)) {
1802		DBG_LEAVE;
1803		return NULL;
1804	}
1805	/*
1806	 * Now make the inumber relative to the entire inode space so it can
1807	 * be sanity checked.
1808	 */
1809	inumber += (cg * sblock.fs_ipg);
1810	if (inumber < ROOTINO) {
1811		DBG_LEAVE;
1812		return NULL;
1813	}
1814	if (inumber > maxino)
1815		errx(8, "bad inode number %llu to ginode",
1816		    (unsigned long long)inumber);
1817	if (startinum == 0 ||
1818	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
1819		inoblk = fsbtodb(&sblock, ino_to_fsba(&sblock, inumber));
1820		rdfs(inoblk, (size_t)sblock.fs_bsize, inobuf, fsi);
1821		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
1822	}
1823	DBG_LEAVE;
1824	if (sblock.fs_magic == FS_UFS1_MAGIC)
1825		return (union dinode *)((uintptr_t)inobuf +
1826		    (inumber % INOPB(&sblock)) * sizeof(struct ufs1_dinode));
1827	return (union dinode *)((uintptr_t)inobuf +
1828	    (inumber % INOPB(&sblock)) * sizeof(struct ufs2_dinode));
1829}
1830
1831/*
1832 * Figure out how many lines our current terminal has. For more details again
1833 * please  see the source of newfs(8), as this function is taken over  almost
1834 * unchanged.
1835 */
1836static int
1837charsperline(void)
1838{
1839	DBG_FUNC("charsperline")
1840	int	columns;
1841	char	*cp;
1842	struct winsize	ws;
1843
1844	DBG_ENTER;
1845
1846	columns = 0;
1847	if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
1848		columns = ws.ws_col;
1849	}
1850	if (columns == 0 && (cp = getenv("COLUMNS"))) {
1851		columns = strtonum(cp, 1, INT_MAX, NULL);
1852	}
1853	if (columns == 0) {
1854		columns = 80;	/* last resort */
1855	}
1856
1857	DBG_LEAVE;
1858	return columns;
1859}
1860
1861/*
1862 * growfs(8) is a utility which allows to increase the size of an existing
1863 * ufs filesystem. Currently this can only be done on unmounted file system.
1864 * It recognizes some command line options to specify the new desired size,
1865 * and it does some basic checkings. The old filesystem size is determined
1866 * and after some more checks like we can really access the new last block
1867 * on the disk etc. we calculate the new parameters for the superblock. After
1868 * having done this we just call growfs() which will do the work. Before
1869 * we finish the only thing left is to update the disklabel.
1870 * We still have to provide support for snapshots. Therefore we first have to
1871 * understand what data structures are always replicated in the snapshot on
1872 * creation, for all other blocks we touch during our procedure, we have to
1873 * keep the old blocks unchanged somewhere available for the snapshots. If we
1874 * are lucky, then we only have to handle our blocks to be relocated in that
1875 * way.
1876 * Also we have to consider in what order we actually update the critical
1877 * data structures of the filesystem to make sure, that in case of a disaster
1878 * fsck(8) is still able to restore any lost data.
1879 * The foreseen last step then will be to provide for growing even mounted
1880 * file systems. There we have to extend the mount() system call to provide
1881 * userland access to the filesystem locking facility.
1882 */
1883int
1884main(int argc, char **argv)
1885{
1886	DBG_FUNC("main")
1887	char	*device, *lastsector;
1888	int	ch;
1889	long long	size = 0;
1890	unsigned int	Nflag = 0;
1891	int	ExpertFlag = 0;
1892	struct stat	st;
1893	struct disklabel	*lp;
1894	struct partition	*pp;
1895	int	i,fsi,fso;
1896	char	reply[5];
1897	const char *errstr;
1898#ifdef FSMAXSNAP
1899	int	j;
1900#endif /* FSMAXSNAP */
1901
1902	DBG_ENTER;
1903
1904	while ((ch = getopt(argc, argv, "Nqs:vy")) != -1) {
1905		switch (ch) {
1906		case 'N':
1907			Nflag = 1;
1908			break;
1909		case 'q':
1910			quiet = 1;
1911			break;
1912		case 's':
1913			size = strtonum(optarg, 1, LLONG_MAX, &errstr);
1914			if (errstr)
1915				usage();
1916			break;
1917		case 'v': /* for compatibility to newfs */
1918			break;
1919		case 'y':
1920			ExpertFlag = 1;
1921			break;
1922		case '?':
1923			/* FALLTHROUGH */
1924		default:
1925			usage();
1926		}
1927	}
1928	argc -= optind;
1929	argv += optind;
1930
1931	if (argc != 1)
1932		usage();
1933
1934	/*
1935	 * Rather than guessing, use opendev() to get the device
1936	 * name, which we open for reading.
1937	 */
1938	if ((fsi = opendev(*argv, O_RDONLY, 0, &device)) < 0)
1939		err(1, "%s", *argv);
1940
1941	/*
1942	 * Try to access our devices for writing ...
1943	 */
1944	if (Nflag) {
1945		fso = -1;
1946	} else {
1947		fso = open(device, O_WRONLY);
1948		if (fso < 0)
1949			err(1, "%s", device);
1950	}
1951
1952	/*
1953	 * Now we have a file descriptor for our device, fstat() it to
1954	 * figure out the partition number.
1955	 */
1956	if (fstat(fsi, &st) != 0)
1957		err(1, "%s: fstat()", device);
1958
1959	/*
1960	 * Try to read a label from the disk.  Then get the partition from the
1961	 * device minor number, using DISKPART().  Probably don't need to
1962	 * check against getmaxpartitions().
1963	 */
1964	lp = get_disklabel(fsi);
1965	if (DISKPART(st.st_rdev) < getmaxpartitions())
1966		pp = &lp->d_partitions[DISKPART(st.st_rdev)];
1967	else
1968		errx(1, "%s: invalid partition number %u",
1969		    device, DISKPART(st.st_rdev));
1970
1971	/*
1972	 * Check if that partition is suitable for growing a file system.
1973	 */
1974	if (DL_GETPSIZE(pp) < 1)
1975		errx(1, "partition is unavailable");
1976	if (pp->p_fstype != FS_BSDFFS)
1977		errx(1, "can only grow ffs partitions");
1978
1979	/*
1980	 * Read the current superblock, and take a backup.
1981	 */
1982	for (i = 0; sblock_try[i] != -1; i++) {
1983		sblockloc = sblock_try[i] / DEV_BSIZE;
1984		rdfs(sblockloc, (size_t)SBLOCKSIZE, (void *)&(osblock), fsi);
1985		if ((osblock.fs_magic == FS_UFS1_MAGIC ||
1986		     (osblock.fs_magic == FS_UFS2_MAGIC &&
1987		      osblock.fs_sblockloc == sblock_try[i])) &&
1988		    osblock.fs_bsize <= MAXBSIZE &&
1989		    osblock.fs_bsize >= (int32_t) sizeof(struct fs))
1990			break;
1991	}
1992	if (sblock_try[i] == -1)
1993		errx(1, "superblock not recognized");
1994	if (osblock.fs_clean == 0)
1995		errx(1, "filesystem not clean - run fsck");
1996	if (sblock.fs_magic == FS_UFS1_MAGIC &&
1997	    (sblock.fs_ffs1_flags & FS_FLAGS_UPDATED) == 0)
1998		ffs1_sb_update(&sblock, sblock_try[i]);
1999	memcpy(&fsun1, &fsun2, sizeof(fsun2));
2000	maxino = sblock.fs_ncg * sblock.fs_ipg;
2001
2002	DBG_OPEN("/tmp/growfs.debug"); /* already here we need a superblock */
2003	DBG_DUMP_FS(&sblock, "old sblock");
2004
2005	/*
2006	 * Determine size to grow to. Default to the full size specified in
2007	 * the disk label.
2008	 */
2009	sblock.fs_size = dbtofsb(&osblock, DL_SECTOBLK(lp, DL_GETPSIZE(pp)));
2010	if (size != 0) {
2011		if (size > DL_GETPSIZE(pp)) {
2012			errx(1, "there is not enough space (%llu < %lld)",
2013			    DL_GETPSIZE(pp), size);
2014		}
2015		sblock.fs_size = dbtofsb(&osblock, DL_SECTOBLK(lp, size));
2016	}
2017
2018	/*
2019	 * Are we really growing ?
2020	 */
2021	if (osblock.fs_size >= sblock.fs_size) {
2022		errx(1, "we are not growing (%jd->%jd)",
2023		    (intmax_t)osblock.fs_size, (intmax_t)sblock.fs_size);
2024	}
2025
2026
2027#ifdef FSMAXSNAP
2028	/*
2029	 * Check if we find an active snapshot.
2030	 */
2031	if (ExpertFlag == 0) {
2032		for (j = 0; j < FSMAXSNAP; j++) {
2033			if (sblock.fs_snapinum[j]) {
2034				errx(1, "active snapshot found in filesystem\n"
2035				    "	please remove all snapshots before "
2036				    "using growfs");
2037			}
2038			if (!sblock.fs_snapinum[j])	/* list is dense */
2039				break;
2040		}
2041	}
2042#endif
2043
2044	if (ExpertFlag == 0 && Nflag == 0) {
2045		printf("We strongly recommend you to make a backup "
2046		    "before growing the Filesystem\n\n"
2047		    " Did you backup your data (Yes/No) ? ");
2048		if (fgets(reply, (int)sizeof(reply), stdin) == NULL ||
2049		    strncasecmp(reply, "Yes", 3)) {
2050			printf("\n Nothing done \n");
2051			exit (0);
2052		}
2053	}
2054
2055	if (!quiet)
2056		printf("new filesystem size is: %jd frags\n",
2057		    (intmax_t)sblock.fs_size);
2058
2059	/*
2060	 * Try to access our new last sector in the filesystem. Even if we
2061	 * later on realize we have to abort our operation, on that sector
2062	 * there should be no data, so we can't destroy something yet.
2063	 */
2064	lastsector = calloc(1, lp->d_secsize);
2065	if (!lastsector)
2066		err(1, "No memory for last sector test write");
2067	wtfs(DL_SECTOBLK(lp, DL_GETPSIZE(pp) - 1), lp->d_secsize,
2068	    lastsector, fso, Nflag);
2069	free(lastsector);
2070
2071	/*
2072	 * Now calculate new superblock values and check for reasonable
2073	 * bound for new filesystem size:
2074	 *     fs_size:    is derived from label or user input
2075	 *     fs_dsize:   should get updated in the routines creating or
2076	 *                 updating the cylinder groups on the fly
2077	 *     fs_cstotal: should get updated in the routines creating or
2078	 *                 updating the cylinder groups
2079	 */
2080
2081	/*
2082	 * Update the number of cylinders and cylinder groups in the file system.
2083	 */
2084	if (sblock.fs_magic == FS_UFS1_MAGIC) {
2085		sblock.fs_ncyl = sblock.fs_size * NSPF(&sblock) / sblock.fs_spc;
2086		if (sblock.fs_size * NSPF(&sblock) >
2087		    sblock.fs_ncyl * sblock.fs_spc)
2088		sblock.fs_ncyl++;
2089	}
2090	sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
2091	maxino = sblock.fs_ncg * sblock.fs_ipg;
2092
2093	if (sblock.fs_size % sblock.fs_fpg != 0 &&
2094	    sblock.fs_size % sblock.fs_fpg < cgdmin(&sblock, sblock.fs_ncg)) {
2095		/*
2096		 * The space in the new last cylinder group is too small,
2097		 * so revert back.
2098		 */
2099		sblock.fs_ncg--;
2100		if (sblock.fs_magic == FS_UFS1_MAGIC)
2101			sblock.fs_ncyl = sblock.fs_ncg * sblock.fs_cpg;
2102		if (!quiet)
2103			printf("Warning: %jd sector(s) cannot be allocated.\n",
2104			    (intmax_t)fsbtodb(&sblock,
2105			    sblock.fs_size % sblock.fs_fpg));
2106		sblock.fs_size = sblock.fs_ncg * sblock.fs_fpg;
2107	}
2108
2109	/*
2110	 * Update the space for the cylinder group summary information in the
2111	 * respective cylinder group data area.
2112	 */
2113	sblock.fs_cssize =
2114	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
2115
2116	if (osblock.fs_size >= sblock.fs_size)
2117		errx(1, "not enough new space");
2118
2119	DBG_PRINT0("sblock calculated\n");
2120
2121	/*
2122	 * Ok, everything prepared, so now let's do the tricks.
2123	 */
2124	growfs(fsi, fso, Nflag);
2125
2126	/*
2127	 * Update the disk label.
2128	 */
2129	pp->p_fragblock =
2130	    DISKLABELV1_FFS_FRAGBLOCK(sblock.fs_fsize, sblock.fs_frag);
2131        pp->p_cpg = sblock.fs_fpg;
2132
2133	return_disklabel(fso, lp, Nflag);
2134	DBG_PRINT0("label rewritten\n");
2135
2136	close(fsi);
2137	if (fso > -1)
2138		close(fso);
2139
2140	DBG_CLOSE;
2141
2142	DBG_LEAVE;
2143	return 0;
2144}
2145
2146/*
2147 * Write the updated disklabel back to disk.
2148 */
2149static void
2150return_disklabel(int fd, struct disklabel *lp, unsigned int Nflag)
2151{
2152	DBG_FUNC("return_disklabel")
2153	u_short	sum;
2154	u_short	*ptr;
2155
2156	DBG_ENTER;
2157
2158	if (!lp) {
2159		DBG_LEAVE;
2160		return;
2161	}
2162	if (!Nflag) {
2163		lp->d_checksum = 0;
2164		sum = 0;
2165		ptr = (u_short *)lp;
2166
2167		/*
2168		 * recalculate checksum
2169		 */
2170		while (ptr < (u_short *)&lp->d_partitions[lp->d_npartitions])
2171			sum ^= *ptr++;
2172		lp->d_checksum = sum;
2173
2174		if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0)
2175			errx(1, "DIOCWDINFO failed");
2176	}
2177	free(lp);
2178
2179	DBG_LEAVE;
2180	return ;
2181}
2182
2183/*
2184 * Read the disklabel from disk.
2185 */
2186static struct disklabel *
2187get_disklabel(int fd)
2188{
2189	DBG_FUNC("get_disklabel")
2190	static struct	disklabel *lab;
2191
2192	DBG_ENTER;
2193
2194	lab = malloc(sizeof(struct disklabel));
2195	if (!lab)
2196		errx(1, "malloc failed");
2197	if (ioctl(fd, DIOCGDINFO, (char *)lab) != 0)
2198		err(1, "DIOCGDINFO");
2199
2200	DBG_LEAVE;
2201	return (lab);
2202}
2203
2204
2205/*
2206 * Dump a line of usage.
2207 */
2208static void
2209usage(void)
2210{
2211	DBG_FUNC("usage")
2212
2213	DBG_ENTER;
2214
2215	fprintf(stderr, "usage: growfs [-Nqy] [-s size] special\n");
2216
2217	DBG_LEAVE;
2218	exit(1);
2219}
2220
2221/*
2222 * This updates most parameters and the bitmap related to cluster. We have to
2223 * assume that sblock, osblock, acg are set up.
2224 */
2225static void
2226updclst(int block)
2227{
2228	DBG_FUNC("updclst")
2229	static int	lcs = 0;
2230
2231	DBG_ENTER;
2232
2233	if (sblock.fs_contigsumsize < 1)	/* no clustering */
2234		return;
2235
2236	/*
2237	 * update cluster allocation map
2238	 */
2239	setbit(cg_clustersfree(&acg), block);
2240
2241	/*
2242	 * update cluster summary table
2243	 */
2244	if (!lcs) {
2245		/*
2246		 * calculate size for the trailing cluster
2247		 */
2248		for (block--; lcs < sblock.fs_contigsumsize; block--, lcs++) {
2249			if (isclr(cg_clustersfree(&acg), block))
2250				break;
2251		}
2252	}
2253	if (lcs < sblock.fs_contigsumsize) {
2254		if (lcs)
2255			cg_clustersum(&acg)[lcs]--;
2256		lcs++;
2257		cg_clustersum(&acg)[lcs]++;
2258	}
2259
2260	DBG_LEAVE;
2261}
2262
2263/*
2264 * This updates all references to relocated blocks for the given inode.  The
2265 * inode is given as number within the cylinder group, and the number of the
2266 * cylinder group.
2267 */
2268static void
2269updrefs(int cg, ino_t in, struct gfs_bpp *bp, int fsi, int fso, unsigned int
2270    Nflag)
2271{
2272	DBG_FUNC("updrefs")
2273	daddr_t	len, lbn, numblks;
2274	daddr_t	iptr, blksperindir;
2275	union dinode	*ino;
2276	int		i, mode, inodeupdated;
2277
2278	DBG_ENTER;
2279
2280	ino = ginode(in, fsi, cg);
2281	if (ino == NULL) {
2282		DBG_LEAVE;
2283		return;
2284	}
2285	mode = DIP(ino, di_mode) & IFMT;
2286	if (mode != IFDIR && mode != IFREG && mode != IFLNK) {
2287		DBG_LEAVE;
2288		return; /* only check DIR, FILE, LINK */
2289	}
2290	if (mode == IFLNK &&
2291	    DIP(ino, di_size) < (u_int64_t) sblock.fs_maxsymlinklen) {
2292		DBG_LEAVE;
2293		return;	/* skip short symlinks */
2294	}
2295	numblks = howmany(DIP(ino, di_size), sblock.fs_bsize);
2296	if (numblks == 0) {
2297		DBG_LEAVE;
2298		return;	/* skip empty file */
2299	}
2300	if (DIP(ino, di_blocks) == 0) {
2301		DBG_LEAVE;
2302		return;	/* skip empty swiss cheesy file or old fastlink */
2303	}
2304	DBG_PRINT2("scg checking inode (%llu in %d)\n",
2305	    (unsigned long long)in, cg);
2306
2307	/*
2308	 * Check all the blocks.
2309	 */
2310	inodeupdated = 0;
2311	len = numblks < NDADDR ? numblks : NDADDR;
2312	for (i = 0; i < len; i++) {
2313		iptr = DIP(ino, di_db[i]);
2314		if (iptr == 0)
2315			continue;
2316		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2317			DIP_SET(ino, di_db[i], iptr);
2318			inodeupdated++;
2319		}
2320	}
2321	DBG_PRINT0("~~scg direct blocks checked\n");
2322
2323	blksperindir = 1;
2324	len = numblks - NDADDR;
2325	lbn = NDADDR;
2326	for (i = 0; len > 0 && i < NIADDR; i++) {
2327		iptr = DIP(ino, di_ib[i]);
2328		if (iptr == 0)
2329			continue;
2330		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2331			DIP_SET(ino, di_ib[i], iptr);
2332			inodeupdated++;
2333		}
2334		indirchk(blksperindir, lbn, iptr, numblks, bp, fsi, fso, Nflag);
2335		blksperindir *= NINDIR(&sblock);
2336		lbn += blksperindir;
2337		len -= blksperindir;
2338		DBG_PRINT1("scg indirect_%d blocks checked\n", i + 1);
2339	}
2340	if (inodeupdated)
2341		wtfs(inoblk, sblock.fs_bsize, inobuf, fso, Nflag);
2342
2343	DBG_LEAVE;
2344}
2345
2346/*
2347 * Recursively check all the indirect blocks.
2348 */
2349static void
2350indirchk(daddr_t blksperindir, daddr_t lbn, daddr_t blkno,
2351    daddr_t lastlbn, struct gfs_bpp *bp, int fsi, int fso, unsigned int Nflag)
2352{
2353	DBG_FUNC("indirchk")
2354	void *ibuf;
2355	int i, last;
2356	daddr_t iptr;
2357
2358	DBG_ENTER;
2359
2360	/* read in the indirect block. */
2361	ibuf = malloc(sblock.fs_bsize);
2362	if (!ibuf)
2363		errx(1, "malloc failed");
2364	rdfs(fsbtodb(&sblock, blkno), (size_t)sblock.fs_bsize, ibuf, fsi);
2365	last = howmany(lastlbn - lbn, blksperindir) < NINDIR(&sblock) ?
2366	    howmany(lastlbn - lbn, blksperindir) : NINDIR(&sblock);
2367	for (i = 0; i < last; i++) {
2368		if (sblock.fs_magic == FS_UFS1_MAGIC)
2369			iptr = ((int32_t *)ibuf)[i];
2370		else
2371			iptr = ((daddr_t *)ibuf)[i];
2372		if (iptr == 0)
2373			continue;
2374		if (cond_bl_upd(&iptr, bp, fsi, fso, Nflag)) {
2375			if (sblock.fs_magic == FS_UFS1_MAGIC)
2376				((int32_t *)ibuf)[i] = iptr;
2377			else
2378				((daddr_t *)ibuf)[i] = iptr;
2379		}
2380		if (blksperindir == 1)
2381			continue;
2382		indirchk(blksperindir / NINDIR(&sblock), lbn + blksperindir * i,
2383		    iptr, lastlbn, bp, fsi, fso, Nflag);
2384	}
2385	free(ibuf);
2386
2387	DBG_LEAVE;
2388}
2389
2390static void
2391ffs1_sb_update(struct fs *fs, daddr_t sbloc)
2392{
2393	fs->fs_flags = fs->fs_ffs1_flags;
2394	fs->fs_sblockloc = sbloc;
2395	fs->fs_maxbsize = fs->fs_bsize;
2396	fs->fs_time = fs->fs_ffs1_time;
2397	fs->fs_size = fs->fs_ffs1_size;
2398	fs->fs_dsize = fs->fs_ffs1_dsize;
2399	fs->fs_csaddr = fs->fs_ffs1_csaddr;
2400	fs->fs_cstotal.cs_ndir = fs->fs_ffs1_cstotal.cs_ndir;
2401	fs->fs_cstotal.cs_nbfree = fs->fs_ffs1_cstotal.cs_nbfree;
2402	fs->fs_cstotal.cs_nifree = fs->fs_ffs1_cstotal.cs_nifree;
2403	fs->fs_cstotal.cs_nffree = fs->fs_ffs1_cstotal.cs_nffree;
2404	fs->fs_ffs1_flags |= FS_FLAGS_UPDATED;
2405}
2406