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